首页 > 解决方案 > 如何从 Elasticsearch 返回源字段

问题描述

我正在使用 Elasticsearch V7。

我只想从 Elasticsearch 中获取源字段。

请求查询:

GET /test/_search?filter_path=hits.hits._source
{
  "query": {
    "term": {
      "test": {
        "value": "123"
      }
    }
  }
}

回复:

{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "field1" : "value1",
          "field2" : "value2",
          "field3" : "value3",
        }
      }
    ]
  }
}

预期结果:

{
  "hits" : {
    "hits" : [
      {
          "field1" : "value1",
          "field2" : "value2",
          "field3" : "value3",
      }, 
      {
          "field1" : "value1",
          "field2" : "value2",
          "field3" : "value3",
      }
    ]
  }
}

或者

    "hits" : [
      {
          "field1" : "value1",
          "field2" : "value2",
          "field3" : "value3",
      }, 
      {
          "field1" : "value1",
          "field2" : "value2",
          "field3" : "value3",
      }
    ]

有没有办法从 Elasticsearch 中获取上述预期结果?

标签: elasticsearch

解决方案


不,这是不可能的。filter_path就本机响应过滤而言,它已尽善尽美。

话虽如此,您可以使用jq命令行 JSON 处理器来_sources进一步提取:

curl -XGET "http://localhost:9200/test/_search?filter_path=hits.hits._source" \
     -H 'Content-Type: application/json' \
     -d'{ 
          "query": {   
              "match_all": {} 
          }
        }' \
    | jq '{hits: [.hits.hits[]._source]}'

推荐阅读