版本: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 } ] }