2016-11-23 45 views
1

我正在對具有多個字段的文檔進行自由文本搜索。當我執行搜索時,我希望在任何標籤上都有完美匹配的文檔具有更高的評分。有什麼辦法可以從查詢中做到這一點?Elasticsearch查詢更喜歡多個字段上的部分匹配的完全匹配

例如,文件有兩個字段:label-alabel-b,當我執行以下多匹配查詢:

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "multi_match": { 
      "query": "apple", 
      "type": "most_fields", 
      "fields": [ 
       "label-a", 
       "label-b" 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

我得到下面的結果(僅相關部分):

"hits": [ 
    { 
    "_index": "salad", 
    "_type": "fruit", 
    "_id": "4", 
    "_score": 0.581694, 
    "_source": { 
     "label-a": "apple pie and pizza", 
     "label-b": "pineapple with apple juice" 
    } 
    }, 
    { 
    "_index": "salad", 
    "_type": "fruit", 
    "_id": "2", 
    "_score": 0.1519148, 
    "_source": { 
     "label-a": "grape", 
     "label-b": "apple" 
    } 
    }, 
    { 
    "_index": "salad", 
    "_type": "fruit", 
    "_id": "1", 
    "_score": 0.038978107, 
    "_source": { 
     "label-a": "apple apple apple apple apple apple apple apple apple apple apple apple", 
     "label-b": "raspberry" 
    } 
    }, 
    { 
    "_index": "salad", 
    "_type": "fruit", 
    "_id": "3", 
    "_score": 0.02250402, 
    "_source": { 
     "label-a": "apple pie and pizza", 
     "label-b": "raspberry" 
    } 
    } 
] 

我想要第二個文檔,其值爲label-a的值爲grape,而值爲apple的爲label-b,因爲我在搜索該值時得分最高蘋果,其中一個標籤具有該確切值。無論哪個標籤出現確切的術語,這應該工作。

回答

0

因爲Elasticsearch使用tf/idf模型進行評分,所以您會得到這些結果。嘗試在索引字段中指定「label-a」和「label-b」作爲未分析(原始)字段。然後像這樣重寫你的查詢:

{ 
    "query": { 
    "bool": { 
     "should": { 
      "match": { 
       "label-a.raw": { 
        "query": "apple", 
         "boost": 2 
         } 
        } 
       }, 
     "must": [ 
     { 
      "multi_match": { 
      "query": "apple", 
      "type": "most_fields", 
      "fields": [ 
       "label-a", 
       "label-b" 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

should子句將使用完全匹配來提升文檔,你可能會首先獲得它們。嘗試使用提升號碼並在運行前檢查設備。這只是和想法你可以做什麼

+0

謝謝你的想法!看起來不錯。我不想改變索引的映射。 –