WoJ
WoJ

Reputation: 29987

How to search for documents which meet meet a criterion of "is between"?

I have documents similar to

{
   "name": "A",
   "start": 5,
   "end": 10
},
{
   "name": "B",
   "start": 1,
   "end": 10
},
{
   "name": "C",
   "start": 20,
   "end": 30
}

I also have a value X (say, 7). I need to look for documents such that start < X < end. In the example above, this would yield two documents (with name equal to A and B).

I do not know how to to formulate the search query: the range query which initially seemed to be the right one actually looks for documents which meet a criterion "greater than" / "smaller than" passed in the query - which is the opposite of what I want to do.

Upvotes: 0

Views: 21

Answers (1)

Val
Val

Reputation: 217324

The following two range queries would work

{
    "query": {
        "bool": {
            "filter": [
                 {"range": {"start": {"lt": 7}}},
                 {"range": {"end": {"gt": 7}}}
            ]
        }
    }
}

Upvotes: 2

Related Questions