将查询与must和should结合使用以进行多匹配查询

时间:2016-06-09 10:41:17

标签: elasticsearch

我正在尝试使用多匹配查询,如下所示。对于某些字段,我希望在字段中找到所有关键字,但对于某些其他字段,应找到至少1个关键字。根据我的想法,我想用must和should组合2个多匹配查询。

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

如果我理解正确MPN和SKU必须只匹配hp和301时,两者都在字段中找到,但是当我分析得分时,我会在下面看到。

产品有: “MPN”:“CF 301 A”

对它的解释:

 "_explanation": {
               "value": 2.0636692,
               "description": "sum of:",
               "details": [
                  {
                     "value": 1.0522164,
                     "description": "max of:",
                     "details": [
                        {
                           "value": 1.0522164,
                           "description": "product of:",
                           "details": [
                              {
                                 "value": 2.1044328,
                                 "description": "sum of:",
                                 "details": [
                                    {
                                       "value": 2.1044328,
                                       "description": "weight(MPN:301 in 375) [PerFieldSimilarity], result of:",
                                       "details": [
                                          {
                                             "value": 2.1044328,
                                             "description": "score(doc=375,freq=1.0), product of:",
                                             "details": [
                                                {
                                                   "value": 0.45506343,
                                                   "description": "queryWeight, product of:",
                                                   "details": [
                                                      {
                                                         "value": 9,
                                                         "description": "boost",
                                                         "details": []
                                                      },
                                                      {
                                                         "value": 9.248965,
                                                         "description": "idf(docFreq=2, maxDocs=11471)",
                                                         "details": []
                                                      },
                                                      {
                                                         "value": 0.0054668393,
                                                         "description": "queryNorm",
                                                         "details": []
                                                      }
                                                   ]
                                                },

如果只有301在MPN字段中,为什么MPN会返回分数?它是否也不需要“hp”存在于该领域?如果没有,我怎样才能使它完全匹配?

1 个答案:

答案 0 :(得分:1)

使用多匹配查询时,搜索项默认被解析为单独的单词,并且如果这些单词中的任何一个与整个匹配的查询匹配。要打破此默认行为,您需要做的是添加'和'运算符,因此它知道只有在找到两个项时才匹配。

"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",
                     "Description^6"
                     ]
            }}
         ]
      }
   }

希望这有帮助!