elasticsearch - Elasticsearch 在给定过滤器后从数组字段返回唯一字符串
问题描述
如何从弹性搜索记录中获取具有给定前缀的所有 id 的所有值并使它们唯一。
记录
PUT items/1
{ "ids" : [ "apple_A", "orange_B" ] }
PUT items/2
{ "ids" : [ "apple_A", "apple_B" ] }
PUT items/3
{ "ids" : [ "apple_C", "banana_A" ] }
我需要的是找到给定前缀的所有唯一 ID,例如,如果输入是苹果,则 ID 的输出应该是 ["apple_A", "apple_B", "apple_C"]
到目前为止,我尝试使用术语聚合,通过以下查询,我能够过滤掉具有给定前缀的 id 的文档,但在聚合中它将返回文档的所有 id 部分。
{
"aggregations": {
"filterIds": {
"filter": {
"bool": {
"filter": [
{
"prefix": {
"ids.keyword": {
"value": "apple"
}
}
}
]
}
},
"aggregations": {
"uniqueIds": {
"terms": {
"field": "ids.keyword",
}
}
}
}
}
}
如果我们将前缀输入作为苹果,它将返回聚合列表为 [ "appleA", "orange_B", "apple_B","apple_C", "banana_A"]。基本上返回所有具有匹配过滤器的 id。
是否只获取与数组中的前缀匹配的 ID,而不是文档数组中的所有 ID?
解决方案
include
您可以使用参数限制返回值:
POST items/_search
{
"size": 0,
"aggregations": {
"filterIds": {
"filter": {
"bool": {
"filter": [
{
"prefix": {
"ids.keyword": {
"value": "apple"
}
}
}
]
}
},
"aggregations": {
"uniqueIds": {
"terms": {
"field": "ids.keyword",
"include": "apple.*" <--
}
}
}
}
}
}
请检查处理在其中使用正则表达式的其他线程include
——它与您的用例非常相似。
推荐阅读
- python - 如何在 Python 中持续设置环境,同时解决 1024 个字符的限制
- sqlite - 烧瓶:卡住 qlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL 约束失败
- oracle - 如何将此 Oracle 存储过程转换为 SQL Server 存储过程?
- c# - 计算未出现在另一个表中的元素数量的最佳方法
- vba - 使用 Web 浏览器对象和 CurrentProject.Path 属性显示 pdf 文件以将文件加载到表单上
- google-cloud-platform - 无效的云构建超时?
- javascript - 如何获得选择的标签?
- h3 - 如何识别与给定地理围栏重叠的六边形?
- r - 在闪亮的 Dygraphs 上本地化日期
- vector - 无法在 Grafana 中将 PCP Vector 与可变目标主机一起使用