1. 概述

之前聊了一下 Elasticsearch 的安裝,今天我們來說說 Elasticsearch 的基本使用。

2. Elasticsearch索引的使用

索引(index)相當於是mysql中的表。

2.1 建立索引

1)Head外掛方式

選擇 索引 頁籤,點選【新建索引】按鈕,輸入索引名稱、分片數、副本數,點選【OK】

之所以叢集健康值呈現黃色,是因為目前是用單伺服器跑的Elasticsearch,而副本是要儲存在不同的伺服器上的,之後會聊一下 Elasticsearch 叢集的搭建。

2)RESTFUL介面方式

PUT  http://192.168.1.11:9200/index_user

引數:

  1. {
  2. "settings":{
  3. "index":{
  4. "number_of_shards":5, // 分片數
  5. "number_of_replicas":0 // 副本數
  6. }
  7. }
  8. }

 2.2 檢視叢集健康狀況

RESTFUL介面方式

GET  http://192.168.1.11:9200/_cluster/health

響應:

  1. {
  2. "cluster_name": "zhuifengren-es",
  3. "status": "yellow",
  4. "timed_out": false,
  5. "number_of_nodes": 1,
  6. "number_of_data_nodes": 1,
  7. "active_primary_shards": 6,
  8. "active_shards": 6,
  9. "relocating_shards": 0,
  10. "initializing_shards": 0,
  11. "unassigned_shards": 5,
  12. "delayed_unassigned_shards": 0,
  13. "number_of_pending_tasks": 0,
  14. "number_of_in_flight_fetch": 0,
  15. "task_max_waiting_in_queue_millis": 0,
  16. "active_shards_percent_as_number": 54.54545454545454
  17. }

2.3 刪除索引

1)Head外掛方式

在 概覽 頁籤,找到需要刪除的索引,選擇 動作 —> 刪除...

2)RESTFUL介面方式

DELETE  http://192.168.1.11:9200/index_user

2.4 檢視叢集整體資訊

RESTFUL介面方式

GET  http://192.168.1.11:9200/_cat/indices?v

響應:

  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  2. green open .geoip_databases pE4IpIAeSA2AiJzdDdviYA 1 0 42 64 65.1mb 65.1mb
  3. green open index_user 2z4cELBeQeijTagp86ShbQ 5 0 0 0 1kb 1kb

3. Elasticsearch對映的使用

對映(mapping)相當於是mysql中的表結構定義。

3.1 Elasticsearch中的主要資料型別

文字型別:text,keyword

整型:long,integer,short,byte

浮點型:double,float

布林型:boolean

日期型:date

物件型:object

3.2 建立索引並建立對映

RESTFUL介面方式

PUT  http://192.168.1.11:9200/index_user

引數:

  1. {
  2. "settings":{
  3. "index":{
  4. "number_of_shards":5,
  5. "number_of_replicas":0
  6. }
  7. },
  8. "mappings" : {
  9. "properties":{
  10. "name":{
  11. "type":"text", // 資料型別
  12. "index":true // 是否索引
  13. },
  14. "loginName":{
  15. "type":"keyword",
  16. "index":false
  17. },
  18. "age":{
  19. "type":"integer",
  20. "index":false
  21. }
  22. }
  23. }
  24. }

3.3 在已有的索引上維護mapping

RESTFUL介面方式

POST  http://192.168.1.11:9200/index_user/_mapping

引數:

  1. {
  2. "properties":{
  3. "nickname":{
  4. "type":"keyword",
  5. "index":false
  6. }
  7. }
  8. }

 注意:mapping中的屬性,只能新增,不能修改。如果屬性設定需要變更,需要刪除索引重建。

3.4 檢視索引的分詞效果 

RESTFUL介面方式

GET  http://192.168.1.11:9200/index_user/_analyze

引數:

  1. {
  2. "field": "name",
  3. "text": "lisa brown"
  4. }

4. Elasticsearch文件的使用

文件(document)相當於是mysql中的資料行。

 4.1 新增文件

RESTFUL介面方式

POST  http://192.168.1.11:9200/index_user/_doc/1

注:url中最後的1是文件在Elasticsearch中的ID,與業務ID無關,如果不寫,則會自動生成一個隨機字串作為文件的ID

引數:

  1. {
  2. "name":"zhang san",
  3. "loginName":"zs",
  4. "age":30
  5. }

如果沒有手動建立 對映(mapping),則新增文件後,Elasticsearch會根據文件的欄位型別自動建立 對映(mapping)。

4.2 刪除文件

RESTFUL介面方式

DELETE  http://192.168.1.11:9200/index_user/_doc/1

4.3 修改文件

RESTFUL介面方式

1)只修改部分欄位

POST  http://192.168.1.11:9200/index_user/_doc/1/_update

引數:

  1. {
  2. "doc":{
  3. "name":"zhangsan2",
  4. "age":33
  5. }
  6. }

2)全部替換

PUT  http://192.168.1.11:9200/index_user/_doc/1

引數:

  1. {
  2. "name":"zhangsan",
  3. "loginName":"zs",
  4. "age":31
  5. }

4.4 查詢文件

RESTFUL介面方式

1)依據文件ID查詢

GET  http://192.168.1.11:9200/index_user/_doc/1

響應資料:

  1. {
  2. "_index": "index_user",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 5,
  6. "_seq_no": 7,
  7. "_primary_term": 1,
  8. "found": true,
  9. "_source": {
  10. "name": "zhangsan",
  11. "loginName": "zs",
  12. "age": 31
  13. }
  14. }

2)查詢所有

GET  http://192.168.1.11:9200/index_user/_doc/_search

響應資料:

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "index_user",
  19. "_type": "_doc",
  20. "_id": "_TVW-XsBNDgg-BBCeUvY",
  21. "_score": 1.0,
  22. "_source": {
  23. "name": "lisi",
  24. "loginName": "ls",
  25. "age": 31
  26. }
  27. },
  28. {
  29. "_index": "index_user",
  30. "_type": "_doc",
  31. "_id": "1",
  32. "_score": 1.0,
  33. "_source": {
  34. "name": "zhangsan",
  35. "loginName": "zs",
  36. "age": 31
  37. }
  38. }
  39. ]
  40. }
  41. }

3)查詢時自定義結果集

GET  http://192.168.1.11:9200/index_user/_doc/1?_source=name,age

GET  http://192.168.1.11:9200/index_user/_doc/_search?_source=name,age

5. 綜述

今天簡單聊了一下 Elasticsearch 的基本使用,希望能對大家的工作有所幫助。

歡迎大家幫忙點贊、評論、加關注 :)

關注追風人聊Java,每天更新Java乾貨。