1

I am trying to get my elasticsearh query to work, but I get this error:

org.elasticsearch.common.ParsingException: [exists] unknown token [START_ARRAY] after [field]

The query is supposed to get all documents that have dates in either date.old or date.new between the two years(1500,1550), and also include documents that have undefined values for those fields.

This is my query:

{
   "query":{
      "bool":{
         "should":[
            {
               "range":{
                  "date.old":{
                     "gte":1500,
                     "lte":1550
                  }
               }
            },
            {
               "range":{
                  "date.new":{
                     "gte":1500,
                     "lte":1550
                  }
               }
            },
            {
               "bool":{
                  "must_not":{
                     "exists":{
                        "field":"date.new"
                     }
                  }
               }
            },
            {
               "bool":{
                  "must_not":{
                     "exists":{
                        "field":"date.old"
                     }
                  }
               }
            }
         ]
      }
   }
}

Do anyone see the problem here? Thanks!

2
  • Which version of ES are you using? Commented Dec 2, 2020 at 17:08
  • I'm using version 7.10.0 Commented Dec 2, 2020 at 17:11

1 Answer 1

1

Hitting the same search query as given in the question above gives no parsing error.

Adding a working example with index mapping, index data, and search result

Index Mapping:

{
  "mappings": {
    "properties": {
      "date": {
        "properties": {
          "old": {
            "type": "long"
          },
          "new": {
            "type": "long"
          }
        }
      }
    }
  }
}

Index Data:

{
  "data": {
    "new": 1501,
    "old": 10
  }
}

{
  "title": "elasticsearch"
}

Search Result:

"hits": [
  {
    "_index": "65112793",
    "_type": "_doc",
    "_id": "1",
    "_score": 1.0,
    "_source": {
      "date": {
        "new": 1501,
        "old": 10
      }
    }
  },
  {
    "_index": "65112793",
    "_type": "_doc",
    "_id": "2",
    "_score": 0.0,
    "_source": {
      "title": "elasticsearch"
    }
  }
]

EDIT 1:

Based on the comment below:

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "date.old": {
              "gte": 1500,
              "lte": 1550
            }
          }
        },
        {
          "range": {
            "date.new": {
              "gte": 1500,
              "lte": 1550
            }
          }
        }
      ],
      "must": [
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "date.new"
              }
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "exists": {
                "field": "date.old"
              }
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, you are right, I might have posted the question too fast. Thank you for your response. However, I am wondering if you know how to get the two last bool's as logical AND instead of OR ?
@kw123 please go through my updated answer, and let me know if this resolves your issue? And thank u for accepting the answer, can you please upvote the answer as well :)
Of course! It almost works, however, I don't get any data back with it. I want the first should parameter to be OR'ed with the must parameter, so that I will get the documents that either has a date between the range, or are missing both fields.. Do you know how to solve that? @ESCoder
@kw123 sorry I didn't get you. Can you please explain your use case with an example ?

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.