【问题标题】:rails - Elasticsearch completion suggester and search APIrails - Elasticsearch 完成建议器和搜索 API
【发布时间】:2015-05-07 05:24:00
【问题描述】:

我正在使用搜索 API,现在需要添加 completion suggester,我正在使用 elasticsearch-rails gem。

当我搜索文章时,一切正常 http://localhost:9200/articles/_search

  "query": {
    "multi_match": {
      "query": "test",
      "fields": [
        "title", "tags", "content"
      ]
    }
  }
}

但由于我已经实现了完成建议,我必须编辑 as_indexed_json 以使其工作,但现在搜索 API 不再工作,只有建议。

这是我的文章模型:

  def self.search(query)
    __elasticsearch__.search(
        {
            query: {
                multi_match: {
                    query: query,
                    fields: ['title', 'content', 'tags']
                }
            }
        })
end

      def self.suggest(query)
        Article.__elasticsearch__.client.suggest(:index => Article.index_name, :body => {
            :suggestions => {
                :text => query,
                :completion => {
                    :field => 'suggest'
                }
            }
        })
      end

      def as_indexed_json(options={}) 
       {
            :name => self.title,
            :suggest => {
                :input => self.title,
                :output => self.title,
                :payload => {
                    :content => self.content,
                    :tags => self.tags,
                    :title => self.title
                }
            }
        }.as_json
      end

是否可以让 _search 和 _suggest 与同一个模型一起工作?

【问题讨论】:

  • 那么,您的映射看起来如何?

标签: ruby-on-rails autocomplete elasticsearch


【解决方案1】:

我只是深入研究弹性搜索,但据我了解,您可以在序列化程序函数中添加修改之前的内容并重新创建索引,它们将在数据库中很好地一起存在。例如:

def as_indexed_json(options={}) 
       {
           :name => self.title,
            :suggest => {
                :input => self.title,
                :output => self.title,
                :payload => {
                    :content => self.content,
                    :tags => self.tags,
                    :title => self.title
                }
            }
        }.as_json.merge(self.as_json) # or the customized hash you used

为避免索引冗余,您可以查看aliases and routing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 2022-01-22
    • 1970-01-01
    • 2023-04-08
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多