1

我是弹性搜索的新手,我正在做一个匹配所有查询,它以这种方式检索数据,但我只想在 _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",
        }
      ]
4

1 回答 1

1

一种可能的方法是通过添加到您的查询来进行源过滤

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

另一种方式,如果您的字段被存储(由于额外的空间要求,并非总是如此,通常不推荐。

默认情况下,对字段值进行索引以使其可搜索,但不会存储它们。这意味着可以查询该字段,但无法检索原始字段值。

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

"stored_fields" : ["loc", "part", "order_qty"]
于 2017-01-31T12:25:05.670 回答