前面幾篇文章介紹了搜索引擎ElasticSearch的內部原理,這篇文章總結了在ElasticSearch使用過程當中常見的用法。node
ElasticSearch 查看集羣健康信息,經常使用命令以下:git
ElasticSearch查看集羣狀態命令:github
curl 'localhost:9200/_cat/health?v'
算法
其中,status爲綠色表示一切正常, 黃色表示全部的數據可用可是部分副本尚未分配,紅色表示部分數據由於某些緣由不可用.
ElasticSearch查看索引分片狀況命令:json
curl 'localhost:9200/_cat/shards?v'
數據結構
能夠看到例子中有兩個索引分別是:people和product,他們各有一個主分片和一個還沒有分配的副分片。
ElasticSearch查看節點信息命令:app
curl 'localhost:9200/_cat/master?v'
curl 'localhost:9200/_cat/nodes?v'
ElasticSearch建立索引命令。curl
7.0版本去掉了type的概念。elasticsearch
curl -X PUT 'localhost:9200/product' -H 'Content-Type: application/json' -d ' { "mappings": { "type_name": { "properties": { "price": { "type": "integer" }, "name": { "type": "text" } } } } }'
curl -X PUT 'localhost:9200/product' -H 'Content-Type: application/json' -d ' { "mappings": { "properties": { "price": { "type": "integer" }, "name": { "type": "text" } } } }'
ElasticSearch查詢全部的索引命令。搜索引擎
curl 'localhost:9200/_cat/indices?v'
能夠看到上面的例子中,集羣中有兩個索引。他們的名字分別爲:people、product。且分別有一個主分片和副分片。
ElasticSearch查詢索引mapping結構命令:
curl 'localhost:9200/_mapping?pretty'
上面的例子查詢了索引people和product的mapping數據結構。
查看索引名爲indexName且type名爲typeName的索引mapping數據結構:
curl -XGET "127.0.0.1:9200/indexName/typeName/_mapping?pretty"
查看索引名爲indexName的索引mapping數據結構:
curl -XGET "127.0.0.1:9200/indexName/_mapping?pretty"
ElasticSearch在索引中添加字段命令
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字段
curl -XPOST "127.0.0.1:9200/product/_mapping?pretty" -H 'Content-Type: application/json' -d '{ "properties": { "tags":{ "type":"text" } } }'
上面的例子,表示給索引名爲product的mapping數據結構添加tags字段
ElasticSearch如今不支持
ElasticSearch 刪除某個索引命令。
curl -X DELETE 'localhost:9200/product'
上面的例子表示刪除名爲product的索引。
下面提到的數據即索引中的文檔。
ElasticSearch寫入數據命令.
7.0版本去掉了type的概念。
curl -X PUT 'localhost:9200/product/type_name/1' -H 'Content-Type: application/json' -d ' { "price":1, "name":"富士山蘋果" }'
curl -X PUT 'localhost:9200/product/_doc/1' -H 'Content-Type: application/json' -d ' { "price":1, "name":"富士山蘋果" }'
ElasticSearch搜索數據命令
curl -X GET 'localhost:9200/product/type_name/1?pretty'
curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d ' { "query": { "match": { "name": "蘋果" } }, "from":0, "size":1 }'
curl -X GET 'localhost:9200/product/_doc/1?pretty'
curl -X GET 'localhost:9200/product/_search?pretty' -H 'Content-Type: application/json' -d ' { "query": { "match": { "name": "蘋果" } }, "from":0, "size":1 }'
查詢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 }'
查詢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 }'
ElasticSearch中更新數據命令:
curl -X PUT 'localhost:9200/product/_doc/1' -H 'Content-Type: application/json' -d ' { "price":1, "name":"富士山蘋果", "tags":["富士山","名牌","蘋果"] }'
ElasticSearch中刪除文檔數據命令:
刪除一個文檔
curl -XDELETE 'http://localhost:9200/indexName/typeName/1'
上面的例子表示刪除索引名爲indexName且type名爲typeName的索引中文檔ID爲1的文檔。
curl -XDELETE 'http://localhost:9200/indexName/_doc/1'
ElasticSearch支持不一樣的分詞插件,在下面的例子中咱們使用了analysis-ik分詞插件。
經過ElasticSearch的API接口,能夠分析不一樣分詞器的分詞結果1。具體的步驟以下:
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" } } }'
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 } ] }
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 } ] }