检索没有 _source 字段的数据:

Retrieve data without _source field:

我是 elasticsearch 的新手,我正在做一个匹配所有查询,它以这种方式检索数据,但我只想在 _source 中检索数据,我该怎么做?有什么想法吗?

    {
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 107271,
    "max_score": 1,
    "hits": [
      {
        "_index": "sku_index",
        "_type": "po",
        "_id": "0",
        "_score": 1,
        "_source": {
          "loc": "ZZZ",
          "part": "PPP",
          "order_qty": "16",
        }
      },
      {
        "_index": "sku_index",
        "_type": "po",
        "_id": "14",
        "_score": 1,
        "_source": {
          "loc": "ZZZ",
          "part": "XXX",
          "order_qty": "7",
        },
{
            "_index": "sku_index",
            "_type": "po",
            "_id": "14",
            "_score": 1,
            "_source": {
              "loc": "ZZZ",
              "part": "ZZZ",
              "order_qty": "7",
            }
          }
      .......

我想要这样的东西:

      [{
          "loc": "ZZZ",
          "part": "PPP",
          "order_qty": "16",
        },
       {
          "loc": "ZZZ",
          "part": "XXX",
          "order_qty": "16",
        },
      {
          "loc": "ZZZ",
          "part": "ZZZ",
          "order_qty": "7",
        }
      ]

一种可行的方法是 source filtering,将其添加到您的查询中

"_source": {
        "includes": [ "loc", "part", "order_qty" ],
        "excludes": [ "not_needed_field" ]
},

另一种方式,如果您的字段是 stored(由于额外的 space 要求,情况并非总是如此,而且通常不推荐。

By default, field values are indexed to make them searchable, but they are not stored. This means that the field can be queried, but the original field value cannot be retrieved.

因此您需要在查询中添加:

"stored_fields" : ["loc", "part", "order_qty"]