0

Using Python 2.7 and elasticsearch-py.

Given the following JSON:

[
    {
        "Name":
        "Addresses": [
            "StreetAdd": "xxx",
            "GeoLocation": {
                "lat": xx,
                "long": yy
            }
        ]
    },
    {
        // And so on.
    }   
]

And the following mapping:

mapping = {
    "mappings": {
        "leads": {
            "properties": {
                "Addresses": {
                    "type": "nested",
                    "include_in_parent": "true",
                    "properties": {
                        "GeoLocation": "geo_point"
                    }
                }
            }
        }
    }
}

How would I get the locations within 10km of latitude 40, longitude -70? My attempt is as follows:

search_body = {
    "query" : { 
        "filtered": {
            "query": {
                "match_all" : { }
            },
            "filter": {
                "geo_distance": {
                    "distance": "10km",
                    "Addresses.GeoLocation": {
                        "lat": 40.0,
                        "lon": -70.0
                    }
                }
            }
        }
    },
    "size": 50
}

result = es.search(index=ES_INDEX, body=search_body, sort="Name:asc")
for hit in result["hits"]["hits"]:
    print hit["_source"]["Name"]

However, this is throwing the following error:

...
C:\Users\xxx\AppData\Local\Continuum\Anaconda2\lib\site-packages\elasticsearch\connection\base.pyc in _raise_error(self, status_code, raw_data)
    103             pass
    104 
--> 105         raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
    106 
    107 

RequestError: TransportError(400, u'search_phase_execution_exception')

Not that proficient yet with ES, so I'm having a hard time envisioning what schema I should use to approach this problem.

What gives?

WGS
  • 13,969
  • 4
  • 48
  • 51

1 Answers1

1

The issue is in your mapping. Here is the fixed version

mapping = {
    "mappings": {
        "leads": {
            "properties": {
                "Addresses": {
                    "type": "nested",
                    "include_in_parent": "true",
                    "properties": {
                        "GeoLocation": {
                            "type":"geo_point" <-- Note the type here
                        }
                    }
                }
            }
        }
    }
}
Rahul
  • 15,979
  • 4
  • 42
  • 63
  • Hi, thanks for the reply. It's still throwing the same error, even if I've already corrected the mapping. Let me test it using `curl`. Maybe `elasticsearch-py` is the problem. – WGS Apr 01 '16 at 08:10
  • Please check the updated mapping. Also, did you reindex docs after changing the mapping? – Rahul Apr 01 '16 at 08:48
  • +1: This has pointed me to half an answer: the mapping is indeed incorrect and this will help me. However, I am encountering a somewhat new but related error: how can I insert records without the `Addresses` property given that this mapping is required for all other entries with the `Addresses` property? Is it another mapping property that I need to make it optional? – WGS Apr 04 '16 at 03:04
  • It is always optional Even when it is defined explicitly in the mapping. You can index.with addresses set to.null. this field will nt be indexed – Rahul Apr 04 '16 at 07:47
  • Accepting this answer. My error seems to be coming from elsewhere. Seems like ES is throwing an error when indexing my data using the mapping above IFF the particular data point doesn't have any address. Weird. – WGS Apr 06 '16 at 04:25