How to get only suitable children?

Consider a very simple model where we have locations, and each place can have zero or more events. The location will have properties such as name, description, and location data (lon / lat). An event must be attached to one place (its parent element) and must have a name and description.

{ "location" : { "properties": { "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" }, "description": { "type": "string", "analyzer": "snowball" }, "geo": { "type": "geo_point" }, "exhibits": { "type": "nested", "properties": { "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" }, "description": { "type": "string", "analyzer": "snowball" } } } } } } 

What I want to do is to request child documents (events) that perform a full-text search by their names and descriptions. I would like to get relevant events back and be able to also get their parent location name. I would also like to narrow down the results by location coordinates. I do not want to receive any events that do not match the request. Is this possible in Elastic Search? What types of queries should I use?

I tried to put events as an array property in the location (see above) and using the nested query, but it does not return the results I want (I think it returns the whole location, including all events, even those that do not match mine request). I tried putting events in a separate index (matching?) Providing the _parent property and then doing a top_children query in places, but I am not getting any results.

 { "exhibit": { "_parent": { "type": "locations" }, "properties": { "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" }, "description": { "type": "string", "analyzer": "snowball" } } } } 

Can anyone shed some light? I donโ€™t know where to start ...

+8
elasticsearch
source share
1 answer

Here is a working solution to my problem, maybe it will be useful to someone.

Location display:

 { "location" : { "properties": { "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" }, "description": { "type": "string", "analyzer": "snowball" }, "geo": { "type": "geo_point" } } } } 

Display exhibits:

 { "exhibit": { "_parent": { "type": "locations" }, "properties": { "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" }, "description": { "type": "string", "analyzer": "snowball" } } } } 

Query:

 { "fields": [ "_parent", "name", "_source" ], "query": { "bool": { "should": [ { "text": { "name": "candy" } }, { "text": { "description": "candy" } } ] } }, "filter": { "and": [ { "terms" : { "_parent": [ "4e7089a9b97d640b30695b7a", "4e7089eeb97d640b30695b7b" ] } }, { "range": { "start": { "lte": "2011-09-22" } } }, { "range": { "end": { "gte": "2011-09-22" } } } ] } } 

You must request the use of the _parent field and pass it an array of identifiers for the places where you want to limit exhibits.

+8
source share

All Articles