2

我有一个 Resource 对象,它具有以下字段:titledescriptionbody

使用轮胎宝石,我想使用标准分析器进行标题描述,但对于身体领域,我想使用雪球分析器。

所以在我的resource.rb文件中,我有以下代码:

mapping do
  indexes :title, type: 'string', :index => :not_analyzed, :store => true
  indexes :description, type: 'string'
  indexes :body, type: 'string', :analyzer => 'snowball'
end

def self.search(params)
  search_query = params[:query]

  tire.search(page: params[:page], per_page: 15) do |s|
    s.query { string params[:query], default_operator: "AND"} if params[:query].present?
    # do i need another line here specifically for description?
    #s.query { string "body:#{params[:query]}", analyzer: 'snowball'} if params[:query].present?
  end
end

我将映射中的分析器设置为雪球。我验证这是否有效,因为如果我运行以下命令:

curl -XGET localhost:9200/example/_search?q=body:cook

它将返回烹饪、煮熟等。

但是我希望能够通过以下查询

curl -XGET localhost:9200/example/_search?q=cooking

我希望elasticsearch在标题描述字段中搜索单词cooking ,并在body中搜索单词cook

这可能吗?而且因为我是整个搜索游戏的新手,这甚至是个好主意吗?还是我只是一个在火坑上玩弄汽油罐的人?

4

1 回答 1

6

When you are not specifying a field in your query the _all field is searched. The _all field contains copy of all fields and it is using the default analyzer. It's possible to change the default analyzer to snowball as it's explained in How do I set the default analyzer for elastic search with tire? And it's also possible to control which fields are included into the _all field.

于 2012-05-30T12:16:12.113 回答