获取 Spring Data Elasticsearch 中对象类型字段的映射

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

我在一个项目中使用 Elasticsearch 和 Spring Data Elasticsearch。

我需要获取索引对象类型的映射。我的@document class 看起来像:

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

    ...

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

    ...
    }

如果我尝试通过 http://xxx:9200/_mapping 获取它,我可以正确获取 "metadata" 字段的映射:

...

"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"
              }
            }
          },

...

但是当我用

在代码中尝试它时
Map mapping = elasticsearchTemplate.getMapping(ESBSDocumentEls.class);

我只能得到:

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

如何使用 ElasticSearchTemplate 或其他 Spring Data Elasticsearch class 获取详细的映射定义??

非常感谢!

终于找到解决办法了

我没有使用 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");

通过这种方式,我可以获得包含所有 "metadata" 字段的地图。您无法从 elasticSearchTemplate (https://jira.spring.io/browse/DATAES-124) 获取 api 客户端,因此您必须注入它 :(

我希望这对某人有所帮助!

您现在可以使用模板中已有的 public <T> Map getMapping(Class<T> clazz) 方法。它 returns 字段和类型的映射。

elasticSearchTemplate.getMapping(ESBSDocumentEls.class);