6

I am using elastic search version 1.7.5

I've taken some ideas from this post: Elastic search Not filter inside and filter

the query below does not seem to be filtering out the items that are associated with item_category_id 17. Is there a syntax error that you can see? The terms defined outside the not, inside the filters array are working fine.

      {
      query: {
          filtered: {
              filter: {
                  and: {
                      filters: [
                          { not: {
                                  filter: {
                                      terms: {item_category_ids: [17] }
                                  }
                              }
                          },
                          { term: { user_inactive: false } },
                          { term: { kind: 1 } }
                      ]
                  }
              }
          }
      }
Community
  • 1
  • 1
malexanders
  • 3,203
  • 5
  • 26
  • 46

2 Answers2

8

The filtered and and/not queries have been deprecated in ES 2.0, you should use bool/filter/must_not instead like this:

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

For ES 1.7, you need to change the query to this:

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must_not": {
            "terms": {
              "item_category_ids": [
                17
              ]
            }
          },
          "must": [
            {
              "term": {
                "user_inactive": false
              }
            },
            {
              "term": {
                "kind": 1
              }
            }
          ]
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
0

This is what I'm after.

{
      query: {
          filtered: {
              filter: {
                  and: [
                      { terms: { published_posted_community_ids: @community_ids } },
                      { term: { user_inactive: false } },
                      { terms: { kind: 1 } },
                      { not: { terms: { item_category_ids: 17 } } }
                  ]
              }
          }
      }
  }
malexanders
  • 3,203
  • 5
  • 26
  • 46