使用轮胎宝石为特定领域设置Elasticsearch分析器

sch*_*udu 2 ruby-on-rails elasticsearch tire

我有一个Resource对象,它包含以下字段:title,descriptionbody.

使用轮胎宝石我想使用标准分析仪来标题描述,但对于身体领域我想使用雪球分析仪.

所以在我的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
Run Code Online (Sandbox Code Playgroud)

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

curl -XGET localhost:9200/example/_search?q=body:cook
Run Code Online (Sandbox Code Playgroud)

它将返回烹饪,煮熟等.

但是,我希望能够传递以下查询

curl -XGET localhost:9200/example/_search?q=cooking
Run Code Online (Sandbox Code Playgroud)

我想elasticsearch搜索词烹饪标题描述字段和搜索单词厨师身体.

这可能吗?而且因为我是这个搜索游戏的新手,这是一个好主意吗?或者我只是在火坑里玩杂耍的汽油罐头?

imo*_*tov 6

如果未在查询中指定字段,则会搜索_all字段._all字段包含所有字段的副本,它使用默认分析器.可以将默认分析仪更改为滚雪球,如下所述:如何设置轮胎弹性搜索的默认分析仪?并且还可以控制_all字段中包含哪些字段.