elasticsearch-rails gem - 完成建议者

时间:2014-12-21 11:57:05

标签: ruby-on-rails ruby autocomplete elasticsearch

您好我想使用elasticsearch-rails gem使用completion suggester

我尝试关注ruby客户端documentation,但在使用postman和rails客户端时,我的结果并不相同。

与邮递员合作:

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

结果:

{
    "_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"
                    }
                }
            ]
        }
    ]
}

但不是ruby客户端:

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

结果:

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

我也尝试用完成替换术语但仍然无效:

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

2 个答案:

答案 0 :(得分:1)

这对我有用。

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

答案 1 :(得分:0)

我发现了我的问题:

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