M
M
Mikhailo Poberezhny2017-04-28 15:52:40
MongoDB
Mikhailo Poberezhny, 2017-04-28 15:52:40

How to search by nested in elasticsearch?

there are Users they have an array of eDates:

"eDates" : [ 
        {
            "dateFrom" : ISODate("2017-04-01T12:33:50.000+0000"), 
            "dateTo" : ISODate("2017-04-05T12:33:50.000+0000"), 
            "_id" : ObjectId("58c2ab50875060766968d41d")
        }, 
        {
            "dateFrom" : ISODate("2017-10-01T12:34:04.000+0000"), 
            "dateTo" : ISODate("2017-10-07T12:34:04.000+0000"), 
            "_id" : ObjectId("58c2ab50875060766968d41c")
        }, ...
    ],

I need to filter by date users in which at least 1 element from the array matches the specified filter.
Those. i fill dateFrom and dateTo and search
let nested = []
let from = {"range" : {"eDates.dateFrom" : {"gt": new Date(data.dateFrom)} }};
let to = {"range" : {"eDates.dateTo" : {"lt": new Date(data.dateTo)} }};
nested.push(from, to);

"filter": {
"nested" : {
            "path" : "eDates",
            "query" : {
                "bool" : {
                    "must" : nested
                }
            }
        }
}

The problem is that it can find matches in different subs. objects and not exactly 1.
Similar to how it does it in mongo:
{ "eDates": { $elemMatch: {dateFrom: {$gte: ISODate("2016-04-04T00:00:00.000Z") }, dateTo: {$lte: ISODate("2017-04-27T00:00:00.000Z")} } } }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-04-28
@RidgeA

The problem is that in the usual order, an array of objects is mapped into one object when indexed.
That's why he finds it.
What would be looking for as you want - you need to change the mapping.
https://www.elastic.co/guide/en/elasticsearch/guid...
https://www.elastic.co/guide/en/elasticsearch/guid...
https://www.elastic.co/ guide/en/elasticsearch/refe...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question