i trying implement partial matching using ngrams in elasticsearch not getting expected results out of it.
following link:-
https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html
have done things mentioned in link. dataset contains 3 fields i.e id,name,age.
here mapping , setting of my_index
get /my_index/_settings { "my_index": { "settings": { "index": { "creation_date": "1433249154544", "uuid": "hkxhvnqarvmji31xk92pva", "number_of_replicas": "1", "analysis": { "filter": { "autocomplete_filter": { "type": "edge_ngram", "min_gram": "1", "max_gram": "20" } }, "analyzer": { "autocomplete": { "type": "custom", "filter": [ "lowercase", "autocomplete_filter" ], "tokenizer": "standard" } } }, "number_of_shards": "1", "version": { "created": "1040499" } } } } }
get /my_index/_mapping/my_type { "my_index": { "mappings": { "my_type": { "properties": { "@timestamp": { "type": "date", "format": "dateoptionaltime" }, "@version": { "type": "string" }, "age": { "type": "long" }, "host": { "type": "string" }, "id": { "type": "string" }, "message": { "type": "string" }, "name": { "type": "string", "index_analyzer": "autocomplete", "search_analyzer": "standard" }, "path": { "type": "string" }, "type": { "type": "string" } } } } } }
query elastic search :-
get /my_index/my_type/_search { "query": { "match": { "name": { "query": "raman r" } } } }
according me results should display "raman ram" showing other results well:-
{ "took": 13, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 9, "max_score": 2.6631343, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "2", "_score": 2.6631343, "_source": { "message": [ "2,raman,23" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.041z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "2", "name": "raman", "age": 23 } }, { "_index": "my_index", "_type": "my_type", "_id": "10", "_score": 1.8003473, "_source": { "message": [ "10,raman ram,43" ], "@version": "1", "@timestamp": "2015-06-02t13:11:03.455z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "10", "name": "raman ram", "age": 43 } }, { "_index": "my_index", "_type": "my_type", "_id": "1", "_score": 0.26245093, "_source": { "message": [ "1,ram,342" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.040z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "1", "name": "ram", "age": 342 } }, { "_index": "my_index", "_type": "my_type", "_id": "3", "_score": 0.26245093, "_source": { "message": [ "3,ramayan,23" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.041z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "3", "name": "ramayan", "age": 23 } }, { "_index": "my_index", "_type": "my_type", "_id": "4", "_score": 0.26245093, "_source": { "message": [ "4,ramaram,231" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.041z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "4", "name": "ramaram", "age": 231 } }, { "_index": "my_index", "_type": "my_type", "_id": "5", "_score": 0.26245093, "_source": { "message": [ "5,rampy,1" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.041z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "5", "name": "rampy", "age": 1 } }, { "_index": "my_index", "_type": "my_type", "_id": "6", "_score": 0.26245093, "_source": { "message": [ "6,ration,11" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.041z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "6", "name": "ration", "age": 11 } }, { "_index": "my_index", "_type": "my_type", "_id": "7", "_score": 0.26245093, "_source": { "message": [ "7,rita,42" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.042z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "7", "name": "rita", "age": 42 } }, { "_index": "my_index", "_type": "my_type", "_id": "8", "_score": 0.26245093, "_source": { "message": [ "8,roni,45" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.050z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "8", "name": "roni", "age": 45 } } ] } }
i correct results when use "minimum_should_match": "100%" while querying.
get /my_index/my_type/_search { "query": { "match": { "name": { "query": "raman r", "minimum_should_match": "100%" } } } }
gave me better result although ranking not right:
{ "took": 4, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 2, "max_score": 2.6631343, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "2", "_score": 2.6631343, "_source": { "message": [ "2,raman,23" ], "@version": "1", "@timestamp": "2015-06-02t13:07:18.041z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "2", "name": "raman", "age": 23 } }, { "_index": "my_index", "_type": "my_type", "_id": "10", "_score": 1.8003473, "_source": { "message": [ "10,raman ram,43" ], "@version": "1", "@timestamp": "2015-06-02t13:11:03.455z", "type": "my_type", "host": "shubham-virtualbox", "path": "/home/shubham/sample.csv", "id": "10", "name": "raman ram", "age": 43 } } ] } }
don't know whether approach correct or not tell me if alternative there this
Comments
Post a Comment