Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "suggest" in elasticsearch pyes?

How to use the "suggest" feature in pyes? Cannot seem to figure it out due to poor documentation. Could someone provide a working example? None of what I tried appears to work. In the docs its listed under query, but using:

query = Suggest(fields="fieldname")
connectionobject.search(query=query)
like image 699
Rolando Avatar asked Jul 07 '14 02:07

Rolando


2 Answers

Since version 5:

_suggest endpoint has been deprecated in favour of using suggest via _search endpoint. In 5.0, the _search endpoint has been optimized for suggest only search requests.

(from https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-suggesters.html)

Better way to do this is using search api with suggest option

from elasticsearch import Elasticsearch
es = Elasticsearch()

text = 'ra'
suggest_dictionary = {"my-entity-suggest" : {
                      'text' : text,
                      "completion" : {
                          "field" : "suggest"
                      }
                    }
                  }

query_dictionary = {'suggest' : suggest_dictionary}

res = es.search(
    index='auto_sugg',
    doc_type='entity',
    body=query_dictionary)
print(res)

Make sure you have indexed each document with suggest field

sample_entity= {
            'id' : 'test123',
            'name': 'Ramtin Seraj',
            'title' : 'XYZ',    
            "suggest" : {
                "input": [ 'Ramtin', 'Seraj', 'XYZ'],
                "output": "Ramtin Seraj",
                "weight" : 34   # a prior weight 
            }
          }
like image 129
Ramtin M. Seraj Avatar answered Oct 17 '22 06:10

Ramtin M. Seraj


Here is my code which runs perfectly.

from elasticsearch import Elasticsearch
es = Elasticsearch()

text = 'ra'
suggDoc = {
           "entity-suggest" : {
                'text' : text,
                "completion" : {
                    "field" : "suggest"
                }
            }
        }

res = es.suggest(body=suggDoc, index="auto_sugg", params=None)
print(res)

I used the same client mentioned on the elasticsearch site here
I indexed the data in the elasticsearch index by using completion suggester from here

like image 5
Shams Avatar answered Oct 17 '22 06:10

Shams