在没有_source字段的情况下检索数据:

时间:2017-01-31 11:58:06

标签: python elasticsearch elasticsearch-5

我是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",
        }
      ]

1 个答案:

答案 0 :(得分:1)

一种可能的方法是通过添加到您的查询来执行source filtering

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

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

  

默认情况下,字段值会被编入索引以使其可搜索,但它们是可搜索的   没有存储。这意味着可以查询该字段,但是   原始字段值无法检索。

所以你需要添加到你的查询中:

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