Displaying fields in an Elasticsearch index

Exploring techniques for displaying fields in an Elasticsearch index.

In this article, we will discuss how to display fields in an Elasticsearch index. This can be useful for understanding the structure of your data, identifying specific fields, and troubleshooting issues. We will cover the following topics:

  1. Using the _mapping API to retrieve field information
  2. Using the _search API to display field values
  3. Filtering fields using the fields parameter
  4. Displaying nested fields

1. Using the _mapping API to retrieve field information

The _mapping API allows you to retrieve the mapping definition for an index or multiple indices. This includes information about the fields, their data types, and other properties. To retrieve the mapping for a specific index, use the following request:

GET /<index_name>/_mapping

For example, if you have an index named my_index, you can retrieve its mapping with the following request:

GET /my_index/_mapping

The response will include the mapping definition for the index, which contains information about the fields and their properties.

It is also possible to retrieve the mapping of one specific field. This can be useful if your mapping is quite big and you only want to focus on a specific field. To retrieve the mapping of a specific field, use the following request:

GET /my_index/_mapping/field/my_field

You can also retrieve the mappings of several fields by separating their names with commas as in the following request:

GET /my_index/_mapping/field/my_field_1,my_field_2,my_field_3

2. Using the _search API to display field values

To display the values of fields in an Elasticsearch index, you can use the _search API. By default, the _search API returns the _source field, which contains the original JSON document that was indexed. To display only specific fields, you can use the _source parameter in the search request.

Here’s an example of a search request that returns the values of the title and author fields for documents in the my_index index:

GET /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["title", "author"]
}

In this example, the _source parameter specifies the fields to be returned.

3. Filtering fields using the fields parameter

You can also use the fields parameter to filter the fields returned in the search response. This can be useful if you only need specific fields and want to reduce the size of the response. The fields parameter accepts an array of field names or wildcard patterns.

For example, to return only the title and author fields for documents in the my_index index, you can use the following search request:

GET /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "fields": ["title", "author"],
  "_source": false
}

Note that the _source parameter is set to false in order to not return the source document.

To return all fields with a text data type, you can use a wildcard pattern like this:

GET /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "fields": ["*.text"],
  "_source": false
}

4. Displaying nested fields

If your index contains nested fields, you can use the dot notation to specify the nested field path in the fields parameter. For example, if you have a nested field named address.city, you can include it in the search response like this:

GET /my_index/_search
{
  "query": {
    "match_all": {}
  },
  "fields": ["title", "author", "address.city"],
  "_source": false
}

In this example, the search response will include the values of the title, author, and address.city fields.

Conclusion

In conclusion, displaying fields in an Elasticsearch index can be achieved using the _mapping API to retrieve field information and the _search API to display field values. You can filter the fields returned in the search response either using the _source or fields parameters and display nested fields using the dot notation. These techniques can help you understand the structure of your data, identify specific fields, and troubleshoot issues.

Ready to try this out on your own? Start a free trial.

Want to get Elastic certified? Find out when the next Elasticsearch Engineer training is running!

Related content

Elasticsearch in Javascript the proper way, part II

Elasticsearch in Javascript the proper way, part II

Reviewing production best practices and explaining how to run the Elasticsearch Node.js client in Serverless environments.

Elasticsearch in Javascript the proper way, part I

Elasticsearch in Javascript the proper way, part I

Explaining how to create a production-ready Elasticsearch backend in JavaScript.

Deleting a field from a document in Elasticsearch

May 9, 2025

Deleting a field from a document in Elasticsearch

Exploring methods for deleting a field from a document in Elasticsearch.

Elasticsearch shards and replicas: Getting started guide

May 21, 2025

Elasticsearch shards and replicas: Getting started guide

Master the concepts of shards and replicas in Elasticsearch and learn how to optimize them.

Elasticsearch string contains substring: Advanced query techniques

May 19, 2025

Elasticsearch string contains substring: Advanced query techniques

Explore techniques for querying Elasticsearch to find documents where a field contains a specific substring.

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as your are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself