【问题标题】:Elastic Search/Tire: How to map to association attribute?弹性搜索/轮胎:如何映射到关联属性?
【发布时间】:2012-08-17 19:53:55
【问题描述】:

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

我正在尝试搜索我的 Price 类并将其所属的 Product 的 :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 mapping doesn't work

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

我不想使用_all 字段。

这是我的代码:

价格.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
      }
    }
  }

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

谢谢。

【问题讨论】:

  • 我将不得不在不久的将来解决同样的问题。所以我会更新我的进度,除非你打败我!
  • @Alain 我还在做这个,所以如果你知道如何让它工作,请告诉我。你的进步会很有帮助。

标签: ruby-on-rails ruby ruby-on-rails-3.1 elasticsearch tire


【解决方案1】:

当你这样做时

 query   { string params[:query] } 

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

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

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

【讨论】:

  • 我在default_field => 'name' 上找不到任何文档。你是在哪里发现这个的?如果我尝试使用 product.name 字段。怎么会这样?
  • 这对我不起作用。我到product.name 的映射是否正确?我认为这就是它不起作用的原因。
  • 哦,您可能想将默认字段设置为 product.name,但没有发现映射的嵌套性
  • 我的意思是使用文本类型而不是字符串类型的查询。值得一探究竟,看看 elasticsearch 是否真的在使用您提供的映射(查看设置和 mappin api)
  • 我会尝试删除索引,然后让轮胎重新创建它
【解决方案2】:

这让我非常头疼,所以我最终去了Ransack,这非常容易理解和使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多