Created
October 17, 2012 10:26
-
-
Save marians/3904852 to your computer and use it in GitHub Desktop.
ElasticSearch - documents with multiple geo_point properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
spatialtest: { | |
document: { | |
properties: { | |
location: { | |
type: "geo_point" | |
}, | |
title: { | |
type: "string" | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# encoding: utf-8 | |
from pyes import * | |
import rawes | |
import json | |
ELASTICSEARCH_HOST = '127.0.0.1:9200' | |
ELASTICSEARCH_INDEX = 'spatialtest' | |
ELASTICSEARCH_TYPE = 'document' | |
if __name__ == '__main__': | |
es = ES(ELASTICSEARCH_HOST) | |
try: | |
es.delete_index(ELASTICSEARCH_INDEX) | |
except: | |
pass | |
es.create_index(ELASTICSEARCH_INDEX) | |
mapping = { | |
'location': { | |
'type': 'geo_point' | |
} | |
} | |
es.put_mapping(ELASTICSEARCH_TYPE, | |
{'properties': mapping}, [ELASTICSEARCH_INDEX]) | |
es.refresh() | |
doc0 = { | |
'title': 'Bonn, one point', | |
'location': [ | |
[7.1183, 50.7350] | |
] | |
} | |
doc1 = { | |
'title': 'Cologne, one point', | |
'location': [ | |
[6.9562, 50.9342] | |
] | |
} | |
doc2 = { | |
'title': 'Cologne and Bonn, two points', | |
'location': [ | |
[7.1183, 50.7350], | |
[6.9562, 50.9342] | |
] | |
} | |
es.index(doc0, ELASTICSEARCH_INDEX, ELASTICSEARCH_TYPE, 1) | |
es.index(doc1, ELASTICSEARCH_INDEX, ELASTICSEARCH_TYPE, 2) | |
es.index(doc2, ELASTICSEARCH_INDEX, ELASTICSEARCH_TYPE, 3) | |
es.refresh() | |
simple_query = { | |
"query": { | |
"query_string": { | |
"fields": ['title'], | |
"query": 'cologne' | |
} | |
} | |
} | |
# This query should match all | |
query_all = { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"geo_distance": { | |
"distance": "1000km", | |
"document.location": [6.9562, 50.9342] | |
} | |
} | |
} | |
# This should only find only two nodes | |
query_close_to_cologne = { | |
"query": { | |
"match_all": {} | |
}, | |
"filter": { | |
"geo_distance": { | |
"distance": "1km", | |
"document.location": [6.9562, 50.9342] | |
} | |
} | |
} | |
# using rawes for querying | |
conn = rawes.Elastic(ELASTICSEARCH_HOST) | |
#simple_query_result = conn.get('spatialtest/document/_search', data=simple_query) | |
#print json.dumps(simple_query_result, indent=4) | |
print "Querying within 100km radius - should match all" | |
results = conn.get('spatialtest/document/_search', data=query_all) | |
print results['hits']['total'], "hits" | |
#print json.dumps(results, indent=4) | |
print "Querying within 1km radius - should match 2" | |
results = conn.get('spatialtest/document/_search', data=query_close_to_cologne) | |
print results['hits']['total'], "hits" | |
print json.dumps(results, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment