Skip to content

Instantly share code, notes, and snippets.

@waldoj
Last active August 29, 2015 14:10
Show Gist options
  • Save waldoj/004f77755179d862fe3a to your computer and use it in GitHub Desktop.
Save waldoj/004f77755179d862fe3a to your computer and use it in GitHub Desktop.
A demonstration of the Elasticsearch query that yields the error "Failed to find geo_shape field [location]]." This is a search of a collection of documents—each of which contain a pair of coordinates—against a pre-indexed collection of shapes. For simplicity's sake, I've included just one shape and just one document. This is with Elasticsearch …
# Remove old data
curl -XDELETE "http://localhost:9200/shapes"
curl -XDELETE "http://localhost:9200/businesses"
# Create shapes index, with mapping
curl -XPOST "http://localhost:9200/shapes/" -d '
{
"shapes" : {
"mappings" : {
"municipalities" : {
"properties" : {
"geometry" : {
"type" : "geo_shape",
"tree" : "quadtree",
"tree_levels" : 26
},
"index" : {
"properties" : {
"_id" : {
"type" : "string"
},
"_index" : {
"type" : "string"
},
"_type" : {
"type" : "string"
}
}
},
"properties" : {
"properties" : {
"ALAND" : {
"type" : "long"
},
"AWATER" : {
"type" : "long"
},
"CBSAFP" : {
"type" : "string"
},
"CLASSFP" : {
"type" : "string"
},
"COUNTYFP" : {
"type" : "string"
},
"COUNTYNS" : {
"type" : "string"
},
"CSAFP" : {
"type" : "string"
},
"FUNCSTAT" : {
"type" : "string"
},
"GEOID" : {
"type" : "string"
},
"INTPTLAT" : {
"type" : "string"
},
"INTPTLON" : {
"type" : "string"
},
"LSAD" : {
"type" : "string"
},
"METDIVFP" : {
"type" : "string"
},
"MTFCC" : {
"type" : "string"
},
"NAME" : {
"type" : "string"
},
"NAMELSAD" : {
"type" : "string"
},
"STATEFP" : {
"type" : "string"
}
}
},
"type" : {
"type" : "string"
}
}
}
}
}
}
'
# Add a municipality to the shapes index
curl -XPOST "http://localhost:9200/shapes/municipalities/540" -d '
{
"type": "Feature",
"properties": {
"STATEFP": "51",
"COUNTYFP": "540",
"COUNTYNS": "01789068",
"GEOID": "51540",
"NAME": "Charlottesville",
"NAMELSAD": "Charlottesville city",
"LSAD": "25",
"CLASSFP": "C7",
"MTFCC": "G4020",
"CSAFP": null,
"CBSAFP": "16820",
"METDIVFP": null,
"FUNCSTAT": "F",
"ALAND": 26516227,
"AWATER": 52974,
"INTPTLAT": "+38.0376579",
"INTPTLON": "-078.4853806"
},
"geometry":{
"type":"Polygon",
"coordinates": [
[[-78.52364,38.022739],[-78.52335,38.023536],[-78.52313,38.02634],[-78.52005,38.02604],[-78.52026,38.028301],[-78.519832,38.028644],[-78.520179,38.029781],[-78.51833,38.02984],[-78.51829,38.028558],[-78.51431,38.02876],[-78.51101,38.03083],[-78.50961,38.03216],[-78.5088,38.03227],[-78.50796,38.03197],[-78.50711,38.032108],[-78.50458,38.03202],[-78.50184,38.03213],[-78.49779,38.033255],[-78.49853,38.033448],[-78.50025,38.034451],[-78.50166,38.036136],[-78.50346,38.036746],[-78.50125,38.04041],[-78.50204,38.041226],[-78.5036,38.042217],[-78.50422,38.04284],[-78.50498,38.042581],[-78.50577,38.04128],[-78.50523,38.041038],[-78.50593,38.040138],[-78.50614,38.03944],[-78.50683,38.039338],[-78.5081,38.035831],[-78.51333,38.037137],[-78.51553,38.036738],[-78.51689,38.039879],[-78.51638,38.040286],[-78.51737,38.04119],[-78.51607,38.04349],[-78.51396,38.04418],[-78.51463,38.044838],[-78.51141,38.04379],[-78.50678,38.04496],[-78.50593,38.04552],[-78.50484,38.047275],[-78.510297,38.051308],[-78.50753,38.05642],[-78.50134,38.057685],[-78.50088,38.05844],[-78.50231,38.06053],[-78.49885,38.06281],[-78.49553,38.064837],[-78.49503,38.065337],[-78.49264,38.06363],[-78.49158,38.06189],[-78.485154,38.069027],[-78.48253,38.067637],[-78.48113,38.066738],[-78.48094,38.06563],[-78.48031,38.065794],[-78.4789,38.065647],[-78.47719,38.064752],[-78.47598,38.06669],[-78.47453,38.06771],[-78.47494,38.068683],[-78.472985,38.07013],[-78.4714,38.070591],[-78.47073,38.06974],[-78.47234,38.067515],[-78.46853,38.068131],[-78.46843,38.067631],[-78.46689,38.06783],[-78.46547,38.065777],[-78.46433,38.064637],[-78.46784,38.05913],[-78.46963,38.055838],[-78.47223,38.052638],[-78.47143,38.050438],[-78.47213,38.049738],[-78.47233,38.049037],[-78.47082,38.048918],[-78.46993,38.04704],[-78.46612,38.045769],[-78.46543,38.047138],[-78.46533,38.048138],[-78.46438,38.04875],[-78.46403,38.049538],[-78.46145,38.04946],[-78.45733,38.049538],[-78.45717,38.05031],[-78.45872,38.05094],[-78.456921,38.055035],[-78.45658,38.05555],[-78.45471,38.054729],[-78.44905,38.05873],[-78.44776,38.057182],[-78.44749,38.0558],[-78.44759,38.054215],[-78.44717,38.051105],[-78.446311,38.04942],[-78.44729,38.04615],[-78.44909,38.045248],[-78.45182,38.045788],[-78.45392,38.045803],[-78.45464,38.045361],[-78.45484,38.043939],[-78.45411,38.042394],[-78.45419,38.039615],[-78.45499,38.037605],[-78.45619,38.03632],[-78.45877,38.034354],[-78.45994,38.033212],[-78.46032,38.03224],[-78.46036,38.03051],[-78.46101,38.02922],[-78.46077,38.02776],[-78.45938,38.02741],[-78.45828,38.02793],[-78.45762,38.028743],[-78.45611,38.029742],[-78.45467,38.029863],[-78.45323,38.028985],[-78.45246,38.02802],[-78.45264,38.02645],[-78.4547,38.022101],[-78.45665,38.021147],[-78.45933,38.022439],[-78.46163,38.022639],[-78.46208,38.02249],[-78.46356,38.020533],[-78.46693,38.01836],[-78.46923,38.014739],[-78.46843,38.01404],[-78.46973,38.01144],[-78.474634,38.01304],[-78.4751,38.013518],[-78.47473,38.01424],[-78.47563,38.014739],[-78.47804,38.01412],[-78.47942,38.013385],[-78.48026,38.01333],[-78.4815,38.011956],[-78.48316,38.011956],[-78.4833,38.014471],[-78.48453,38.0157],[-78.48666,38.016138],[-78.48727,38.016555],[-78.48895,38.01667],[-78.49033,38.017257],[-78.491351,38.016922],[-78.49197,38.01624],[-78.49351,38.01502],[-78.49486,38.01443],[-78.495661,38.014409],[-78.498837,38.012904],[-78.49988,38.01286],[-78.50079,38.01214],[-78.50172,38.01105],[-78.5033,38.0102],[-78.50443,38.010118],[-78.50724,38.01086],[-78.50828,38.01128],[-78.50929,38.011082],[-78.510179,38.009923],[-78.51156,38.009589],[-78.51285,38.0096],[-78.51475,38.00994],[-78.51738,38.010245],[-78.51915,38.01083],[-78.51882,38.011865],[-78.51875,38.013211],[-78.51911,38.0146],[-78.51904,38.015308],[-78.51954,38.017252],[-78.52046,38.01773],[-78.52037,38.01835],[-78.5213,38.018884],[-78.5221,38.018423],[-78.52343,38.019539],[-78.52364,38.022739]]
]
}
}
'
# Create businesses index
curl -XPUT 'http://localhost:9200/business/'
# Define the mapping for the businesses index
curl -XPUT localhost:9200/business/1/_mapping -d '
{
"properties": {
"agent_name": {
"type": "string"
},
"address_date": {
"type": "date"
},
"domestic": {
"type": "string"
},
"agent_status": {
"type": "string"
},
"agent_court_locality": {
"type": "string"
},
"incorporation_date": {
"type": "date"
},
"id": {
"index": "not_analyzed",
"type": "string"
},
"city": {
"type": "string"
},
"zip": {
"index": "not_analyzed",
"type": "string"
},
"state": {
"index": "not_analyzed",
"type": "string"
},
"location": {
"type": "object",
"properties": {
"coordinates": {
"type": "geo_point",
"lat_lon": "true"
}
}
},
"state_formed": {
"index": "not_analyzed",
"type": "string"
},
"status": {
"type": "string"
},
"agent_zip": {
"type": "string"
},
"agent_city": {
"type": "string"
},
"status_date": {
"type": "date"
},
"street_1": {
"type": "string"
},
"street_2": {
"type": "string"
},
"agent_date": {
"type": "date"
},
"name": {
"type": "string"
},
"expiration_date": {
"type": "date"
},
"industry": {
"type": "string"
},
"agent_street_1": {
"type": "string"
},
"agent_street_2": {
"type": "string"
},
"agent_state": {
"index": "not_analyzed",
"type": "string"
}
}
}
'
# Add a business document to the businesses index
curl -XPOST "http://localhost:9200/business/1/" -d
'{
"agent_name": "JON DOE",
"address_date": null,
"domestic": "L",
"agent_status": "7",
"agent_court_locality": "CHARLOTTESVILLE CITY",
"incorporation_date": "1988-07-12",
"id": "L004036",
"city": "CHARLOTTESVILLE",
"zip": "22905",
"state": "VA",
"state_formed": "VIRGINIA",
"status": "ACTIVE",
"agent_zip": "22905",
"agent_city": "CHARLOTTESVILLE",
"status_date": "2004-10-06",
"street_1": "102 SOUTH FIRST ST",
"street_2": "POST OFFICE BOX 530",
"agent_date": "1998-06-10",
"name": "ACME INVESTMENTS, LP",
"expiration_date": "2043-12-31",
"industry": "",
"agent_street_1": "102 SOUTH FIRST ST",
"agent_street_2": "PO BOX 530",
"agent_state": "VA"
}'
# Refresh indices
curl -XPOST "http://localhost:9200/business/_refresh"
curl -XPOST "http://localhost:9200/shapes/_refresh"
# Run our search
curl -XPOST "http://localhost:9200/business/1/_search?pretty=true" -d '
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_shape": {
"location": {
"indexed_shape": {
"id": "540",
"type": "municipalities",
"index": "shapes",
"path": "geometry"
}
}
}
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment