【发布时间】:2016-05-08 06:02:42
【问题描述】:
我无法弄清楚为什么 elasticsearch 不使用 not_analysed 索引进行搜索。我的模型中有以下设置,
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :id
indexes :name, index: 'not_analyzed'
indexes :email, index: 'not_analyzed'
indexes :contact_number
end
end
def as_indexed_json(options = {})
as_json(only: [ :id, :name, :username, :user_type, :is_verified, :email, :contact_number ])
end
而且我在elasticsearch的映射是对的,如下。
{
"users-development" : {
"mappings" : {
"user" : {
"dynamic" : "false",
"properties" : {
"contact_number" : {
"type" : "string"
},
"email" : {
"type" : "string",
"index" : "not_analyzed"
},
"id" : {
"type" : "string"
},
"name" : {
"type" : "string",
"index" : "not_analyzed"
}
}
}
}
}
}
但问题是,当我对未分析的字段(姓名和电子邮件,因为我希望它们不被分析)进行搜索时,它只搜索完整的单词。就像下面的示例一样,它应该返回 John、Johny 和 Tiger,所有 3 条记录。但它只返回 2 条记录。
我正在搜索如下
settings = {
query: {
filtered: {
filter: {
bool: {
must: [
{ terms: { name: [ "john", "tiger" ] } },
]
}
}
}
},
size: 10
}
User.__elasticsearch__.search(settings).records
这就是我在回调after_save 中为我的用户对象创建索引的方式,
User.__elasticsearch__.client.indices.create(
index: User.index_name,
id: self.id,
body: self.as_indexed_json,
)
一些应该匹配的文档
[{
"_index" : "users-development",
"_type" : "user",
"_id" : "670",
"_score" : 1.0,
"_source":{"id":670,"email":"john@monkeyofdoom.com","name":"john baba","contact_number":null}
},
{
"_index" : "users-development",
"_type" : "user",
"_id" : "671",
"_score" : 1.0,
"_source":{"id":671,"email":"human@monkeyofdoom.com","name":"Johny Rocket","contact_number":null}
}
, {
"_index" : "users-development",
"_type" : "user",
"_id" : "736",
"_score" : 1.0,
"_source":{"id":736,"email":"tiger@monkeyofdoom.com","name":"tiger sherof", "contact_number":null}
} ]
请有任何建议。
【问题讨论】:
-
上述查询中的 user_type 是什么?
-
您是如何编制索引的? “约翰”还是“约翰”?你能告诉我们你认为应该匹配的文件吗?
-
@ChintanShah25 添加了应该匹配的文档
-
@ChintanShah25 感谢您的建议,实际上它只是搜索部分单词,例如名称是 John Baba 和 Johny Rocket,查询字符串是 John -- 它应该返回 2 条记录,但它只返回一条记录,即约翰。任何建议谢谢
-
查看您的文档,您不会得到任何结果。你的要求是什么?什么应该匹配,什么不应该匹配?还有 johny 会如何匹配 john?
标签: ruby-on-rails-4 elasticsearch elasticsearch-rails elasticsearch-model