弹性搜索/轮胎:如何映射到关联属性? [英] Elastic Search/Tire: How to map to association attribute?

查看:106
本文介绍了弹性搜索/轮胎:如何映射到关联属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用轮胎进行弹性搜索。在我的应用程序中,我有2个模型;价格和产品。



我正在搜索我的Price类,并使用它所属的产品:name 属性为搜索字段。现在,如果我有一个名为 Product 1 并输入pro,prod或duct的产品,则不会显示任何结果。但输入产品或产品显示结果。我相信问题在于我的映射。我查看了这个查询:

  ... localhost:3000 / search / results?utf8 =%E2%9C% 93& query = product 

当我认为应该是:

  ... localhost:3000 / search / results?utf8 =%E2%9C%93& query:product = product 

根据此问题: ElasticSearch映射不起作用



我不知道如何使我的 params [:query] 只能使用 product.name 。我试图使用: string params [:query],default_field:product.name但是没有工作。



我不想使用 _all 字段。



这是我的代码: p>

Price.rb

 包括轮胎: :模型::搜索
包括轮胎::模型::回调

def self.search(params)
tire.search(load:true,page:params [:page ],per_page:20)do
query do
boolean do
must {string params [:query]}如果params [:query] .present?
must {term:private,false}
end
end
sort do
by:date,desc
由:amount,asc
end
end
end

def to_indexed_json
to_json(include:{product:{only:[:name]}})
end

mapping do
索引:id,类型:integer
索引:金额,类型:string,索引:not_analyzed
索引:日期,键入:date,index:not_analyzed
索引:private,type:boolean
索引:product do
索引:名称,类型:string custom_analyzer
end
end

设置分析:{
analyzer:{
custom_analyzer:{
tokenizer:[whitespace,小写],
过滤器:[ngram_filter,word_delimiter_filter],
类型:自定义
}
},
过滤器:{
ngram_filter:{
typ e:nGram,
min_gram:2,
max_gram:15
}
},
过滤器:{
word_delimiter_filter:{
键入:word_delimiter,
generate_word_parts:true,
generate_number_parts:true,
preserve_original:true,
stem_english_possessive:true
}
}
}

任何人都有任何建议或知道如何设置查询字段以使用产品名称只有?



谢谢。

解决方案

令人头疼,所以我结束了 Ransack ,这个比较容易弄清楚和使用。 p>

I'm using Tire for Elastic Search. In my application I have 2 models; Price and Product.

I'm trying to search my Price class and use the Product it belongs to's :name attribute for the search field. Right now if I had a product called Product 1 and type in "pro", "prod" or "duct", no results come up. But typing "product" or "Product" shows the results. I believe the problem lies in my mapping. I looked at the query and its:

...localhost:3000/search/results?utf8=%E2%9C%93&query=product

When I think it should be:

...localhost:3000/search/results?utf8=%E2%9C%93&query:product=product

Judging by this question: ElasticSearch mapping doesn't work

I don't know how to make my params[:query] to take the product.name only though. I tried to use: string params[:query], default_field: "product.name" but that didn't work.

I don't want to use the _all field.

Here is my code:

Price.rb

  include Tire::Model::Search
  include Tire::Model::Callbacks

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 20) do
       query do
         boolean do
            must { string params[:query] } if params[:query].present?
            must { term :private, false }
         end
       end
       sort do
         by :date, "desc"
         by :amount, "asc"
       end
    end
  end

  def to_indexed_json
    to_json( include: { product: { only: [:name] } } )
  end

  mapping do
    indexes :id,        type: "integer"
    indexes :amount,    type: "string", index: "not_analyzed"
    indexes :date,      type: "date",   index: "not_analyzed"
    indexes :private,   type: "boolean"
    indexes :product do
      indexes :name, type: "string", analyzer: "custom_analyzer"
    end
  end

  settings analysis: {
    analyzer: {
      custom_analyzer: {
        tokenizer: [ "whitespace", "lowercase" ],
        filter: [ "ngram_filter", "word_delimiter_filter" ],
        type: "custom"
      }
    },
    filter: {
      ngram_filter: {
        type: "nGram",
        min_gram: 2,
        max_gram: 15
      }
    },
    filter: {
      word_delimiter_filter: {
        type: "word_delimiter",
        generate_word_parts: true,
        generate_number_parts: true,
        preserve_original: true,
        stem_english_possessive: true
      }
    }
  }

So does anyone have any suggestions or know how to set the query field to use the Product name only?

Thank you.

解决方案

This was to much of a headache so I ended going to Ransack which was insanely much easier to figure out and use.

这篇关于弹性搜索/轮胎:如何映射到关联属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆