弹性搜索必须不被忽略

时间:2016-02-26 11:30:07

标签: elasticsearch

我有一个弹性搜索查询对我来说似乎忽略了“must_not”部分(使用sense来测试/调试)

POST /myserver.dev/indexedproduct/_search
{
      "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "isPublished": true
              }
            },
            {
              "term": {
                "isProjectPublished": true
              }
            },
            {
              "term": {
                "hidden": false
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "term": {
                      "excludedMarkets": ["GI"]
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "category": [
                        "headwear"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

这将返回104个结果。

如果我跑:

POST /myserver.dev/indexedproduct/_search
{
    "query": { 
        "bool": {
            "must_not": { "match": { "excludedMarkets":   "GI"}}
        }
    }
} 

然后返回41个结果。

由于“must_not”子句,我希望第一个(不工作的)返回41或更少的结果。

我查看了ES文档,我的查询看起来是根据过滤的查询和嵌套的must / must_not语句正确形成的。

任何帮助都将不胜感激。

编辑,测试数据,只有2,一个与GI exludedMarkerts,一个没有,意味着最终结果应该只返回一个,

"_index":"myindex.dev",
"_type":"indexedproduct",
"_id":"29426",
"_score":1,
"_source":{  
   "id":29426,
   "sku":"0123",
   "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ne.",
   "searchKeywords":[  
      "Lorem",
      "ipsum",
      "dolor"
   ],
   "productType":[  
      "Ipsum"
   ],
   "category":[  
      "Lorem"
   ],
   "colour":[  
      "Black/Black"
   ],
   "prices":{  
      "eur":35,
      "gbp":28
   },
   "catalogId":3,
   "ageRange":[  
      "adults"
   ],
   "brand":[  
      null
   ],
   "available":true,
   "bestSeller":false,
   "collections":[  
      "lorumipsum"
   ],
   "fit":[  
      "fitted"
   ],
   "newArrival":false,
   "style":[  
      "Lorum"
   ],
   "excludedMarkets":[  
      "BA",
      "GI",
      "MC",
      "MD",
      "SM",
      "AL"
   ],
   "isPublished":true,
   "isTranslated":false,
   "isProjectPublished":true,
   "hidden":false,
   "availableDate":"2015-09-17T01:00:00+01:00"
}
},
"_index":"myindex.dev",
"_type":"indexedproduct",
"_id":"2942",
"_score":1,
"_source":{  
   "id":2942,
   "sku":"012",
   "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ne.",
   "searchKeywords":[  
      "Lorem",
      "ipsum",
      "dolor"
   ],
   "productType":[  
      "IpsumLorem"
   ],
   "category":[  
      "IpsumLorem"
   ],
   "colour":[  
      "Black/Blue"
   ],
   "prices":{  
      "eur":35,
      "gbp":28
   },
   "catalogId":3,
   "ageRange":[  
      "adults"
   ],
   "brand":[  
      null
   ],
   "available":true,
   "bestSeller":false,
   "collections":[  
      "lorumipsum"
   ],
   "fit":[  
      "fitted"
   ],
   "newArrival":false,
   "style":[  
      "Lorum"
   ],
   "excludedMarkets":[  
      "BA",
      "MC",
      "MD",
      "SM",
      "AL"
   ],
   "isPublished":true,
   "isTranslated":false,
   "isProjectPublished":true,
   "hidden":false,
   "availableDate":"2015-09-17T01:00:00+01:00"
}
}

2 个答案:

答案 0 :(得分:0)

您应该在同一级别拥有must / must_notshould条款。此查询应该有效:

POST /myserver.dev/indexedproduct/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            { "term": { "isPublished": true } },
            { "term": { "isProjectPublished": true } },
            { "term": { "hidden": false } }
          ],
          "must_not": [
            { "term": { "excludedMarkets": ["GI"] } }
          ],
          "should": [
            { "terms": { "category": ["headwear"] } }
          ]
        }
      }
    }
  }
}

答案 1 :(得分:0)

我希望这会奏效:试试这个

    POST /myserver.dev/indexedproduct/_search
    {
        "query": {
         "filtered": {
           "filter": {
            "bool": {
              "must": [{"term": {"isPublished": true}},
                       {"term": {"isProjectPublished": true}},
                       {"term": {"hidden": false}},
                       {"bool": {
                          "must_not": [{"terms": {"excludedMarkets": ["GI"]}}]
                         }
                    },
                    {"bool": { "should": [{"terms": {"category": ["headwear"]}}]}}
             ]
            }
          }
        }
      }
    }

我正在使用elasticsearch 2.1,当我使用 term 而不是 terms 时,它会给我query_parsing_exception。