首页 > 解决方案 > 是否可以从 Elasticsearch 中的文档中获取整个嵌套对象?

问题描述

想象一下,我有一个这样的文件:

{
  "_index": "bank-accounts",
  "_type": "_doc",
  "_id": "1",
  "_version": 4,
  "_seq_no": 3,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "id": 1,
    "balance": 140,
    "transactions": [
      {
        "id": "42f52474-a49b-4707-86e4-e983efb4ab31",
        "type": "Deposit",
        "amount": 100
      },
      {
        "id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
        "type": "Deposit",
        "amount": 50
      },
      {
        "id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
        "type": "Withdraw",
        "amount": 10
      }
    ],
    "accountCreatedAt": 1614029062764
  }
}

我确实想只返回transactions查询中的数组。

我将如何在 Elasticsearch 中执行此操作?这甚至可能吗?我已经使用 实现了结果fields[ "transactions.*" ],但它返回单独数组中的每个字段:

{
    ...
    "hits": [
      {
        "_index": "bank-accounts",
        "_type": "_doc",
        "_id": "1",
        "_score": 1,
        "fields": {
          "transactions.id": [
            "42f52474-a49b-4707-86e4-e983efb4ab31",
            "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
            "5693585d-6356-4d1a-8d7b-cac5d0dab39f"
          ],
          "transactions.amount": [
            100,
            50,
            10
          ],
          "transactions.type": [
            "Deposit",
            "Deposit",
            "Withdraw"
          ],
          ...
        }
      }
    ]
  }
}

我的意思是,我很可能会使用它,但我想要一些更简单的东西来处理。我希望得到这样的东西:

*我必须在搜索中使用文档 ID

{
    ...
    "hits": [
      {
        "_index": "bank-accounts",
        "_type": "_doc",
        "_id": "1",
        "_score": 3,
        "transactions": [
          {
            "id": "42f52474-a49b-4707-86e4-e983efb4ab31",
            "type": "Deposit",
            "amount": 100
          },
          {
            "id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
            "type": "Deposit",
            "amount": 50
          },
          {
            "id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
            "type": "Withdraw",
            "amount": 10
          },
          ....
        ]
      }
    ]
  }
}

这有可能实现吗?

标签: elasticsearch

解决方案


如果您只想返回transactions数组(因为您没有提到任何需要搜索的查询条件),您可以使用源过滤来实现。

添加一个工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "transactions": {
        "type": "nested"
      }
    }
  }
}

指数数据:

{
  "id": 1,
  "balance": 140,
  "transactions": [
    {
      "id": "42f52474-a49b-4707-86e4-e983efb4ab31",
      "type": "Deposit",
      "amount": 100
    },
    {
      "id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
      "type": "Deposit",
      "amount": 50
    },
    {
      "id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
      "type": "Withdraw",
      "amount": 10
    }
  ],
  "accountCreatedAt": 1614029062764
}

搜索查询:

{
  "_source": [
    "transactions.*"
  ]
}

搜索结果:

"hits": [
      {
        "_index": "66324257",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "transactions": [
            {
              "amount": 100,
              "id": "42f52474-a49b-4707-86e4-e983efb4ab31",
              "type": "Deposit"
            },
            {
              "amount": 50,
              "id": "3f8396a3-d747-4a4c-8926-cdcedea6b5c3",
              "type": "Deposit"
            },
            {
              "amount": 10,
              "id": "5693585d-6356-4d1a-8d7b-cac5d0dab39f",
              "type": "Withdraw"
            }
          ]
        }
      }
    ]

推荐阅读