Go 基于另一个字段获取给定字段的所有值
我有一些文档包含字段:id、大小等 我想找到所有可能的大小,其中id=1或2Go 基于另一个字段获取给定字段的所有值,go,
elasticsearch,Go,
elasticsearch,我有一些文档包含字段:id、大小等 我想找到所有可能的大小,其中id=1或2 这可能吗?您可以将术语查询与源筛选结合使用。添加工作示例 索引数据: { "id": 1, "size": 1 } { "id": 2, "size": 2 } { "id": 3, "size": 3 } { "_source": "siz
这可能吗?您可以将术语查询与源筛选结合使用。添加工作示例 索引数据:
{
"id": 1,
"size": 1
}
{
"id": 2,
"size": 2
}
{
"id": 3,
"size": 3
}
{
"_source": "size",
"query": {
"terms": {
"id": [
1,
2
]
}
}
}
"hits": [
{
"_index": "66381642",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"size": 1
}
},
{
"_index": "66381642",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"size": 2
}
}
]
搜索查询:
{
"id": 1,
"size": 1
}
{
"id": 2,
"size": 2
}
{
"id": 3,
"size": 3
}
{
"_source": "size",
"query": {
"terms": {
"id": [
1,
2
]
}
}
}
"hits": [
{
"_index": "66381642",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"size": 1
}
},
{
"_index": "66381642",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"size": 2
}
}
]
搜索结果:
{
"id": 1,
"size": 1
}
{
"id": 2,
"size": 2
}
{
"id": 3,
"size": 3
}
{
"_source": "size",
"query": {
"terms": {
"id": [
1,
2
]
}
}
}
"hits": [
{
"_index": "66381642",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"size": 1
}
},
{
"_index": "66381642",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"size": 2
}
}
]
如果要显示该ID的可能大小,则应使用聚合
POST your_index/_search
{
"size": 0,
"query": {
"terms": {
"id": [
"1",
"2"
]
}
},
"aggs": {
"sizes": {
"terms": {
"field": "size"
}
}
}
}
响应将是唯一大小以及具有该大小的文档数量
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"sizes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"doc_count" : 1
},
{
"key" : 2,
"doc_count" : 1
}
]
}
}
}
@jason135您是否有机会仔细阅读答案,期待您的反馈