Installing ElasticSearch on Amazon EC2

I wanted to set up ElasticSearch on Amazon EC2 and these are the steps that i followed to do that
  • Connect to your Amazon EC2 instance using SSH and execute following command to download ElasticSearch v 90.9 on my instance
    
    wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.9.tar.gz
    
    It took less than a minute to download the elasticsearch binary on the instance
  • Next i executed following command to unzip the elasticsearch-0.90.9.tar.gz
    
    tar -xf elasticsearch-0.90.9.tar.gz
    
  • After extracting the elasticsearch binaries start it by executing following command
    
    elasticsearch-0.90.9/bin/elasticsearch –f
    
  • After starting the ElasticSearch instance i could use the ES REST API from the SSH console on the same instance
    
    curl -XGET http://localhost:9200/_search
    
  • But since i want to access it from my local machine/outside the instance i had to open the 9200 port on that instances firewall that i could do by changing the security group and adding 9200 and 9300 ports to it
  • Then you can use the public DNS for your EC2 instance and query the REST instance from your local machine like this
    
    curl -XGET http://<yourinstancename>.us-west-1.compute.amazonaws.com:9200/_search
    

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.