Skip to content

Instantly share code, notes, and snippets.

@spara
Created July 31, 2011 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spara/1117013 to your computer and use it in GitHub Desktop.
Save spara/1117013 to your computer and use it in GitHub Desktop.
Elasticsearch, register multiple geo_point types to single index
# first geo_point mapping: map1.json
"place" : {
"properties" : {
"geometry": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
curl -XPUT http://localhost:9200/geo/place/_mapping -d @map1.json
{"ok":true,"acknowledged":true}
# the data
head -n4 place_es.json
{"index":{"_index":"geo","_type":"place","_id":0}}
{"geometry":{"coordinates":{"lat":43.005895,"lon":-71.013202}},"zipcode":"00210","city":"Portsmouth","state":"NH","city_phone":"PRTSM","latitude":"43.005895","longitude":"-71.013202","status":"U","fips_class":"C5","fips_place":"3362900","fips_county":"33015","priority":"1"}
{"index":{"_index":"geo","_type":"place","_id":1}}
{"geometry":{"coordinates":{"lat":43.005895,"lon":-71.013202}},"zipcode":"00211","city":"Portsmouth","state":"NH","city_phone":"PRTSM","latitude":"43.005895","longitude":"-71.013202","status":"U","fips_class":"C5","fips_place":"3362900","fips_county":"33015","priority":"1"}
# load the data
time curl -XPUT 'http://localhost:9200/_bulk/' --data-binary @place_es.json
# returns:
...{"index":{"_index":"geo","_type":"place","_id":"105244","_version":1,"ok":true}},{"index":{"_index":"geo","_type":"place","_id":"105245","_version":1,"ok":true}},{"index":{"_index":"geo","_type":"place","_id":"105246","_version":1,"ok":true}}]}
#search
curl -XGET 'http://localhost:9200/geo/place/_search' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "20km",
"geometry.coordinates": {
"lat": 32.25,
"lon": -97.75
}
}
}
}
}
}'
# returns:
... {"_index":"geo","_type":"place","_id":"86704","_score":1.0, "_source" : {"geometry":{"coordinates":{"lat":32.421697,"lon":-97.811051}},"zipcode":"76048","city":"Acton","state":"TX","city_phone":"AKTN","latitude":"32.421697","longitude":"-97.811051","status":"C","fips_class":"U6","fips_place":"4801144","fips_county":"48221","priority":"3"}}]}}
# add second mapping for zipcodes; map3.json
{
"zipcode" : {
"properties" : {
"geometry": {
"properties": {
"coordinates": {
"type": "geo_point"
}
}
}
}
}
}
curl -XPUT http://localhost:9200/geo/zipcode/_mapping -d @map3.json{"ok":true,"acknowledged":true}
# load zipcode data
head -n4 zipcode.json
head zipcode.json
{"index":{"_index":"geo","_type":"zipcode","_id":0}}
{"geometry":{"coordinates":{"lat":46.66262363305924,"lon":-68.17241080966178}},"ZCTA5CE10":"04757","GEOID10":"04757","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S"}
{"index":{"_index":"geo","_type":"zipcode","_id":1}}
{"geometry":{"coordinates":{"lat":46.554241657415005,"lon":-67.8472337219097}},"ZCTA5CE10":"04758","GEOID10":"04758","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S"}
time curl -XPUT 'http://localhost:9200/_bulk/' --data-binary @zipcode.json
#returns:
...{"index":{"_index":"geo","_type":"zipcode","_id":"33117","_version":1,"ok":true}},{"index":{"_index":"geo","_type":"zipcode","_id":"33118","_version":1,"ok":true}},{"index":{"_index":"geo","_type":"zipcode","_id":"33119","_version":1,"ok":true}}]}
# query for zipcode geo_point
curl -XGET 'http://localhost:9200/geo/zipcode/_search' -d '{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "20km",
"geometry.coordinates": {
"lat": 32.25,
"lon": -97.75
}
}
}
}
}
}'
# returns
{"took":14,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":4,"max_score":1.0,"hits":[{"_index":"geo","_type":"zipcode","_id":"28018","_score":1.0, "_source" : {"geometry":{"coordinates":{"lat":32.28214132796423,"lon":-97.70542826804316}},"ZCTA5CE10":"76077","GEOID10":"76077","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S"}},{"_index":"geo","_type":"zipcode","_id":"29249","_score":1.0, "_source" : {"geometry":{"coordinates":{"lat":32.082253927219824,"lon":-97.7332717789152}},"ZCTA5CE10":"76690","GEOID10":"76690","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S"}},{"_index":"geo","_type":"zipcode","_id":"27996","_score":1.0, "_source" : {"geometry":{"coordinates":{"lat":32.20530457721625,"lon":-97.79713295504516}},"ZCTA5CE10":"76043","GEOID10":"76043","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S"}},{"_index":"geo","_type":"zipcode","_id":"28015","_score":1.0, "_source" : {"geometry":{"coordinates":{"lat":32.262468127364365,"lon":-97.65430177697428}},"ZCTA5CE10":"76070","GEOID10":"76070","CLASSFP10":"B5","MTFCC10":"G6350","FUNCSTAT10":"S"}}]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment