1

我有一个Item属于模型的Product模型。我想Items使用product.platform. 我设置了一个委托方法item.product_platform并索引了它的输出。

似乎轮胎 / ES 仅按一个单词过滤,并且仅使用小写字母。所以:

item.product_platform = "Nintendo Wii"
filter :term, product_platform: "wii"          # works
filter :term, product_platform: "nintendo"     # works
filter :term, product_platform: "Wii"          # does not work
filter :term, product_platform: "Nintendo"     # does not work
filter :term, product_platform: "nintendo wii" # does not work
filter :term, product_platform: "Nintendo Wii" # does not work

但是当我传入一个精确的字符串时,我希望它返回一个结果,比如“Nintendo Wii”。

这是我的item.rb

  belongs_to :product
  delegate :platform, to: :product, prefix: true

  def self.search(params)
    tire.search do
      query { string params[:query] } if params[:query].present?

      filter :term, product_platform: params[:platform] if params[:platform]
    end
  end

  def to_indexed_json
    to_json(include: :product, methods: :product_platform)
  end

这是我的映射:

  mapping date_detection: false do
    indexes :id, type: 'integer'
    indexes :user_id, type: 'integer'
    indexes :product_id, type: 'integer'
    indexes :product_platform
  end
4

1 回答 1

1

这是因为 elasticsearch 会分析和标记您放入索引中的所有内容,除非您另行配置。

根据您想要过滤的内容,您有几个选项:

  1. 使用:index => 'not_analyzed'
  2. 使用:analyzer => 'keyword'

看看这个问题和喜欢的 github 问题:使用弹性搜索过滤带有空格的标签

于 2013-07-02T18:43:10.327 回答