Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mapping corresponding to a specific query in Elasticsearch

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 ?

like image 219
louis Avatar asked Feb 01 '19 14:02

louis


People also ask

How do I show mappings in Elasticsearch?

These can be accessed from your dashboard by choosing Stack Settings > Elasticsearch . The next step is to write a a curl -x get command to retrieve the mappings from Elasticsearch. You will be returned with a json output similar to the below screenshot.

How do I capture a specific field in Elasticsearch?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.

Can I change mapping for existing index Elasticsearch?

It is not possible to update the mapping of an existing field. If the mapping is set to the wrong type, re-creating the index with updated mapping and re-indexing is the only option available. In version 7.0, Elasticsearch has deprecated the document type and the default document type is set to _doc.


1 Answers

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.

like image 131
ttylec Avatar answered Oct 14 '22 14:10

ttylec