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

引數:

{
"settings":{
"index":{
"number_of_shards":5, // 分片數
"number_of_replicas":0 // 副本數
}
}
}

 2.2 檢視叢集健康狀況

RESTFUL介面方式

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

響應:

{
"cluster_name": "zhuifengren-es",
"status": "yellow",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 6,
"active_shards": 6,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 5,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 54.54545454545454
}

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

響應:

health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases pE4IpIAeSA2AiJzdDdviYA 1 0 42 64 65.1mb 65.1mb
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

引數:

{
"settings":{
"index":{
"number_of_shards":5,
"number_of_replicas":0
}
},
"mappings" : {
"properties":{
"name":{
"type":"text", // 資料型別
"index":true // 是否索引
},
"loginName":{
"type":"keyword",
"index":false
},
"age":{
"type":"integer",
"index":false
}
}
}
}

3.3 在已有的索引上維護mapping

RESTFUL介面方式

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

引數:

{
"properties":{
"nickname":{
"type":"keyword",
"index":false
}
}
}

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

3.4 檢視索引的分詞效果 

RESTFUL介面方式

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

引數:

{
"field": "name",
"text": "lisa brown"
}

4. Elasticsearch文件的使用

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

 4.1 新增文件

RESTFUL介面方式

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

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

引數:

{
"name":"zhang san",
"loginName":"zs",
"age":30
}

如果沒有手動建立 對映(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

引數:

{
"doc":{
"name":"zhangsan2",
"age":33
}
}

2)全部替換

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

引數:

{
"name":"zhangsan",
"loginName":"zs",
"age":31
}

4.4 查詢文件

RESTFUL介面方式

1)依據文件ID查詢

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

響應資料:

{
"_index": "index_user",
"_type": "_doc",
"_id": "1",
"_version": 5,
"_seq_no": 7,
"_primary_term": 1,
"found": true,
"_source": {
"name": "zhangsan",
"loginName": "zs",
"age": 31
}
}

2)查詢所有

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

響應資料:

{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "index_user",
"_type": "_doc",
"_id": "_TVW-XsBNDgg-BBCeUvY",
"_score": 1.0,
"_source": {
"name": "lisi",
"loginName": "ls",
"age": 31
}
},
{
"_index": "index_user",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "zhangsan",
"loginName": "zs",
"age": 31
}
}
]
}
}

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乾貨。