小编典典

Elastic search / tire:如何映射到关联属性?

elasticsearch

我正在使用Tire进行elasticsearch。在我的应用程序中,我有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

从以下问题

我不知道该如何使自己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, 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
      }
    }
  }

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

谢谢。


阅读 307

收藏
2020-06-22

共1个答案

小编典典

这让我很头疼,所以我结束了去Ransack的工作,这简直太容易弄清楚和使用了。

2020-06-22