Using Elastic Search to implement Locate US/ Geo Saptial Search

Elastic Search has nice support for geo spatial search. I wanted to try that out. So i started by creating a index called testgeo and i did add addresses of few walmart locations in it. Then i used Elastic Search to figure out what store location is close to my location. I followed these steps to implement this sample
  1. First i did create a new Index named testgeo like this, while creating index i marked location field as geo_type, this makes it possible to make geo distance queries.
    
    curl -XPUT 'localhost:9200/testgeo' -d '{
        "mappings": {
            "walmart": {
              "properties": {
                "shopName": {
                    "type": "string"
                 },
                 "address": {
                    "properties": {
                       "city": {
                          "type": "string"
                       },
                       
                       "state": {
                          "type": "string"
                       },
                       "streetName": {
                          "type": "string"
                       },
                       "location": {
                          "type": "geo_point"
                          }
                       }
                    }
                 }
                 
              }
        }
    }'
    
  2. Then i did add few locations to my index for each of the location i used the latitude, longitude for address of the location and stored it in my testgeo index
    
    curl -XPOST 'localhost:9200/testgeo/walmart' -d '{
       "storeName": "Walmart Supercenter - Marina",
       "address": {
          "streetName": "150 Beach Rd",
          "cityName": "Marina",
          "state": "CA",
          "zipCode": 93933,
          "location": [
             -121.800565,
             36.69359
          ] 
       }
    }'
                
    curl -XPOST 'localhost:9200/testgeo/walmart' -d '{
       "storeName": "Walmart Mkt - Modesto",
       "address": {
          "streetName": "\"1421 Coffee Rd",
          "cityName": "Modesto",
          "state": "CA",
          "zipCode": 95355,
          "location": [
             -120.976622,
             37.664054
          ] 
       }
    }'
                
    curl -XPOST 'localhost:9200/testgeo/walmart' -d '{
       "storeName": "Walmart Supercenter - Patterson",
       "address": {
          "streetName": "\"1030 Sperry Ave",
          "cityName": "Patterson",
          "state": "CA",
          "zipCode": 95363,
          "location": [
             -121.142528,
             37.464052
          ] 
       }
    }'            
    
  3. Once my index data is in place i could query the index with type equal to geo_distance, and i had to give a location from which i want to search all the stores in 100 KM of the location
    
    curl -XPOST 'localhost:9200/testgeo/repair/_search?pretty=true'  -d '
    {
        "query": {
            "filtered": {
               "query": {
                    "match_all": {}
                },
               "filter": {
                   "geo_distance": {
                      "distance": 100,
                      "distance_unit": "km",
                      "address.location": {
                         "lat": 37.53,
                         "lon": -121.97
                      }
                   } 
               }
            }
        }
    }'
    
Elastic Search returned list of stores matching the criteria.

1 comment:

Anonymous said...

Works well,

In your search, change 'repair' to 'walmart'