最近在學習ElasticSearch官方文檔過程中發現的某個問題,記錄一下 希望能幫助到后面的朋友
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_finding_exact_values.html
先說結論:字段類型更改為 'keyword'
elasticSearch官方文檔中創建index代碼如下
PUT /my_store { "mappings" : { "products" : { "properties" : { "productID" : { "type" : "string", "index" : "not_analyzed" } } } } }
由於es官方文檔版本基於2.x編寫,而本人安裝版本為6.6 在執行如上代碼過程中出現如下錯誤
No handler for type [string] declared on field [productID]
這里報錯是因為ElasticSearch5.x以上版本沒有string類型了,換成了text和keyword作為字符串類型。
字符串 - text:用於全文索引,該類型的字段將通過分詞器進行分詞,最終用於構建索引
字符串 - keyword:不分詞,只能搜索該字段的完整的值,只用於 filtering
此時我們將文檔中代碼更改為如下
PUT /my_store { "mappings" : { "products" : { "properties" : { "productID" : { "type" : "keyword", "index": true } } } } }
創建成功,此時我們進行查詢試試看
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "term" : { "productID" : "XHDK-A-1293-#fJ3" } } } } }