3

I need to filter out documents with some kind of conditional logic (I think), but I can't get this working.

My documents are like this: {type: 'A'} {type: 'B', foo: ['bar']} {type: 'B', foo: []}

Now I need to filter out all documents of type 'B' where foo is empty. In pseudocode the query would be: if(type == 'B' && foo == []) { // filter out this document } My main query looks like this: query: { filtered: { query: { bool: { must: { match_all: [] } } }, filter: { bool:{ must_not: { term: {'_hidden': true} } } } } } Elasticsearch Version is 1.5.0

4
  • Elasticsearch does not store empty fields. I guess that you are talking about a field that does not exists, right? Could you use a exists filter inside a must_not? elastic.co/guide/en/elasticsearch/reference/1.4/… did you try? Commented Jun 28, 2016 at 16:16
  • Hi @WaldemarNeto. Yes, I did try to use an exists and/or missing filter. That does work for all documents of type 'B'. but it also filters all documents of type 'A', because there is never a "property" foo. Commented Jun 28, 2016 at 16:22
  • Just to understand, if type == "A" foo does not matter, right? and if type == "B" foo need to be empty? Commented Jun 28, 2016 at 16:29
  • Sorry, I misread your questions. If type == 'A' foo doesn't matter, because there is no foo, if type == 'B' foo need NOT to be empty! Commented Jun 28, 2016 at 16:40

1 Answer 1

2

If I understood, is that? if type == a, all documents that have a will return. And if type == b and the field foo is not there they will return, make sense?

{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "bool": {
               "should": [
                  {
                     "term": {
                        "type": "a"
                     }
                  },
                  {
                      "bool": {
                          "must": [
                             {
                                 "term": {
                                    "type": "b"
                                 }
                             }
                          ],
                          "must_not": [
                             {
                                 "missing": {
                                    "field": "foo"
                                 }
                             }
                          ]
                      }
                  }
               ]
            }
         }
      }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think he wants to exclude type b documents which have a missing foo field not include them in the results.
That is correct. And additionally I don't really care about the type of the other documents. There may be many different types. All I want is to exclude the documents of type 'B' where 'foo' is missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.