我的查詢成功回傳了我正在尋找的確切結果。
{"size": 100,"from": 0, "query": {"bool": {"must": [{"bool":{"should":[{"match":{"ProcessId":"from-cn"}}]}}]}}}
這僅回傳 ProcessId "from-cn" 的專案但是,當我添加這樣的排序查詢時:
{"size": 100,"from": 0,"sort": [{"CreatedTimeStamp": {"order": "desc"}}], "query": {"bool": {"must": [{"bool":{"should":[{"match":{"ProcessId":"from-cn"}}]}}]}}}
這現在回傳所有“from-cn”,但它還回傳其他幾個沒有 ProcessId“from-cn”的結果。
我知道這是導致問題的排序,因為當我洗掉排序時,它會完美地回傳。
為什么這里會發生這種情況?我該如何解決?
uj5u.com熱心網友回復:
試試這個查詢。它產生什么?
{
"size": 100,
"from": 0,
"sort": [
{
"CreatedTimeStamp": {
"order": "desc"
}
}
],
"query": {
"bool": {
"filter": [
{
"match": {
"ProcessId": "from-cn"
}
}
]
}
}
}
uj5u.com熱心網友回復:
match query 執行全文搜索。
這意味著它會分析提供的文本,生成在對檔案欄位進行實際匹配時將使用的標記。
除非您為ProcessId欄位定義了自定義搜索分析器,否則Elasticsearch 將在此處使用標準分析器。
您可以"from-cn"使用Analyze API驗證它為文本生成的標記,在這種情況下:
POST http://localhost:9200/_analyze
{
"analyzer" : "standard",
"text" : "from-cn"
}
回應:
{
"tokens": [
{
"token": "from",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "cn",
"start_offset": 5,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
}
]
}
你可以看到它產生了兩個標記:"from"和"cn"。因此,只有其中之一的檔案也將匹配查詢。在您的情況下,我相信它們只是從您請求的前 100 個結果中掉出來,因此在沒有自定義排序的情況下進行搜索時您看不到它們。
當您不使用自定義排序時,檔案按分數排序,與查詢更相關的檔案在串列中排名靠前。在您的情況下,匹配兩個標記的檔案將比僅匹配一個標記的檔案得分更高。但是通過自定義排序,您不再依賴分數,因此相關性較低的檔案可能會更高。
解決方案:
如果您想完全匹配欄位的內容,請在您的映射中將該欄位定義為未分析(例如使用keywordtype 而不是text)并使用不分析提供的文本的term查詢(例如query 而不是match)。
重新創建索引,ProcessId欄位為keyword。
POST http://localhost:9200/my-index
{
"mappings": {
"properties": {
"ProcessId": {
"type": "keyword"
},
... other fields
}
}
}
重新索引資料后,使用該欄位進行term查詢搜索。
{
"size": 100,
"from": 0,
"sort": [
{
"CreatedTimeStamp": {
"order": "desc"
}
}
],
"query": {
"term": {
"ProcessId": "from-cn"
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320928.html
標籤:弹性搜索
