Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all fields in an elasticsearch index?

How do I get a list of all the fields that are present in an index (i.e. fields that occur in indexed documents, not just in the mapping)?

like image 790
ejain Avatar asked Apr 30 '14 01:04

ejain


People also ask

How do I get all field names in Elasticsearch?

You may use _field_names field. The _field_names field indexes the names of every field in a document that contains any value other than null. Yes, you may only query for the existence of a field but can't aggregate on the same. The get mappings approach will not work if you are using the join functionality.

How do I get Elasticsearch index list?

There are multiple ways to list all of the indexes contained in an Elasticsearch cluster. One common way of doing this is to use cURL and Kibana to issue HTTP requests to communicate with the Elasticsearch cluster to have it return all of the index names and their respective UUIDs.

Are all fields indexed in Elasticsearch?

By default, Elasticsearch indexes all data in every field and each indexed field has a dedicated, optimized data structure. For example, text fields are stored in inverted indices, and numeric and geo fields are stored in BKD trees.

What are fields in Elasticsearch?

Fields are the smallest individual unit of data in Elasticsearch. These are customizable and could include, for example: title, author, date, summary, team, score, etc. Each field has a defined datatype and contains a single piece of data.


2 Answers

Explanation:

Don't think there is any way to do exactly that. But since everything in the index automatically gets thrown in the mapping, we know that the mapping contains at least every field in the index. From there, you can loop through each field in the mapping and run a count on the number of results in the index that have that field. If the count is more than 0, then that field exists; if the count is 0, then that field is not part of the index. Since we know that every field in the index will exist in your mapping, this should cover all possibilities.

Some example API calls:

# Get the mapping
$ curl -XGET 'http://localhost:9200/index/type/_mapping?pretty'

# Count a field
$ curl -XGET 'http://localhost:9200/index/type/_count' -d '
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "name_from_mapping" }
            }
        }
    }
}'

Documentation:

  • GET mapping
  • count API
  • exists filter
like image 67
Sam Avatar answered Oct 06 '22 16:10

Sam


In current(5.2) version, you can use the mapping API to get all the field names:

GET index_name/_mapping?pretty

please refer to the official document for more information.

like image 14
dezhi Avatar answered Oct 06 '22 17:10

dezhi