2016-02-26 104 views
0

我有我似乎忽略了「must_not」部分(使用感測試/調試)的elasticsearch查詢彈性搜索must_not越來越忽略

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

回答

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

你好,感謝,仍然帶回很多(104點擊)。也許我需要查詢,然後再次查詢,所以must_not超出了過濾 – jaymarvels

+0

@ jj72uk也許你應該在'must_not'子句中使用'terms'而不是'term'? –

+0

我也曾嘗試過,發帖之前:) – jaymarvels

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和它給我query_parsing_exception當我使用,而不是方面長期

+0

這仍然帶來了超過<我所期望的41次命中。 – jaymarvels

+0

@ jj72uk你能編輯你的文章並給出一個例子或你的數據樣本嗎? –

+0

已經這樣做了。希望格式正確! – jaymarvels