Elasticsearch-dsl-py: "Failed to find geo_shape field" when doing GeoShape filtering

Created on 9 May 2019  路  2Comments  路  Source: elastic/elasticsearch-dsl-py

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.

Most helpful comment

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.

All 2 comments

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 !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beanaroo picture beanaroo  路  4Comments

primoz-k picture primoz-k  路  4Comments

zahir-koradia picture zahir-koradia  路  3Comments

njoannin picture njoannin  路  3Comments

mortada picture mortada  路  3Comments