我正在使用带有 mongoid 的轮胎 (https://github.com/karmi/tire)。这是我的模型定义:
class SomethingWithTag
include Mongoid::Document
include Mongoid::Timestamps
field :tags_array, type: Array
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :tags_array, type: :array, index: :not_analyzed
end
end
假设我有一个文档 {tags_array: ["hello world"]}。然后以下查询可以正常工作:
SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello"] }
SomethingWithTag.tire.search { filter :terms, :tags_array => ["world"] }
SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello", "world"] }
但是以下内容不会返回任何结果:
SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello world"] }
我应该怎么做才能让它发挥作用?
编辑:这里有一小段要测试的代码:http://pastebin.com/n1rUtK3e
最佳答案
问题已解决:
使用 keyword
分析器来分析 tags_array
属性:
class SomethingWithTag
# ...
mapping do
indexes :tags_array, analyzer: 'keyword'
end
end
关于ruby - 使用elasticsearch过滤带有空格的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11822019/