javascript - 如何过滤掉复杂查询中的文本字段?
问题描述
我的 UI 上有很多文档过滤器(日期范围、复选框、输入字段),因此查询是动态生成的——这就是我决定创建一个布尔查询并将所有内容推送到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" }}
]
}
}
}
上面的查询为我提供了索引中包含“TEST”(及其分数)的所有文档,如果我更改match
为term
- 它返回一个空数组。
由于我的字段属于“文本”类型,我也尝试过filter
查询 - ES 仍然提供所有带有“测试”字样的文档:
{
"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
。对于country
. 像这样修改你的映射:
"properties" : {
"country" : {
"type" : "keyword"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "keyword"
}
}
然后重新索引您的数据,您的查询将起作用。
推荐阅读
- xamarin.forms - 如何在 xamarin Forms 中监控蓝牙设备连接状态?
- javascript - 如何转到下一张幻灯片 jQuery
- android - 向上滚动时显示工具栏 - 具有多个捕捉点的 CoordinatorLayout
- php - 默认显示一个 symfony 表单块,但我需要零个块
- java - 如果可以通过反射绕过访问修饰符,它们的目的是什么?
- azure-devops-rest-api - 使用 powershell 脚本获取测试套件的默认测试人员
- r - 在 R 中将数字 1000 格式化为 1k,将 1000000 格式化为 1m 等
- performance - MoviePy:减慢视频的渲染速度
- angular8 - 电子邮件和密码的正则表达式验证不起作用
- tensorflow - 在 {TF 2.0.0-beta1 上使用 tflite_convert 时出现“未知(自定义)损失函数”;Keras} 模型