Rolando
Rolando

Reputation: 62674

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)

Upvotes: 7

Views: 3989

Answers (2)

Shams
Shams

Reputation: 3677

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

Upvotes: 5

Ramtin M. Seraj
Ramtin M. Seraj

Reputation: 705

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 
            }
          }

Upvotes: 7

Related Questions