Geopoint with an array - elasticsearch

I have got a DOC 'company' and a TYPE 'user'. Inside 'user' there is a field called 'locations' and this is an array. Every location has a field called 'point' which is a GeoPoint as a string "40.748770,-73.985487". How can I get the points nearby this point?
This example I'm showing bellow is not working:
GET /company/user/_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "500m",
"locations.point" : "40.748770, -73.985487"
}
}
}
}
}

Below example might help you,
GET /company/user/_search
{
"query": {
"filtered": {
"filter": {
"geo_distance": {
"distance": "1km",
"location": {
"lat": 40.715,
"lon": -73.988
}
}
}
}
}
}

Related

How to get the distance between two geo points in the elasticsearch?

The following Query in the elasticsearch will give results located within the specified distance. The result doesn't includes the actual distance. Is there any way to get this information in elasticsearch ?
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "12km",
"pin.location" : [-70, 40]
}
}
}
}
Use a script field with the same reference point as in the geo_distance filter. Full query:
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "5000km",
"pin.location": [
-70,
40
]
}
}
}
},
"script_fields": {
"distance_in_m": {
"script": "doc['pin.location'].arcDistance(40, -70)"
}
}
}
OR
geo-sort and you'll get the distance info included in the sort response.

ElasticSearch Search geo-points within a circle that was created from a geo-point

I've gone through the documentation and searched google but can't find the answer I'm looking for. All i want to do is search points within a circle created from a geo-point
GET /pointsinradius/_doc/_search
{
"query": {
"geo_shape": {
"location": {
"shape": {
"type": "circle",
"radius": "1km",
"coordinates": [
-32.360738, 22.56237
]
}
}
}
}
}
You need to use geo_distance query
Here is an example
GET /my_locations/_search
{
"query": {
"bool" : {
"must" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "200km",
"pin.location" : {
"lat" : 40,
"lon" : -70
}
}
}
}
}
}

Querying a nested array in Elasticsearch

I have the following data index in Elasticsearch with the following syntax:
PUT /try1
{
"mappings" : {
"product" : {
"properties" : {
"name": { "type" : "text" },
"categories": {
"type": "nested",
"properties": {
"range":{"type":"text"}
}
}
}
}
}
}
The range type has an array of words:["high","medium","low"]
I need to access the range element inside the nested category. I tried using the following syntax:
GET /try1/product/_search
{
"query": {
"nested" : {
"path" : "categories",
"query" : {
"bool" : {
"must" : [
{ "match" : {"categories.range": "low"} }
]
}
}
}
}
}
However, I am getting an error with the message:
"reason": """failed to create query:...
Can someone please offer a solution to this?
#KGB can you try to make your query slightly differently like this:
{
"query": {
"bool": {
"must": [
{
"match": {
"categories.range": "low"
}
}
]
}
}
}
{
"query": {
"nested" : {
"path" : "categories",
"query" : {
"bool" : {
"must" : [
{ categories.range": "low"}
]
}
}
}
}
}
This worked perfectly

Filtering on Elasticsearch Optional Fields

I'm using Elasticsearch to query a document type, that has an optional location field. When searching, if that field does not exist, those results should be returned, as well as filtering on the results that do.
It seems like the OR filter in Elasticsearch does not short circuit, as this:
"query": {
"filtered": {
"query": {
"match_phrase_prefix": {
"display_name": "SearchQuery"
}
},
"filter": {
"or": [
{
"missing": {
"field": "location"
}
},
{
"geo_distance" : {
"distance" : "20mi",
"location" : {
"lat" : 33.47,
"lon" : -112.07
}
}
}
]
}
Fails with "failed to find geo_point field [location]".
Is there any way to perform this (or something along the same vein) in ES?
I don't know why yours isn't working but I've used the bool filter with great success in the past. The should option is essentially an or and makes sure at least one is true. Give it a try and comment on my answer if it still doesn't work. Also double check I copied your query terms properly :)
{
"filtered" : {
"query" : {
"match_phrase_prefix": {
"display_name": "SearchQuery"
}
},
"filter" : {
"bool" : {
"should" : [
{
"missing": { "field": "location" }
},
{
"geo_distance" : {
"distance" : "20mi",
"location" : {
"lat" : 33.47,
"lon" : -112.07
}
}
}
]
}
}
}
}
For anyone with the same issue, I kind of just hacked around it. For any documents that were missing a "location", I added one with a lat/lon of 0/0. Then I altered my query to be:
"filter": {
"or": [
{
"geo_distance": {
"distance": "0.1mi",
"location": {
"lat": 0,
"lon": 0
}
}
},
{
"geo_distance": {
"distance": "30mi",
"location": {
"lat": [lat variable],
"lon": [lon variable]
}
}
}
]
}

Apply geo distance filter on nested field

My mapping contains a nested field like this:
"Locations": {
"type": "nested",
"properties": {
"Name": {
"type": "string"
},
"GeoPoint": {
"type": "geo_point"
}
}
}
So basically what I'm trying to do is store some additional attributes with every location of the document.
Unfortunately, it looks like this won't work with a geo distance filter:
GET /myIndex/myType/_search
{
"filter": {
"geo_distance": {
"distance": "100 mi",
"Locations.GeoPoint": {
lat: 40.70,
lon: -74.00
}
}
}
}
won't return the any results, whereas the filter works flawlessly if the GeoPoint is directly on the document itself instead of on the nested field:
GET /myIndex/myType/_search
{
"filter": {
"geo_distance": {
"distance": "100 mi",
"GeoPoint": {
lat: 40.70,
lon: -74.00
}
}
}
}
Is there any way to make geo distance filter work with geo_point on the nested field?
A nested filter does the job:
GET /myIndex/myType/_search
{
"filter" : {
"nested" : {
"path" : "Locations",
"filter" : {
"geo_distance": {
"distance": "100 mi",
"GeoPoint": {
"lat": 40.70,
"lon": -74.00
}
}
}
}
}
}
An update for ElasticSearch 5.5 users, #Max's answer has been deprecated for the Nested Filter and Nested Query feature using the dot notation instead of explicitly wrapping a "nested" field around your query.
For example:
{
"query": {
"nested" : {
"path" : "obj1",
"score_mode" : "avg",
"query" : {
"bool" : {
"must" : [
{ "match" : {"obj1.name" : "blue"} },
{ "range" : {"obj1.count" : {"gt" : 5}} }
]
}
}
}
}
}
More information: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html

Resources