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

时间:2015-07-28 15:35:42

标签: elasticsearch 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!

1 个答案:

答案 0 :(得分:1)

最后我得到了一个解决方案。

我没有使用elasticSearchTemplate,而是使用了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");

这样我就可以获得包含所有“元数据”字段的地图。你无法从elasticSearchTemplate(https://jira.spring.io/browse/DATAES-124)获得api客户端,所以你必须注入它:(

我希望这有助于某人!