1. 學習查詢的api語法
首先向es中titles寫入兩個文檔
POST titles/_doc/1
{
"title": "Quick brown rabbits",
"body": "Brown rabbits are commonly seen."
}
POST titles/_doc/2
{
"title": "Keeping pets healthy",
"body": "My quick brown fox eats rabbits on a regular basis."
}
2 . 查詢 tille 和body中 全文檢索 quick brown的內容,語法如下
1. 首先在寫入文檔的時候會先對寫入的內容進行分詞,分詞后寫入倒排索引,查詢的時候也會對查詢內容進行分詞,分詞后和倒排索引進行匹配,進行相關度查詢
POST titles/_search { "query": { "bool": { "should": [ {"match": {"title": "quick brown"}}, {"match": {"body": "quick brown"}} ] } } }
結果如下
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 2.1213155, "hits" : [ { "_index" : "titles", "_type" : "_doc", "_id" : "1", "_score" : 2.1213155, "_source" : { "title" : "Quick brown rabbits", "body" : "Brown rabbits are commonly seen." } }, { "_index" : "titles", "_type" : "_doc", "_id" : "2", "_score" : 0.55788946, "_source" : { "title" : "Keeping pets healthy", "body" : "My quick brown fox eats rabbits on a regular basis." } } ] } }
可以看到查詢結果, 匹配到兩個文檔,文檔1的評分比文檔2的評分要高,直觀感受應該是文檔2的評分更匹配,這是因為should 查詢評分是疊加的關系,會對body 和titile 中查詢字段評分進行疊加 1,中body和title 中都有brown
2 中的titile沒有匹配,雖然body內容更加匹配。 有時候我們需要查詢某個字段更加匹配展示給用戶,可以使用dis_max 查詢
POST titles/_search { "query": { "multi_match": { "query": "brown fox", "fields": ["title","body"], "type": "best_fields", "tie_breaker": 0.2 } } }
說明下 ,tile_breaker 是對除了最匹配的字段外,提升其他字段的權重得分.默認是0 也就是除了最匹配外,其他字段都忽略。
查詢結果如下,可以看到文檔2的評分比文檔1高,查詢也可以修改字段的權重,title^10 標識把title查詢的權重提升10倍。 默認title和body的權重是一樣的。 這個可以根據實際業務來看
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.77041256, "hits" : [ { "_index" : "titles", "_type" : "_doc", "_id" : "2", "_score" : 0.77041256, "_source" : { "title" : "Keeping pets healthy", "body" : "My quick brown fox eats rabbits on a regular basis." } }, { "_index" : "titles", "_type" : "_doc", "_id" : "1", "_score" : 0.735369, "_source" : { "title" : "Quick brown rabbits", "body" : "Brown rabbits are commonly seen." } } ] } }
參考: Elasticsearch核心技術與實戰