Elfogre
Elfogre

Reputation: 105

Get the mapping of an Object type field in Spring Data Elasticsearch

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!

Upvotes: 1

Views: 5060

Answers (2)

Udara Bentota
Udara Bentota

Reputation: 91

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);

Upvotes: 1

Elfogre
Elfogre

Reputation: 105

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!

Upvotes: 1

Related Questions