前面几篇文章介绍了搜索引擎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 } ] }