6

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)? Commented Oct 23, 2019 at 15:22

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.