6
votes

Since last versions of Elasticsearch have deprecated multiples types in a single index, I have organised my data through a "doc_type" field with type "keyword", which enables me to run queries like:

curl 'localhost:9200/my_index/_search?pretty&q=doc_type:my_doc_type'

This query will return all documents whose doc_type field is 'my_doc_type' exactly. Now, I would like to retrieve mapping only for fields from this kind of request.

To reproduce the situation, suppose we define our index mapping as followed:

curl -XPUT 'localhost:9200/my_index/my_type/_mapping?pretty' -H 'Content-Type: application/json' -d '{
  "my_type" : {
    "properties" : {
      "first_name" : {"type" : "text" },
      "last_name": {"type": "text"},
      "doc_type": {"type": "keyword"}
    }
  }
}'

Now, let's inject the two following documents:

curl -XPUT 'localhost:9200/my_index/my_type/1' -H 'Content-Type: application/json' -d '{ "last_name": "Doe", "first_name": "John", "doc_type": "type_a"}'
curl -XPUT 'localhost:9200/my_index/my_type/1' -H 'Content-Type: application/json' -d '{ "first_name": "Jane", "doc_type": "type_b"}'

I would be able to retrieve mapping only for documents that match the query q=doc_type:type_b. In that case, I should only retrieve mapping for fields "keyword" and "first_name".

If this is not possible with a single query, I know that Elasticsearch provides a specific field mapping query, but to use it, I would first need to retrieve ALL fields union of all documents matching the q=doc_type:type_b query. Is there also a way to do that ?

1
If you need different "types" why not use different indexes (and have truly different types)? - ttylec

1 Answers

1
votes

I am not an expert, but I believe that it is not possible to get a "mapping" of a result of specific ElasticSearch query. The definition of the mapping from the ES documentation says:

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. For instance, use mappings to define: [...]

This concept makes no sense for the result of a query.

It seems that retrieving all results and computing union is necessary. Alternatively, you could try to do aggregation on all fields from the mapping counting non-null values. Not sure how efficient it would be.

In any case, it's probably better to receive whole mapping and filter relevant fields than retrieve each separately for each field.