Jagadeeshlal
Jagadeeshlal

Reputation: 140

To Select documents having same startDate and endDate

I have some documents where in each document , there is a startDate and endDate date fields. I need all documents with both these value as same. I couldn't find any query which will help me to do it.

Upvotes: 5

Views: 84

Answers (2)

Vineeth Mohan
Vineeth Mohan

Reputation: 19273

This can be achieved in 2 manner

  1. Index solution - While indexing add an additional field called isDateSame and set it to true or false based on the value of startDate and endDate. Then you can easily do a query based on that field. This is the best optimized solution
  2. Script solution - Elasticsdearch maintains all the indexed data in field data which is more like a reverse reverse index. Using script you can access any indexed fields and do comparison. This is pretty fast but not as good as first one.You can use the following query for the same

Upvotes: 1

ManishKG
ManishKG

Reputation: 609

Elasticsearch supports script filters, which you can use in this case . More Info Something like this is what you will need -

POST /<yourIndex>/<yourType>/_search?
{
    "query": {
        "filtered": {
           "filter": {
               "script": {
                  "script": "doc['startDate'].value == doc['endDate'].value"
               }
           }
        }
    }
}

Upvotes: 3

Related Questions