如何结合多个匹配查询?

时间:2016-08-11 11:02:12

标签: elasticsearch

我的目的是结合2个多重匹配查询,如下所示。我希望MPN或SKU字段必须匹配每个关键字。关键词" hp"和" 301"两者都必须存在于MPN或SKU中。 或者Name或Name.raw应该有一个关键字" hp"或" 301"。 MPN / SKU的精确匹配应该有更高的分数。 我写了如下查询,但它没有返回任何结果,虽然有文件有这些条件。 我的问题是,

1) Does must query require keyword to be found in both SKU and MPN
for the multi_match query?

2) Combination of must and should is AND or OR? it looks like AND
for me as it doesn't return any result. if it is AND, how to make it
as OR? I cant find any operator on bool. I tried to wrap in another
should query but it didnt help.

我也非常感谢任何查询建议。

   "from": 0,
       "size": 10,
       "explain": true,
       "query": {
          "bool": {
             "must": [
                {
                   "multi_match": {
                      "type": "best_fields",
                      "query": "hp 301",
                      "fields": [                       
                         "MPN^9",
                         "SKU^8"             
                      ],
              "operator": "and"
                   }
                }
             ],
             "should": [
                {"multi_match": {
                      "type": "best_fields",
                   "query": "hp 301",
                   "fields": [
                        "Name.raw2^7.5",
                         "Name^7"
                         ]
                }}
             ]
          }
       }

1 个答案:

答案 0 :(得分:0)

我假设这个问题与此问题有关。 How do && and || work constructing queries in NEST?

如果我理解正确,必须和应该的组合必须作为必须作为助推器,如下所述

  

bool
>     must 
>         term1
>     should
>         term2
>         term3
>         term4 

这是因为一旦布尔查询具有must子句,就应该开始充当提升因子。所以在

  

之前您可以获得仅包含term1的结果   显然不是你想要的严格布尔意义上的输入。

因此我将查询更改为2个查询,其中一个查询具有“AND”运算符。所以这个行为就像必须一样,因为它会强制所有搜索关键字都应该在字段中找到。

"from": 0,
       "size": 10,
       "explain": true,
       "query": {
          "bool": {
             "should": [
                {
                   "multi_match": {
                      "type": "best_fields",
                      "query": "hp 301",
                      "fields": [                       
                         "MPN^9",
                         "SKU^8"             
                      ],
              "operator": "and"
                   }
                }               ,
                {
                "multi_match": {
                      "type": "best_fields",
                   "query": "hp 301",
                   "fields": [
                        "Name.raw2^7.5",
                         "Name^7"
                         ]
                }
                }
             ]

          }
       }