Wednesday, July 30, 2014

autocomplete with elasticsearch

step:1-> Index with analyzer settings

{
   "settings": {
      "analysis": {
         "filter": {
            "nGram_filter": {
               "type": "nGram",
               "min_gram": 2,
               "max_gram": 20,
               "token_chars": [
                  "letter",
                  "digit",
                  "punctuation",
                  "symbol"
               ]
            }
         },
         "analyzer": {
            "nGram_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding",
                  "nGram_filter"
               ]
            },
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding"
               ]
            }
         }
      }
   },
   "mappings": {
   
           "movies": {
         "_all": {
            "index_analyzer": "nGram_analyzer",
            "search_analyzer": "whitespace_analyzer"
         },
         "properties": {
            "addToCartUrl": {
               "type": "string",
               "index": "no",
               "include_in_all": false
            },
            "format": {
               "type": "string",
               "index": "not_analyzed"
            },
            "genre": {
               "type": "string",
               "index": "not_analyzed"
            },
            "image": {
               "type": "string",
               "index": "no",
               "include_in_all": false
            },
            "mpaaRating": {
               "type": "string",
               "index": "not_analyzed",
               "include_in_all": false
            },
            "name": {
               "type": "string",
               "index": "not_analyzed"
            },
            "plot": {
               "type": "string",
               "index": "no",
               "include_in_all": false
            },
            "price": {
               "type": "double",
               "include_in_all": false
            },
            "quantityLimit": {
               "type": "integer",
               "include_in_all": false
            },
            "releaseDate": {
               "type": "date",
               "format": "yyyy-MM-dd"
            },
            "sku": {
               "type": "string",
               "index": "not_analyzed"
            },
            "studio": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
   }
}



Step:2-> index document

{
   "studio":"annapurna studios",
   "genre":"Fantasy",
   "mpaaRating":"G",
   "image":"http://images.bestbuy.com/BestBuy_US/en_US/images/musicmoviegame//pdpimages/1008628.jpg",
   "format":"DVD",
   "sku":1008628,
   "plot":"hyderabad",
   "releaseDate":"2013-08-06",
   "quantityLimit":3,
   "name":"The Sword in the Stone (DVD)",
   "addToCartUrl":"http://www.bestbuy.com/site/olspage.jsp?id=pcmcat152200050035&type=category&cmp=RMX&ky=2f5IBQ5bGt0gS6BfPZApsjVfKmtgVmI14&qvsids=1008628",
   "salePrice":21.99
}

Step:3-> query

{
   "size": 10,
   "query": {
      "match": {
         "_all": {
            "query": "anna",
            "operator": "and"
         }
      }
   }
}