弹性搜索/轮胎:如何映射到关联属性?

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

我正在尝试搜索我的Price类并使用它所属的产品:name搜索字段的:name属性。 现在,如果我有一个名为Product 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映射不起作用

我不知道如何让我的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 } } } 

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

谢谢。

当你这样做

  query { string params[:query] } 

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

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

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

这让我很头疼,所以我结束了去Ransack ,这更容易弄清楚和使用。