2016-12-14 54 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