在 ElasticSearch 中放松

Unwind in ElasticSearch

我目前在 ElasticSearch 中有以下索引

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "type" : {
          "type": "text",
          "fielddata": true
        },
        "id" : {
          "type": "text",
          "fielddata": true
        },
        "nestedTypes": {
          "type": "nested",
          "properties": {
            "nestedTypeId":{
              "type": "integer"
            },
            "nestedType":{
              "type": "text",
              "fielddata": true
            },
            "isLead":{
              "type": "boolean"
            },
            "share":{
              "type": "float"
            },
            "amount":{
              "type": "float"
            }
          }
        }
      }
    }
  }
}

我需要在 HTML table 中显示嵌套类型以及每行中的 idtype 字段。

我正在尝试在 MongoDB 中实现类似于 unwind 的东西。

我试过如下反向嵌套聚合

GET my_index/_search
{
  "size": 0,
  "aggs": {
"NestedTypes": {
  "nested": {
    "path": "nestedTypes"
  },
  "aggs": {
    "NestedType": {
      "terms": {
        "field": "nestedTypes.nestedType",
        "order": {
          "_key": "desc"
        }
      },
      "aggs": {
        "Details": {
          "reverse_nested": {}, 
          "aggs": {
            "type": {
              "terms": {
                "field": "type"
              }
            },
            "id": {
              "terms": {
                "field": "id"
              }
            }
          }
        }
      }
    }
  }
}
  }
}

但是上面returns只有一个字段来自nestedTypes,但是我需要全部

此外,我需要为此 table 进行排序和分页。您能告诉我如何在 ElasticSearch 中实现这一点吗?

ElasticSearch 不支持这种开箱即用的操作。当在 git 中提出实施相同的请求时,给出了以下响应:

We discussed it in Fixit Friday and agreed that we won't try to implement it due to the fact that we can't think of a way to support such operations efficiently.

The only ideas that we thought were reasonable boiled down to having another index that stores the same data but flattened. Depending on your use-case, you might be able to maintain those two views in parallel or would only maintain the one you have today, then materialize a flattened view of the data when you need it and throw it away after you are done querying. In both cases, this requires client-side logic.

请求的link是here