如何在Elasticsearch中仅返回查询结果的_source字段?
_source Content in Elasticsearch Search Results Hey there! Let's solve this—you want to cut through all the default extra fields (like took, timed_out, _shards, max_score, plus metadata like _id/_score in each hit) and only get the _source content from your Elasticsearch queries. Here are the most straightforward ways to do it:
1. Use the filter_path Parameter (Simplest & Most Effective)
This parameter lets you filter the entire JSON response to keep only the paths you care about. To fetch just the _source entries from your search hits, add this to your request:
Example with URL Parameter:
GET /your_target_index/_search?filter_path=hits.hits._source
Example with Request Body:
POST /your_target_index/_search { "filter_path": ["hits.hits._source"], "query": { // Replace with your actual query (e.g., match, term, etc.) "match_all": {} } }
This response will only include the array of _source objects—no extra top-level fields or hit metadata cluttering things up.
2. Combine _source and filter_path (For Explicit Control)
If you want to explicitly confirm you're fetching the full _source (in case your index has custom default settings), pair _source: true with the filter_path parameter:
POST /your_target_index/_search { "_source": true, "filter_path": ["hits.hits._source"], "query": { "match": { "content": "elasticsearch tips" } } }
Note: _source: true is Elasticsearch's default behavior, so this is optional—but it’s handy if you need to override any configurations that might hide the _source.
3. Skip the fields Parameter for This Use Case
While you could use fields to fetch all fields, it’s not ideal for getting the full _source. It returns a different structure (field names as keys with array values) instead of the original _source object. Stick to the first two methods for clean, direct results.
That’s all you need! Any of these approaches will give you exactly the _source content you want, without the extra noise.
内容的提问来源于stack exchange,提问作者Tzipporah Raanan

