Elasticsearch系列七:常見用法手冊

前面幾篇文章介紹了搜索引擎ElasticSearch的內部原理,這篇文章總結了在ElasticSearch使用過程當中常見的用法。node

一、查看集羣信息

ElasticSearch 查看集羣健康信息,經常使用命令以下:git

1.一、查看集羣狀態

ElasticSearch查看集羣狀態命令:github

curl 'localhost:9200/_cat/health?v'算法

其中,status爲綠色表示一切正常, 黃色表示全部的數據可用可是部分副本尚未分配,紅色表示部分數據由於某些緣由不可用.

1.二、查看索引分片狀況

ElasticSearch查看索引分片狀況命令:json

curl 'localhost:9200/_cat/shards?v'數據結構

能夠看到例子中有兩個索引分別是:people和product,他們各有一個主分片和一個還沒有分配的副分片。

1.三、查看節點信息

ElasticSearch查看節點信息命令:app

  • 主節點:curl 'localhost:9200/_cat/master?v'
  • 全部節點:curl 'localhost:9200/_cat/nodes?v'

二、索引相關命令

2.一、建立索引

ElasticSearch建立索引命令。curl

7.0版本去掉了type的概念。elasticsearch

2.1.一、7.0以前版本建立索引

curl -X PUT 'localhost:9200/product' -H 'Content-Type: application/json' -d '
{
  "mappings": {
    "type_name": {
      "properties": {
        "price": {
          "type": "integer"
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}'

2.1.二、7.0及其以後的版本建立索引

curl -X PUT 'localhost:9200/product' -H 'Content-Type: application/json' -d '
{
  "mappings": {
      "properties": {
        "price": {
          "type": "integer"
        },
        "name": {
          "type": "text"
        }
      }
    }
}'

2.二、查詢索引

ElasticSearch查詢全部的索引命令。搜索引擎

curl 'localhost:9200/_cat/indices?v'

能夠看到上面的例子中,集羣中有兩個索引。他們的名字分別爲:people、product。且分別有一個主分片和副分片。

2.三、查詢索引結構

2.3.一、查看全部的索引mapping數據結構

ElasticSearch查詢索引mapping結構命令:

curl 'localhost:9200/_mapping?pretty'

上面的例子查詢了索引people和product的mapping數據結構。

2.3.二、查看指定的索引mapping數據結構

2.3.2.一、7.0以前版本

查看索引名爲indexName且type名爲typeName的索引mapping數據結構:

curl -XGET "127.0.0.1:9200/indexName/typeName/_mapping?pretty"

2.3.2.二、7.0及其以後版本

查看索引名爲indexName的索引mapping數據結構:

curl -XGET "127.0.0.1:9200/indexName/_mapping?pretty"

2.四、給索引添加字段

ElasticSearch在索引中添加字段命令

2.4.一、7.0以前版本

curl -XPOST "127.0.0.1:9200/indexName/typeName/_mapping?pretty" -H 'Content-Type: application/json' -d '{
 "typeName": {
            "properties": {
                 "tags":{
                    "type":"text"
               }
            }
        }
}'
上面的例子,表示給索引名爲indexName且type名爲typeName的mapping數據結構添加tags字段

2.4.二、7.0及其以後版本

curl -XPOST "127.0.0.1:9200/product/_mapping?pretty" -H 'Content-Type: application/json' -d '{
    "properties": {
         "tags":{
            "type":"text"
       }
    }
}'
上面的例子,表示給索引名爲product的mapping數據結構添加tags字段

2.五、索引中刪除字段

ElasticSearch如今不支持

2.六、刪除索引

ElasticSearch 刪除某個索引命令。

curl -X DELETE 'localhost:9200/product'

上面的例子表示刪除名爲product的索引。

三、數據文檔搜索查詢相關命令

下面提到的數據即索引中的文檔。

3.一、寫入數據命令

ElasticSearch寫入數據命令.

7.0版本去掉了type的概念。

3.1.一、7.0以前版本寫入數據

curl -X PUT 'localhost:9200/product/type_name/1' -H 'Content-Type: application/json' -d '
{
    "price":1,
    "name":"富士山蘋果"
}'

3.1.二、7.0及其以後的版本寫入數據

curl -X PUT 'localhost:9200/product/_doc/1' -H 'Content-Type: application/json' -d '
{
    "price":1,
    "name":"富士山蘋果"
}'

3.二、搜索查詢數據命令

ElasticSearch搜索數據命令

3.2.一、7.0以前版本搜索數據

3.2.1.一、主鍵搜索

curl -X GET 'localhost:9200/product/type_name/1?pretty'

3.2.1.二、關鍵字搜索
curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '
{
  "query": {
    "match": {
      "name": "蘋果"
    }
  },
  "from":0,
  "size":1
}'

3.2.二、7.0及其以後的版本搜索數據

3.2.2.一、主鍵搜索

curl -X GET 'localhost:9200/product/_doc/1?pretty'

3.2.2.二、關鍵字搜索
curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '
{
  "query": {
    "match": {
      "name": "蘋果"
    }
  },
  "from":0,
  "size":1
}'

3.2.三、對多值的字段搜索

查詢name包含「蘋果」且tags有「蘋果」或者「富士山」的文檔數據:

curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query":{
        "bool":{
            "must":[
                {
                    "bool":{
                        "should":[
                            {"match":{"name":"蘋果"}},
                            {"terms": {"tags": ["蘋果","富士山"]}}
                        ]
                    }
                }
            ]
        }
    },
    "sort":{
        "_score":{"order":"desc"}
    },
    "from":0,
    "size":10
}'

3.2.四、多字段聯合查詢

查詢name包含「蘋果」且price爲1的文檔數據:

curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query":{
        "bool":{
            "must":[
                {
                    "bool":{
                        "should":[
                            {"match":{"name":"蘋果"}},
                            {"match":{"price":1}}
                        ]
                    }
                }
            ]
        }
    },
    "sort":{
        "_score":{"order":"desc"}
    },
    "from":0,
    "size":10
}'

3.三、更新數據命令

ElasticSearch中更新數據命令:

curl -X PUT 'localhost:9200/product/_doc/1' -H 'Content-Type: application/json' -d '
{
    "price":1,
    "name":"富士山蘋果",
    "tags":["富士山","名牌","蘋果"]
}'

3.四、刪除數據

ElasticSearch中刪除文檔數據命令:

3.4.一、7.0以前版本

刪除一個文檔

curl -XDELETE 'http://localhost:9200/indexName/typeName/1'

上面的例子表示刪除索引名爲indexName且type名爲typeName的索引中文檔ID爲1的文檔。

3.4.二、7.0及其以後的版本

curl -XDELETE 'http://localhost:9200/indexName/_doc/1'

四、ElasticSearch中分詞器分析器在命令行中的用法

ElasticSearch支持不一樣的分詞插件,在下面的例子中咱們使用了analysis-ik分詞插件。

經過ElasticSearch的API接口,能夠分析不一樣分詞器的分詞結果1。具體的步驟以下:

4.一、添加兩個字符類型的字段,並指定不一樣的分詞器:

curl -XPOST "127.0.0.1:9200/product/_mapping?pretty" -H 'Content-Type: application/json' -d '{
    "properties": {
        "pNameIkMaxWord":{
            "type":"text",
            "analyzer":"ik_max_word"
        },
        "pNameIkSmart":{
            "type":"text",
            "analyzer":"ik_smart"
        }
    }
}'

4.二、使用ik_max_word分詞分析

curl -XPOST 'http://localhost:9200/product/_analyze?pretty' -H 'Content-Type: application/json' -d '
{
    "field": "pNameIkMaxWord",
    "text": "中華人民共和國國歌"
}'

分詞結果以下:

{
  "tokens" : [
    {
      "token" : "中華人民共和國",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "中華人民",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "中華",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "華人",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "人民共和國",
      "start_offset" : 2,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    },
    {
      "token" : "人民",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "共和國",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 6
    },
    {
      "token" : "共和",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 7
    },
    {
      "token" : "國",
      "start_offset" : 6,
      "end_offset" : 7,
      "type" : "CN_CHAR",
      "position" : 8
    },
    {
      "token" : "國歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 9
    }
  ]
}

4.三、使用ik_smart分詞分析

curl -XPOST 'http://localhost:9200/product/_analyze?pretty' -H 'Content-Type: application/json' -d '
{
    "field": "pNameIkSmart",
    "text": "中華人民共和國國歌"
}'

分詞結果以下:

{
  "tokens" : [
    {
      "token" : "中華人民共和國",
      "start_offset" : 0,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "國歌",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

系列文章

  1. ElasticSearch系列一:源碼編譯和Debug環境搭建
  2. ElasticSearch系列二:啓動過程詳解
  3. Elasticsearch系列三:建立索引過程詳解
  4. Elasticsearch系列四:搜索過程詳解
  5. Elasticsearch系列五:搜索相關性排序算法詳解
  6. Elasticsearch系列六:ES中的倒排索引
  7. Elasticsearch系列七:常見用法手冊

  1. https://elasticsearch.cn/arti...
相關文章
相關標籤/搜索