簡體   English   中英

ElasticSearch:如何使用 ElasticSearch API 僅返回特定字段?

[英]ElasticSearch: How to return only specific fields using ElasticSearch API?

我正在使用 ElasticSearch 7.3 來查詢一些文檔,
我想在查詢響應中只返回每個文檔的特定字段,
我發現_source可以用來實現這個,
我可以使用這個查詢從 Kibana 做到這一點 -

GET /test/_search?_source=id
{
  "query": {
    "match_all": {}
  }
}

向我返回正確的數據 -

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "id" : 3
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : 2
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : { }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "id" : 4
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "id" : 5
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "id" : 6
        }
      }
    ]
  }
}

但我無法使用 ElasticSearch 的節點客戶端實現相同的目標 -

const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })

let searchTest = async () => {
  let indexToQuery = 'test';
  let esQuery = {
    index: indexToQuery,
    //q: '_source:id',
    body: {
        "query": {
          "match_all": {}
        }
    }
  };
  console.log('esQuery =>\n', esQuery);

  const result = await client.search(esQuery);
  console.log("search resp => \n", result.body.hits.hits);
};

searchTest();

有人可以幫我找到實現我的用例的正確方法嗎?

參考 -
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-get.html#get-source-filtering
https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/api-reference.html#api-search

_source也可以用作查詢的一部分。 在正文塊中添加_source作為query的兄弟。 更新如下:

{
  "query": {
    "match_all": {}
  },
  "_source": ["id"]
}

您可以使用_source控制應從搜索命中中檢索的字段。

請注意,如果您在搜索查詢中使用聚合並且您只對它們的結果(= 存儲桶)感興趣,您會在 _source-field 之外找到它們。 因此,您可以設置

"_source": false

避免獲取 _source 下的所有字段並稍微提高查詢的性能。

---編輯---

下面是一個示例查詢,它使用 kibana 示例航班數據上的術語聚合來獲取每個目的地機場 id 的點擊量:

GET kibana_sample_data_flights/_search
{
  "_source": false, 
  "query": {
    "match_all": {}
  },
  "aggs": {
    "airport_id_agg": {
      "terms": {
        "field": "DestAirportID"
      }
    }
}

}

響應如下所示:

"hits" : {
"total" : {
  "value" : 10000,
  "relation" : "gte"
},
"max_score" : 1.0,
"hits" : [
  {
    "_index" : "kibana_sample_data_flights",
    "_type" : "_doc",
    "_id" : "K-Aj6mwBFbrhq0Rw_O-6",
    "_score" : 1.0
  },
  ...
  ]
  },
  "aggregations" : {
    "airport_id_agg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 8898,
      "buckets" : [
        {
          "key" : "ZRH",
          "doc_count" : 691
        },
        {
          "key" : "XIY",
          "doc_count" : 526
        },
        {
          "key" : "YWG",
          "doc_count" : 460
        },
        ...
      ]
    }
  }

如您所見,響應不包含任何數據字段。 將此搜索查詢的結果與Elastic 演示頁面上未設置 "_source": false 的結果進行比較

以下是在響應中包含和排除字段的示例:

{
    "_source": {
        "include": [ "obj1.*", "obj2.*" ],
        "exclude": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM