2016-11-23 104 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

谢谢你的想法!看起来不错。我不想改变索引的映射。 –