Hey there, I am having some trouble doing queries using GeoShape. Here's my basic setup.
I have an index setup like so.
class LayerIndex(DocType):
location = GeoShape()
I prepare the location field as an envelope with coordinates for its bounding box. So far so good, as I can see the following from doing a request to my layer index:
"location": {
"type": "envelope",
"coordinates": [[-168.4105682373, -60.9225349426], [-24.6494560242, 76.1670074463]]
}
My problem is I now want to add a filter on this GeoShape, such as intersects or contains. I have added other filters in the past with no problem, but there is something I must be missing here. Here's my function to add this filter:
Q = elasticsearch_dsl.query.Q
def add_geoshape_search(search, bbox):
# Add in Bounding Box filter
if bbox:
minx, miny, maxx, maxy = bbox.split(',')
bbox_filter = Q({'geo_shape': {
'location': {
'shape': {
'type': 'envelope',
'coordinates': [[float(minx), float(miny)],
[float(maxx), float(maxy)]]
},
'relation': 'contains'
}
}})
search = search.query(bbox_filter)
return search
In my main querying function, I call it like so:
search = add_geoshape_search(search, parameters.get("extent", None))
This correctly grabs the extent from the parameters, correctly creates the bbox_filter (at least it seems to), but the problem is that I get an error when applying said filter:
RequestError(400, u'search_phase_execution_exception', u'failed to find geo_shape field [location]')
This doesn't make sense to me as I can see from earlier that the location field does exist, which I have specified is a GeoShape field. Am I doing something wrong or is this not supported?
EDIT: For reference, this is version 6.2.0.
Never mind, solved my own issue.
What I was missing was adding "ignore_unmapped": True in my geo_shape query beside location. The behaviour of this is confusing in my opinion. Essentially, what was happening was I would give it a query which matched none of my Layer objects - and it gives an error. However, if the bbox I gave it finds at least one match, it's fine. I don't want the search to fail just because there's no results, I just want it to give no results.
ignore_unmapped defaults to False, hence I was getting this error. My LayerIndex is working perfectly fine.
Hopefully this helps some future person :) Cheers.
Thank you for leaving the solution here, @travislbrundage !
Most helpful comment
Never mind, solved my own issue.
What I was missing was adding
"ignore_unmapped": Truein mygeo_shapequery besidelocation. The behaviour of this is confusing in my opinion. Essentially, what was happening was I would give it a query which matched none of my Layer objects - and it gives an error. However, if the bbox I gave it finds at least one match, it's fine. I don't want the search to fail just because there's no results, I just want it to give no results.ignore_unmappeddefaults toFalse, hence I was getting this error. My LayerIndex is working perfectly fine.Hopefully this helps some future person :) Cheers.