我有一个属于Item
模型的Product
模型。我想使用Items
过滤product.platform
。我设置了一个委托方法item.product_platform
并为其输出编制了索引。
Tire / 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
答案 0 :(得分:1)
这是因为elasticsearch会分析并标记您放入索引中的所有内容,除非您另外配置它。
取决于您想要过滤的内容,您有以下几种选择:
:index => 'not_analyzed'
:analyzer => 'keyword'
看看这个问题和喜欢的github问题:using elasticsearch to filter through tags with whitespace