Elasticsearch return 给定过滤器后数组字段中的唯一字符串
Elasticsearch return unique string from array field after a given filter
如何从弹性搜索记录中获取具有给定前缀的所有 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 的文档,但在聚合中它将 return 所有ids 文档的一部分。
{
"aggregations": {
"filterIds": {
"filter": {
"bool": {
"filter": [
{
"prefix": {
"ids.keyword": {
"value": "apple"
}
}
}
]
}
},
"aggregations": {
"uniqueIds": {
"terms": {
"field": "ids.keyword",
}
}
}
}
}
}
returning 聚合列表为 [ "appleA", "orange_B", "apple_B","apple_C", "banana_A "] 如果我们将前缀输入作为苹果。基本上 return 所有具有匹配过滤器的 ID。
是否只获取数组中匹配前缀的id,而不是document数组中的所有id?
您可以使用 include
parameter:
限制返回值
POST items/_search
{
"size": 0,
"aggregations": {
"filterIds": {
"filter": {
"bool": {
"filter": [
{
"prefix": {
"ids.keyword": {
"value": "apple"
}
}
}
]
}
},
"aggregations": {
"uniqueIds": {
"terms": {
"field": "ids.keyword",
"include": "apple.*" <--
}
}
}
}
}
}
请检查 ,它涉及在 include
中使用正则表达式——它与您的用例非常相似。
如何从弹性搜索记录中获取具有给定前缀的所有 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 的文档,但在聚合中它将 return 所有ids 文档的一部分。
{
"aggregations": {
"filterIds": {
"filter": {
"bool": {
"filter": [
{
"prefix": {
"ids.keyword": {
"value": "apple"
}
}
}
]
}
},
"aggregations": {
"uniqueIds": {
"terms": {
"field": "ids.keyword",
}
}
}
}
}
}
returning 聚合列表为 [ "appleA", "orange_B", "apple_B","apple_C", "banana_A "] 如果我们将前缀输入作为苹果。基本上 return 所有具有匹配过滤器的 ID。
是否只获取数组中匹配前缀的id,而不是document数组中的所有id?
您可以使用 include
parameter:
POST items/_search
{
"size": 0,
"aggregations": {
"filterIds": {
"filter": {
"bool": {
"filter": [
{
"prefix": {
"ids.keyword": {
"value": "apple"
}
}
}
]
}
},
"aggregations": {
"uniqueIds": {
"terms": {
"field": "ids.keyword",
"include": "apple.*" <--
}
}
}
}
}
}
请检查 include
中使用正则表达式——它与您的用例非常相似。