我有一組我需要搜索的 search_as_you_type_fields。這是我的映射
"mappings" : {
"properties" : {
"description" : {
"type" : "search_as_you_type",
"doc_values" : false,
"max_shingle_size" : 3
},
"questions" : {
"properties" : {
"content" : {
"type" : "search_as_you_type",
"doc_values" : false,
"max_shingle_size" : 3
},
"tags" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
}
},
"title" : {
"type" : "search_as_you_type",
"doc_values" : false,
"max_shingle_size" : 3
},
}
}
我正在使用 bool_prefix 型別的 multi_match 查詢。
"query": {
"multi_match": {
"query": "triangle",
"type": "bool_prefix",
"fields": [
"title",
"title._2gram",
"title._3gram",
"description",
"description._2gram",
"description._3gram",
"questions.content",
"questions.content._2gram",
"questions.content._3gram",
"questions.tags",
"questions.tags._2gram",
"questions.tags._3gram"
]
}
}
到目前為止作業正常。現在我想添加一個在 ES 中是模糊的錯字容忍度。但是,看起來 bool_prefix 與此有一些沖突。因此,如果我修改查詢并添加 "fuzziness": "AUTO" 并在單詞 "triangle" -> "triangld" 中出錯,它將不會得到任何結果。
但是,如果我正在尋找一個短語“直角三角形”,我會有一些不同的行為:
- 即使沒有打錯字,我也能通過“模糊”得到更多結果:“AUTO”(1759 vs 1267)
- 如果我在二維單詞“right triangdd”中添加一個錯字,它似乎可以作業,但是看起來它現在首先推送包含“right”而不是“triangle”的結果(“權利法案”、“正當程式和權利”)隱私”等)在前面。
- 如果我在第一個單詞(“直角三角形”)或兩者(“右三角”)中打錯字,結果似乎很好。所以這可能是唯一正確的行為。
我已經看過幾篇文章,甚至 GitHub 問題,使用 bool_prefix 進行 multi_match 查詢時,模糊性無法正常作業,但是我找不到解決方法。我已經嘗試更改查詢型別,但看起來 bool_prefix 是唯一一種支持在您鍵入時進行搜索的型別,我需要在用戶開始鍵入內容時獲取搜索結果。
Since I make all the requests from ES from our backend What I also can do is manipulate a query string to build different search query types if needed. For example, for 1 word searches use one type for multi use another. But I basically need to maintain current behavior.
I've also tried appending a sign "~" or "~1[2]" to the string which seems to be another way of specifying the fuzziness, but the results are rather unclear and performance (search speed) seems to be worse.
My questions are:
- How can I achieve fuzziness for 1 word searches? so that query "triangld" returns documents containing "triangle" etc.
- How can I achieve correct search results when the typo in the 2d (last?) word of the query? Like I mentioned above it works, but see the point 2 above
- 即使短語正確,為什么只添加模糊性(參見第 1 頁)會回傳更多結果?
- 我需要在我的分析儀等中改變什么?
uj5u.com熱心網友回復:
因此,為了實作所需的行為,我們執行了以下操作:
- 將查詢型別更改為“query_string”
- 在后端添加了查詢字串預處理。我們用空格分割查詢字串,如果它們的長度分別超過 4 個或 8 個字符,則為每個單詞添加“~1”或“~2”。~ 是 ES 中的模糊語法。但是,在用戶鍵入空格之前,我們不會將其添加到當前鍵入的單詞中。例如,用戶輸入 [t, tr, tri, ... triangle] => 沒有模糊,但一次“triangle”=>“triangle~2”。這是因為最后一個詞模糊會出現意想不到的結果
- 我們還從搜索欄位中洗掉了所有 ngram 欄位,因為我們得到了相同的結果,但性能要好一些。
- 將 "default_operator": "AND" 添加到查詢以包含來自短語查詢的一個欄位的結果
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377955.html
