diff --git a/README.md b/README.md index e6dc67f..9438c10 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,23 @@ PUT products { "settings": { "number_of_shards": 1, - "number_of_replicas": 0 + "number_of_replicas": 0, + "analysis": { + "analyzer": { + "autocomplete_analyzer" : { + "type" : "custom", + "tokenizer": "standard", + "filter": ["lowercase", "autocomplete_filter"] + } + }, + "filter": { + "autocomplete_filter": { + "type": "edge_ngram", + "min_gram": 2, + "max_gram": 10 + } + } + } } } @@ -49,10 +65,24 @@ PUT products/product/_mapping { "properties": { "name": { - "type": "text" + "type": "text", + "fields": { + "autocomplete": { + "type": "text", + "analyzer": "autocomplete_analyzer", + "search_analyzer": "standard" + } + } }, "description": { - "type": "text" + "type": "text", + "fields": { + "autocomplete": { + "type": "text", + "analyzer": "autocomplete_analyzer", + "search_analyzer": "standard" + } + } }, "quantity": { "type": "long" @@ -75,7 +105,14 @@ PUT products/product/_mapping "type": "long" }, "name": { - "type": "keyword" + "type": "keyword", + "fields": { + "autocomplete": { + "type": "text", + "analyzer": "autocomplete_analyzer", + "search_analyzer": "standard" + } + } } } } diff --git a/services/productSearchService.js b/services/productSearchService.js index c4f9918..55d9920 100644 --- a/services/productSearchService.js +++ b/services/productSearchService.js @@ -2,8 +2,7 @@ var db = require('./../libraries/elasticsearch'); exports.getRecords = function(params, callback) { body = { - query: { - }, + query: {}, size: 50 }; @@ -17,8 +16,9 @@ exports.getRecords = function(params, callback) { if (params.keyword) { body.query.query_string = { - "fields": ["name^4", "description^2", "categories.name^3"], - "query": params.keyword + "fields": ["name.autocomplete^10", "description.autocomplete^2", "categories.name.autocomplete^3"], + "query": params.keyword, + "default_operator": "AND" }; }