使用弹性搜索来过滤带有空格的标签 [英] using elasticsearch to filter through tags with whitespace
本文介绍了使用弹性搜索来过滤带有空格的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
SomethingWithTag.tire.search {filter:terms,:tags_array => [hello]}
SomethingWithTag.tire.search {filter:terms,:tags_array => [world]}
SomethingWithTag.tire.search {filter:terms,:tags_array => [你好,世界]}
但是以下内容不返回任何结果: / p>
SomethingWithTag.tire.search {filter:terms,,tags_array => [你好世界]}
我该怎么做才能使它工作?
编辑:这是一小段代码来测试: http://pastebin.com/ n1rUtK3e
解决方案
问题解决于:
使用关键字
分析器为 tags_array
属性:
class SomethingWithTag
#...
mapping do
索引:tags_array,analyzer:'keyword'
end
end
I am using tire (https://github.com/karmi/tire) with mongoid. Here is my model definition:
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
Say I have a document {tags_array: ["hello world"]}. Then the following queries work fine:
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"] }
But the following doesn't return any results:
SomethingWithTag.tire.search { filter :terms, :tags_array => ["hello world"] }
What should I do to make it work?
Edit: here's a small piece of code to test: http://pastebin.com/n1rUtK3e
解决方案
Issue solved at :
Use the keyword
analyzer for the tags_array
property:
class SomethingWithTag
# ...
mapping do
indexes :tags_array, analyzer: 'keyword'
end
end
这篇关于使用弹性搜索来过滤带有空格的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文