如何在 ElasticSearch 中仅从源中获取内部字段?

how can I fetch only inner fields from source in ElasticSearch?

我有这样的索引结构:

{
          "id" : 42,
          "Person" : {
            "contracts" : [
              {
                "contractID" : "000000000000102"
              }
            ],
            "Ids" : [
              3,
              387,
              100,
              500,
              274,
              283,
              328,
              400,
              600
            ]
          },
          "dateUpdate" : "2020-12-07T13:15:00.408Z"
        }
      },
      ...
}

我需要一个搜索查询,它将仅从源中获取内部“Ids”字段,仅此而已。我该怎么做?

您可以在inner_hits中使用_source,方法如下

索引映射:

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

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "Person",
            "query": {
              "match_all": {}
            },
            "inner_hits": {
              "_source": {
                "includes": [
                  "Person.Ids"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

搜索结果:

"inner_hits": {
          "Person": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 1.0,
              "hits": [
                {
                  "_index": "65237264",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "Person",
                    "offset": 0
                  },
                  "_score": 1.0,
                  "_source": {
                    "Ids": [
                      3,
                      387,
                      100,
                      500,
                      274,
                      283,
                      328,
                      400,
                      600
                    ]
                  }
                }
              ]
            }
          }
        }

你也可以使用nested inner_hits and _souce,方法如下

{
  "query": {
    "nested": {
      "path": "Person",
      "query": {
        "match_all": {}
      },
      "inner_hits": {
        "_source" : false,
        "docvalue_fields" : [
          {
            "field": "Person.Ids",
            "format": "use_field_mapping"
          }
        ]
      }
    }
  }
}