Skip to content

Instantly share code, notes, and snippets.

@missinglink
Created December 27, 2014 14:38
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 missinglink/6e96f06e9e6032aa6416 to your computer and use it in GitHub Desktop.
Save missinglink/6e96f06e9e6032aa6416 to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################
# https://groups.google.com/forum/#!msg/elasticsearch/AqVrhQ7UiG8/3FlF9droCqMJ
################################################
ES='localhost:9200';
# drop index
curl -XDELETE "$ES/foo?pretty=true" 2>/dev/null;
# create index
curl -XPUT "$ES/foo?pretty=true" -d'
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"bar": {
"properties": {
"COUNTY_NAME" : {
"type" : "string"
},
"ID" : {
"type" : "long"
},
"POLYS" : {
"type" : "geo_shape",
"tree" : "quadtree",
"tree_levels" : "26"
},
"STATE" : {
"type" : "string"
}
}
}
}
}';
# Index a polygon
index_poly() {
curl -XPOST "$ES/foo/bar/1234?pretty=true" -d'
{
"ID": "1234",
"STATE": "XX",
"COUNTY_NAME": "YY",
"POLYS":{
"type":"Polygon",
"coordinates":[[
["-117.7656797176","35.2420721325"],
["-117.766565557","35.2429646794"],
["-117.7675000712","35.2429532681"],
["-117.7661768866","35.2415486409"],
["-117.7661640858","35.2415341862"],
["-117.765523769","35.2419167046"],
["-117.7656797176","35.2420721325"]
]]
}
}';
}
index_poly;
# Refresh index
curl -XPOST "$ES/foo/_refresh";
# Test query from email thread
query_from_google_group_email() {
curl -XPOST "$ES/foo/_search?pretty=true" -d'
{
"query": {
"geo_shape": {
"POLYS": {
"shape": {
"type": "envelope",
"coordinates": [
["-118.58", "35.32"],
["-118.68", "35.30"]
]
},
"relation": "intersects"
}
}
}
}';
}
# Filter query
filter_query() {
curl -XPOST "$ES/foo/_search?pretty=true" -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [{
"geo_shape": {
"POLYS": {
"relation": "intersects",
"shape": {
"type": "envelope",
"coordinates": [
["-118.58", "35.32"],
["-118.68", "35.30"]
]
}
}
}
}]
}
}
}
}
}';
}
# visual representation of geometries:
# https://gist.github.com/anonymous/c250d602d1e7fe6d3655
query_from_google_group_email;
# hit
filter_query;
# hit
curl -XGET "$ES";
# {
# "status" : 200,
# "name" : "Rax",
# "version" : {
# "number" : "1.3.4",
# "build_hash" : "a70f3ccb52200f8f2c87e9c370c6597448eb3e45",
# "build_timestamp" : "2014-09-30T09:07:17Z",
# "build_snapshot" : false,
# "lucene_version" : "4.9"
# },
# "tagline" : "You Know, for Search"
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment