1

I'm working in a project with Elasticsearch and Spring Data Elasticsearch.

I need to get the mapping of an object type of my index. My @document class looks like:

    @Document(indexName = "esbsdocuments", type = ESBSDocumentEls.MAPPING_TYPE)
    public class ESBSDocumentEls extends ESBSDomainModel {

    ...

    @Field(type =FieldType.Object, store = false)
        private Object metadata;

    ...
    }

If I try to get it via http://xxx:9200/_mapping I can get the mapping for "metadata" field correctly:

...

"metadata": {
            "properties": {
              "APP": {
                "type": "string"
              },
              "APPDST": {
                "type": "string"
              },
              "APPSUB": {
                "type": "string"
              },
              "COUNTSUB": {
                "type": "string"
              },
              "DOMINIO": {
                "type": "string"
              },
              "DUPLICATE": {
                "type": "string"
              },
              "EXCLUDEFIELDNAMES": {
                "type": "string"
              },
              "FECHA": {
                "type": "string"
              },
              "ID": {
                "type": "string"
              },
              "IDF": {
                "type": "string"
              },
              "IDSUB": {
                "type": "string"
              },
              "LOCALEDATE": {
                "type": "string"
              },
              "MENSAJE": {
                "type": "string"
              },
              "TAMANYO": {
                "type": "string"
              },
              "TIPO": {
                "type": "string"
              },
              "VERSION": {
                "type": "string"
              }
            }
          },

...

But when I try it in code with

Map mapping = elasticsearchTemplate.getMapping(ESBSDocumentEls.class);

I can only get:

... (definition of metadata dynamic templates)
metadata={type=object}
...

How can I get the detailed mapping definition using ElasticSearchTemplate or another Spring Data Elasticsearch class??

Thank you very much!

2 Answers 2

1

Finally I got a solution.

Instead of using elasticSearchTemplate, I have used the els java api:

GetMappingsResponse res = elasticSearchClient.admin().indices().getMappings(new GetMappingsRequest().indices(indexName)).get();
ImmutableOpenMap<String, MappingMetaData> indexMappings  = res.mappings().get(indexName);

MappingMetaData mappingMetaData = indexMappings.get(mappingType);

Map<String, Object> map = mappingMetaData.getSourceAsMap();
Map<String, Object> metadataProperties = (Map<String, Object>) ((Map<String, Object>) map.get("properties")).get("metadata");
Map<String, Object> metadataList = (Map<String, Object>) metadataProperties.get("properties");

This way I can get a Map with all my "metadata" fields. You can't get the api client from the elasticSearchTemplate (https://jira.spring.io/browse/DATAES-124) so you have to inject it :(

I hope this help someone!

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

Comments

1

You can now use the public <T> Map getMapping(Class<T> clazz) method which is already available in the template. It returns a map of fields and types.

elasticSearchTemplate.getMapping(ESBSDocumentEls.class);

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.