加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 弹性搜索/轮胎:如何映射到关联属性?

发布时间:2020-12-17 02:36:51 所属栏目:百科 来源:网络整理
导读:我正在使用 Tire进行弹性搜索.在我的应用程序中,我有2个模型;价格和产品. 我正在尝试搜索我的Price类并使用它所属的产品:搜索字段的name属性.现在,如果我有一个名为产品1的产品并输入“pro”,“prod”或“duct”,则不会出现任何结果.但输入“product”或“P
我正在使用 Tire进行弹性搜索.在我的应用程序中,我有2个模型;价格和产品.

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

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

当我认为它应该是:

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

从这个问题来看:ElasticSearch mapping doesn’t work

我不知道如何让我的params [:query]只接受product.name.我尝试使用:string params [:query],default_field:“product.name”但是没有用.

我不想使用_all字段.

这是我的代码:

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

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

谢谢.

解决方法

当你这样做

query   { string params[:query] }

并且查询未指定字段,您正在搜索magic _all字段.此字段具有自己的分析器设置 – 它不会使用您为名称字段设置的分析器设置.

您可以配置_all字段以使用分析器,更改默认分析器或更改查询以专门查询名称字段,例如

query { string params[:query],:default_field => 'name'}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读