Created
March 3, 2014 16:15
-
-
Save johbo/9328423 to your computer and use it in GitHub Desktop.
Closest item out of a group
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
# try to delete the index in case it's already there | |
curl -XDELETE "http://localhost:9200/joh-test/" | |
# create index, add mapping | |
curl -XPOST "http://localhost:9200/joh-test/" | |
curl -XPUT "http://localhost:9200/joh-test/item/_mapping" -d' | |
{ | |
"item": { | |
"properties": { | |
"location": { | |
"type": "geo_point", | |
"lat_lon": true | |
}, | |
"group_locations": { | |
"properties": { | |
"location": { | |
"type": "geo_point", | |
"lat_lon": true | |
} | |
} | |
} | |
} | |
} | |
}' | |
# fill items | |
curl -XPOST "http://localhost:9200/joh-test/item/1/" -d' | |
{ | |
"id": "1", | |
"name": "A (close)", | |
"group": null, | |
"location": { | |
"lon": 13.4, | |
"lat": 52.5 | |
} | |
}' | |
curl -XPOST "http://localhost:9200/joh-test/item/2g1/" -d' | |
{ | |
"id": "2g1", | |
"name": "B in group 1 (close)", | |
"group": 1, | |
"group_locations": [ | |
{ | |
"gid": "2g1", | |
"location": { | |
"lon": 13.399, | |
"lat": 52.5 | |
} | |
}, | |
{ | |
"gid": "3g1", | |
"location": { | |
"lon": 13.395, | |
"lat": 52.5 | |
} | |
} | |
], | |
"location": { | |
"lon": 13.399, | |
"lat": 52.5 | |
} | |
}' | |
curl -XPOST "http://localhost:9200/joh-test/item/3g1/" -d' | |
{ | |
"id": "3g1", | |
"name": "C in group 1 (far)", | |
"group": 1, | |
"group_locations": [ | |
{ | |
"gid": "2g1", | |
"location": { | |
"lon": 13.399, | |
"lat": 52.5 | |
} | |
}, | |
{ | |
"gid": "3g1", | |
"location": { | |
"lon": 13.395, | |
"lat": 52.5 | |
} | |
} | |
], | |
"location": { | |
"lon": 13.395, | |
"lat": 52.5 | |
} | |
}' | |
curl -XPOST "http://localhost:9200/joh-test/item/4/" -d' | |
{ | |
"id": "4", | |
"name": "D (far)", | |
"group": null, | |
"location": { | |
"lon": 13.39, | |
"lat": 52.5 | |
} | |
}' | |
curl -XPOST "http://localhost:9200/joh-test/item/5g2/" -d' | |
{ | |
"id": "5g2", | |
"name": "E (far)", | |
"group": 2, | |
"group_locations": [ | |
{ | |
"gid": "5g2", | |
"location": { | |
"lon": 13.385, | |
"lat": 52.5 | |
} | |
} | |
], | |
"location": { | |
"lon": 13.385, | |
"lat": 52.5 | |
} | |
}' | |
# curl -XGET "http://localhost:9200/joh-test/item/_search?pretty=1" -d' | |
# { | |
# "fields": [] | |
# }' | |
echo "Waiting" | |
sleep 1 | |
echo | |
echo | |
echo "Running search" | |
curl -XGET "http://localhost:9200/joh-test/item/_search?pretty=1" -d' | |
{ | |
"fields": [], | |
"query": { | |
"filtered": { | |
"filter": { | |
"script": { | |
"script": "import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.unit.DistanceUnit; minid = null; x = Double.POSITIVE_INFINITY; if (doc[\"group_locations.location\"].empty) {true} else {foreach(grouploc: _source.group_locations) {distance = GeoDistance.ARC.calculate(grouploc.location.lat, grouploc.location.lon, lat, lon, DistanceUnit.DEFAULT); if (distance < x) {x = distance; minid = grouploc.gid}}; minid == doc.id.value}", | |
"params": { | |
"lon": 13.4, | |
"lat": 52.5 | |
} | |
} | |
} | |
} | |
}, | |
"aggregations": { | |
"test": { | |
"terms": { | |
"field": "group" | |
} | |
} | |
} | |
}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment