2016-12-14 72 views
0

我使用彈性搜索版本1.7.5如何實現彈性搜索而不是過濾?

我已經採取了一些想法,從這個帖子: Elastic search Not filter inside and filter

下面的查詢似乎並沒有被過濾掉了與item_category_id 17相關的項目。有沒有可以看到的語法錯誤? not以外的filters陣列內定義的術語工作正常。

 { 
     query: { 
      filtered: { 
       filter: { 
        and: { 
         filters: [ 
          { not: { 
            filter: { 
             terms: {item_category_ids: [17] } 
            } 
           } 
          }, 
          { term: { user_inactive: false } }, 
          { term: { kind: 1 } } 
         ] 
        } 
       } 
      } 
     } 

回答

0

這是我在追求什麼。

{ 
     query: { 
      filtered: { 
       filter: { 
        and: [ 
         { terms: { published_posted_community_ids: @community_ids } }, 
         { term: { user_inactive: false } }, 
         { terms: { kind: 1 } }, 
         { not: { terms: { item_category_ids: 17 } } } 
        ] 
       } 
      } 
     } 
    } 
1

在ES 2.0的filtered and and/not queries have been deprecated,你應該使用bool/filter/must_not,而不是像這樣:

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "bool": { 
      "must_not": { 
       "terms": { 
       "item_category_ids": [ 
        17 
       ] 
       } 
      } 
      } 
     }, 
     { 
      "term": { 
      "user_inactive": false 
      } 
     }, 
     { 
      "term": { 
      "kind": 1 
      } 
     } 
     ] 
    } 
    } 
} 

對於ES 1.7,你需要查詢改成這樣:

{ 
    "query": { 
    "constant_score": { 
     "filter": { 
     "bool": { 
      "must_not": { 
      "terms": { 
       "item_category_ids": [ 
       17 
       ] 
      } 
      }, 
      "must": [ 
      { 
       "term": { 
       "user_inactive": false 
       } 
      }, 
      { 
       "term": { 
       "kind": 1 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 
+0

對不起,我應該指定我公司採用彈性搜索版本1.7.5 – matthewalexander

+0

看樣子,這查詢不工作,ES 1.7.5 ... – matthewalexander

+0

沒有這對ES 2查詢然後。我已經更新了我的答案 – Val