我将标记存储为keywords数组
...
Tags: {
type: "keyword"
},
...结果数组如下所示:
Tags: [
"windows",
"opengl",
"unicode",
"c++",
"c",
"cross-platform",
"makefile",
"emacs"
]我想,因为我使用的是keyword类型,所以我可以很容易地进行精确的搜索,因为它不应该使用任何分析器。
显然我错了!这给出了我的结果:
body.query.bool.must.push({term: {"_all": "c"}}); # 38 results但这不是:
body.query.bool.must.push({term: {"_all": "c++"}}); # 0 results尽管很明显存在这个标记的实例,如上所述。
如果我使用body.query.bool.must.push({match: {"_all": search}}); (使用match而不是term),那么"c“和"c++”返回完全相同的结果,这也是错误的。
发布于 2017-07-29 05:57:22
这里的问题是您使用的是_all - Field,它使用分析器(默认情况下是标准的)。对你的数据做一个小测试,以确保:
测试1:
curl -X POST http://127.0.0.1:9200/script/test/_search \
-d '{
"query": {
"term" : { "_all": "c++"}
}
}'测试2:
curl -X POST http://127.0.0.1:9200/script/test/_search \
-d '{
"query": {
"term" : { "tags": "c++"}
}
}'在我的测试中,第二个查询返回文档,第一个不返回。
您真的需要使用多个字段进行搜索吗?如果是这样的话,你可以覆盖_all字段的默认分析器--为了快速测试,我设置了一个索引,设置如下:
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"test" : {
"_all" : {"type" : "string", "index" : "not_analyzed", "analyzer" : "keyword"},
"properties": {
"tags": {
"type": "keyword"
}
}
}
}
}或者,您可以创建Custom _all Field。
像Multi Field query这样的允许定义要搜索的字段列表的解决方案,其行为方式更像您使用body.query.bool.must.push({match: {"_all": search}});的示例。
https://stackoverflow.com/questions/45381793
复制相似问题