Elasticsearch(ES)的基本使用

1. 概述

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

2. Elasticsearch索引的使用

索引(index)相當(dāng)于是mysql中的表。

2.1 創(chuàng)建索引

1)Head插件方式

選擇 索引 頁簽,點(diǎn)擊【新建索引】按鈕,輸入索引名稱、分片數(shù)、副本數(shù),點(diǎn)擊【OK】

image

之所以集群健康值呈現(xiàn)黃色,是因?yàn)槟壳笆怯脝畏?wù)器跑的Elasticsearch,而副本是要存儲在不同的服務(wù)器上的,之后會聊一下 Elasticsearch 集群的搭建。

image

2)RESTFUL接口方式

PUT http://192.168.1.11:9200/index_user

參數(shù):

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

2.2 查看集群健康狀況

RESTFUL接口方式

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

響應(yīng):

{
    "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插件方式

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

image

2)RESTFUL接口方式

DELETE http://192.168.1.11:9200/index_user

2.4 查看集群整體信息

RESTFUL接口方式

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

響應(yīng):

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)相當(dāng)于是mysql中的表結(jié)構(gòu)定義。

3.1 Elasticsearch中的主要數(shù)據(jù)類型

文本類型:text,keyword

整型:long,integer,short,byte

浮點(diǎn)型:double,float

布爾型:boolean

日期型:date

對象型:object

3.2 創(chuàng)建索引并創(chuàng)建映射

RESTFUL接口方式

PUT http://192.168.1.11:9200/index_user

參數(shù):

{
    "settings":{
        "index":{
            "number_of_shards":5,
            "number_of_replicas":0
        }
    },
    "mappings" : {
        "properties":{
            "name":{
                "type":"text",    // 數(shù)據(jù)類型
                "index":true      // 是否索引
            },
            "loginName":{
                "type":"keyword",
                "index":false
            },
            "age":{
                "type":"integer",
                "index":false
            }
        }
    }
}

3.3 在已有的索引上維護(hù)mapping

RESTFUL接口方式

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

參數(shù):

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

注意:mapping中的屬性,只能添加,不能修改。如果屬性設(shè)置需要變更,需要刪除索引重建。

3.4 查看索引的分詞效果

RESTFUL接口方式

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

參數(shù):

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

4. Elasticsearch文檔的使用

文檔(document)相當(dāng)于是mysql中的數(shù)據(jù)行。

4.1 新增文檔

RESTFUL接口方式

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

注:url中最后的1是文檔在Elasticsearch中的ID,與業(yè)務(wù)ID無關(guān),如果不寫,則會自動生成一個隨機(jī)字符串作為文檔的ID

參數(shù):

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

如果沒有手動創(chuàng)建 映射(mapping),則新增文檔后,Elasticsearch會根據(jù)文檔的字段類型自動創(chuàng)建 映射(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

參數(shù):

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

2)全部替換

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

參數(shù):

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

4.4 查詢文檔

RESTFUL接口方式

1)依據(jù)文檔ID查詢

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

響應(yīng)數(shù)據(jù):

{
    "_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

響應(yīng)數(shù)據(jù):

{
    "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)查詢時自定義結(jié)果集

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 的基本使用,希望能對大家的工作有所幫助。

歡迎大家?guī)兔c(diǎn)贊、評論、加關(guān)注 :)

關(guān)注追風(fēng)人聊Java,每天更新Java干貨。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容