版本:5.6.8
一、创建索引index和映射mapping

PUT http://localhost:9200/blog1 { "mappings": { "article": { "properties": { "id": { "type": "long", "store": true, "index": "not_analyzed" }, "title": { "type": "text", "store": true, "index": "analyzed", "analyzer": "standard" }, "content": { "type": "text", "store": true, "index": "analyzed", "analyzer": "standard" } } } } }
二、先索引后映射

PUT http://localhost:9200/blog2 POST http://localhost:9200/blog2/article/_mapping { "article": { "properties": { "id": { "type": "keyword" }, "title": { "type": "text", "analyzer": "standard" }, "content": { "type": "text", "analyzer": "standard" } } } }
三、删除索引
DELETE http://localhost:9200/blog1
四、创建/修改文档(根据id)

POST http://localhost:9200/blog1/article/1 { "id": 1, "title": "es", "content": "es是一个分布式多用户能力的全文搜索引擎,基于RESTful web接口,ES是用Java开发的,能够达到实时搜索,稳定可靠。快速安装使用方便。" }
五、删除文档
DELETE http://localhost:9200/blog1/article/1
六、根据id查询文档
GET http://localhost:9200/blog1/article/1
七、根据querystring查询
默认支持分词搜索,所以搜索 "分布式" 或 "分布公式" 都可以搜到这条记录。

POST http://localhost:9200/blog1/article/_search { "query":{ "query_string":{ "default_field": "content", "query":"分布式" } } }
八、term搜索(精确匹配)
不会做分词搜索,这时用 "分布公式" 就搜索不到了。使用 "分布式" 也搜索不到,需要自定义分词策略。

POST http://localhost:9200/blog1/article/_search { "query":{ "term":{ "content":"分布" } } }
九、查看标准分词
可以看到分词结果把每个中文分开了,而 content 的内容中又不存在空格隔开的中文关键字,所以搜索不到。

GET http://localhost:9200/_analyze?analyzer=standard&pretty=true&text=分布公式 结果: { "tokens": [ { "token": "分", "start_offset": 0, "end_offset": 1, "type": "<IDEOGRAPHIC>", "position": 0 }, { "token": "布", "start_offset": 1, "end_offset": 2, "type": "<IDEOGRAPHIC>", "position": 1 }, { "token": "公", "start_offset": 2, "end_offset": 3, "type": "<IDEOGRAPHIC>", "position": 2 }, { "token": "式", "start_offset": 3, "end_offset": 4, "type": "<IDEOGRAPHIC>", "position": 3 } ] }