-->

弹性搜索/轮胎:如何映射到关联属性?(Elastic Search/Tire: How to map

2019-07-31 03:27发布

我使用的轮胎的弹性搜索。 在我的应用程序有2种型号; 价格和产品。

我试图寻找我的舱位价格,并用它所属的产品:name属性为搜索字段。 现在,如果我有一个产品叫Product 1 ,键入“亲”,“刺”或“管”,没有结果上来。 但输入“产品”或“产品”显示的结果。 我认为,问题出在我的映射。 我看着查询及其:

...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领域。

这里是我的代码:

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

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

谢谢。

Answer 1:

当你做

 query   { string params[:query] } 

和查询不指定字段,您正在搜索的神奇_all领域。 该场有自己的分析仪设置 - 它不会使用你已经设置你的名字视野中的一个。

您可以配置_all场使用您的分析仪,更改默认的分析或更改您的查询,查询name专门领域,例如

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


Answer 2:

这是多大的头痛所以我最终去洗劫这是疯狂更容易找出并使用。



文章来源: Elastic Search/Tire: How to map to association attribute?