【发布时间】:2017-06-17 22:03:55
【问题描述】:
大家好,我是弹性搜索的新手,但我已经阅读了基本的 ElasticSearch 5.1 文档。
一行的问题:
搜索成功,但过滤器无法正常工作。
映射数据类型
{
"properties": {
"title": {"type": "string"},
"description": {"type": "string"},
"slug": {"type": "string"},
"course_type": {"type": "string", "index" : "not_analyzed"},
"price": {"type": "string"},
"categories": {"type": "keyword", "index" : "not_analyzed"},
"tags": {"type" : "keyword"},
// "tags": {"type" : "keyword", "index" : "not_analyzed"},
"status": {"type" : "string","index" : "not_analyzed"},
}
}
正如@Darth_Vader 所述,我也尝试了映射。以下是我的映射
索引中的文档 (Req-1)
....
{
"_index": "learnings",
"_type": "materials",
"_id": "582d9xxxxxxxx9b27fab2c",
"_score": 1,
"_source": {
"title": "Mobile Marketing",
"slug": "mobile-marketing",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eleifend hendrerit vehicula.",
"categories": [
"Digital Marketing"
],
"tags": [
"digital-marketing",
"mobile-marketing"
],
"status": "published"
}
},
...
和上面一样,我在索引中有一百个文档
我正在使用的搜索查询已满
"query": {
"bool": {
"must": {
"multi_match" : {
"query" : "mobile",
"fields" : [ "title^5", "tags^4", "categories^3" ],
"operator": "and"
}
},
"filter": {
"bool" : {
"must" : [
{"term" : {"status": "published"} }
]
}
}
}
}
在上述查询中,最重要的搜索条件/过滤器是
{"term" : {"status": "published"} }。每个搜索结果都必须满足这个 要求。现在从结果列表中,我想过滤更多。因此,假设我只想获得具有
mobile-marketing作为tag的documents。我的文档 (Req-1) 有这个tag(mobile-marketing)
现在的问题是:
如果我修改我的 Search Query 并添加我所需的 filter,如下所示:我得到 NO 搜索结果 (hits = 0)即使我的文档 (Req-1) 具有 mobile-marketing 作为 tag
"filter": {
"bool" : {
"must" : [
{"term" : {"status": "published"} },
{"term" : {"tags": "mobile-marketing"} }
]
}
}
但是如果我更改过滤器
{"tags": "mobile-marketing"}TO{"tags": "mobile"},我会得到所需的文档 (Req-1) 作为结果.
我想使用此过滤器获取相同的文档:{"tags": "mobile-marketing"}。那么我在哪里做错了?
我的搜索查询需要进行哪些修改?
谢谢
【问题讨论】:
-
[对于有类似问题的人]。上述过程和相关代码有效,请确保在映射和其他相关操作时使用正确的索引名称和索引类型
标签: node.js elasticsearch