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 ?