1
votes

My purpose is to combine 2 multi match queries as below. I want MPN or SKU fields must match for each keyword. So keywords "hp" and "301" both must be exist either in MPN or SKU. or either Name or Name.raw should have one of the keyword either "hp" or "301". exact match which are MPN/SKU should have higher score. I wrote query as below but it doesnt return any results although there are documents having these conditions. my questions are,

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.

I appreciate also any query suggestion for my purpose.

   "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

1 Answers

0
votes

I am assuming that problem is something related to this question here. How do && and || work constructing queries in NEST?

If I understand correctly, a combination of must and should are acting as must with should as a booster as he says below

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

This is because as soon as a boolean query has a must clause the should start acting as a boosting factor. So in the

previous you could get back results that ONLY contain term1 this is clearly not what you want in the strict boolean sense of the input.

Therefore I changed my query into 2 should queries with one of them having "AND" operator. So this one will behave like MUST as it will force all search keywords should be found in fields.

"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"
                         ]
                }
                }
             ]

          }
       }