Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch-rails gem - completion suggester

Hi I want to use the completion suggester using the elasticsearch-rails gem.

I tried to follow the ruby client documentation but I don't have the same results when using postman and the rails client.

Works with postman:

{
    "suggestions" : {
        "text" : "s",
        "completion" : {
            "field" : "suggest"
        }
    }
}

Results:

{
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "suggestions": [
        {
            "text": "s",
            "offset": 0,
            "length": 3,
            "options": [
                {
                    "text": "superman",
                    "score": 1,
                    "payload": {
                        "id": 922,
                        "tumb_img": "/user/avatar/20/thumb_img.jpg"
                    }
                }
            ]
        }
    ]
}

But not with the ruby client:

Article.__elasticsearch__.client.suggest(:index => '', :body => {
        :suggestions => {
            :text => "s",
            :term => {
                :field => 'suggest'
            }
        }
    })

Results:

{
    "_shards": {
        "total": 11,
        "successful": 11,
        "failed": 0
    },
    "suggestions": [
        {
            "text": "s",
            "offset": 0,
            "length": 1,
            "options": []
        }
    ]
}

I also tried replacing term with completion but still doesn't work:

Article.__elasticsearch__.client.suggest(:index => '', :body => {
        :suggestions => {
            :text => "s",
            :completion => {
                :field => 'suggest'
            }
        }
    })
like image 803
user2037696 Avatar asked Nov 28 '25 10:11

user2037696


1 Answers

Here what works for me.

Elasticsearch::Model.client.suggest index: 'articles', 
                       body: { 
                              suggestion: { 
                                   text: 's', 
                                   completion: { 
                                       field: 'suggest' 
     #suggest or any field that has mapping with type: 'completion', payloads: true
                                               } 
                                          } 
                              }
like image 165
Misha Avatar answered Nov 30 '25 01:11

Misha