我的 UI 上有很多文档过滤器(日期范围、复选框、输入字段),因此查询是动态生成的 - 这就是为什么我决定创建一个 bool 查询,并将所有内容推送到 must
大批。这是我的请求的示例:
const {
body: {
hits
}
} = await esclient.search({
from: filterQuery.page || 0,
size: filterQuery.limit || 1000,
index,
body: query
});
复选框(我在 bool.should
数组中使用了额外的 must
)和日期范围工作完美,但术语/匹配过滤根本不起作用:
{
"query": {
"bool": {
"must": [
{"match": { "issueNumber": "TEST-10" }}
]
}
}
}
如果我更改match
,上面的查询会为我提供包含“TEST”(及其分数)的索引中的所有文档。至term
- 它返回一个空数组。
由于我的字段属于“文本”类型,因此我也尝试过 filter
查询 - ES 仍然给出所有带有 'TEST' 单词的文档:
{
"query": {
"bool": {
"must": [
{
"bool": {
"filter": {
"match": {"issueNumber": "TEST-10"}
}
}
}
]
}
}
}
这就是我的点击效果:
{
"_index" : "test_elastic",
"_type" : "_doc",
"_id" : "bj213hj2gghg213",
"_score" : 0.0,
"_source" : {
"date" : "2019-11-26T13:27:01.586Z",
"country" : "US",
"issueNumber" : "TEST-10",
}
有人可以告诉我如何在复杂查询中正确过滤文档吗?
这是我的索引的结构:
{
"test_elasticsearch" : {
"aliases" : { },
"mappings" : {
"properties" : {
"country" : {
"type" : "text"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1574759226800",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "PTDsdadasd-ERERER",
"version" : {
"created" : "7040299"
},
"provided_name" : "logs"
}
}
}
}
最佳答案
好的,问题是您的 issueNumber
字段的类型不正确,如果您的目标是,它应该是 keyword
而不是 text
对其进行精确搜索。对于国家
来说也是如此。像这样修改您的映射:
"properties" : {
"country" : {
"type" : "keyword"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "keyword"
}
}
然后重新索引您的数据,您的查询就会起作用。
关于javascript - 如何过滤掉复杂查询中的文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59064728/