elasticsearch - 如何在基于字段的 Elasticsearch 中获取唯一文档,以及如何基于其他字段 'group by'结果

标签 elasticsearch nest

我刚刚开始进行 Elasticsearch ,需要解决一个对我来说太复杂的问题。我在索引中有数千个文档,必须从中查询预定义数量的文档(也可以是几千个),我必须根据某些字段从基于另一个字段的唯一文档中查找文档组。 (唯一文档的数量最多可以是几百个)。

我的索引中的文档如下所示:

{  
 "complexProperty1" : {
            "A" : "example",
            "B" : "1",
            "D" : true,
            "E" : "case",
            "F" : ["guide1","guide2"]
},
   "complexProperty2" : {
            "X" : "10",
            "Y" : ["specimen1","specimen2"],
            "Z" : "blueprint"
}
}

许多文档将complexProperty1.A作为“示例” 。我想一次包含它们,并且将生成的文档按complexProperty1.DcomplexProperty1.E分组,即对于每对complexProperty1.DcomplexProperty1.E,我都有一个文档列表(我的结果中只需要这些文档)。我正在使用Nest实现此目的。

最佳答案

您可以从一堆原始 terms 聚合开始,然后回到NEST DSL:

POST complexities/_doc
{
  "complexProperty1": {
    "A": "example",
    "B": "1",
    "D": true,
    "E": "case",
    "F": [
      "guide1",
      "guide2"
    ]
  },
  "complexProperty2": {
    "X": "10",
    "Y": [
      "specimen1",
      "specimen2"
    ],
    "Z": "blueprint"
  }
}
GET complexities/_search
{
  "size": 0,
  "aggs": {
    "by_A": {
      "terms": {
        "field": "complexProperty1.A.keyword"
      },
      "aggs": {
        "by_D": {
          "terms": {
            "field": "complexProperty1.D"
          }
        },
        "by_E": {
          "terms": {
            "field": "complexProperty1.E.keyword"
          }
        }
      }
    }
  }
}

为了获得基础文档,您可以将top_hits agg附加到每个子agg,包括。最高一级:
{
  "size": 0,
  "aggs": {
    "by_A": {
      "terms": {
        "field": "complexProperty1.A.keyword"
      },
      "aggs": {
        "top_hits_only": {
          "top_hits": {
            "_source": "*"
          }
        },
        "by_D": {
          "terms": {
            "field": "complexProperty1.D"
          },
          "aggs": {
            "top_hits_only": {
              "top_hits": {
                "_source": "*"
              }
            }
          }
        },
        "by_E": {
          "terms": {
            "field": "complexProperty1.E.keyword"
          },
          "aggs": {
            "top_hits_only": {
              "top_hits": {
                "_source": "*"
              }
            }
          }
        }
      }
    }
  }
}

关于elasticsearch - 如何在基于字段的 Elasticsearch 中获取唯一文档,以及如何基于其他字段 'group by'结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305890/

相关文章:

elasticsearch - 在ElasticSearch中建立索引以进行审核

elasticsearch - 在 Elasticsearch 中使用每日索引有什么缺点?

c# - Elasticsearch Nest找不到过滤器

c# - 使用 NEST 进行 Elasticsearch - 异步操作

elasticsearch - 结合两个Elasticsearch查询

templates - 创建针对特定索引ES 6.x的Elasticsearch模板

node.js - “NestMicroservice”错误地实现了接口(interface) 'INestMicroservice'

c# - 如何使用NEST获得包含给定子词的搜索结果?

elasticsearch - NEST C#查询在Foreach中创建SORT-ElasticSearch

elasticsearch - 使用Elasticsearch进行查询时遇到麻烦