Spring Data Elasticsearch不会从注释隐式创建映射

问题描述

我正在尝试使用Spring Data Elasticsearch 4.0.1,并试图弄清Spring是如何以及何时创建Elasticsearch映射的,或者是由Spring创建的。

如果我有这样的实体:

@Document(indexName = "companies")
public class CompanyEntity {

  @Id
  private final String id;

  @MultiField(
      mainField = @Field(type = Text),otherFields = {
          @InnerField(suffix = "raw",type = Keyword)
      }
  )
  private final String companyName;

  @PersistenceConstructor
  public CompanyEntity(String id,String companyName) {
    this.id = id;
    this.companyName = companyName;
  }

  public String getId() {
    return id;
  }

  public String getCompanyName() {
    return companyName;
  }
}

我的印象是Spring会为该索引隐式创建映射,但是我似乎错了。 Elasticsearch仍会为此索引创建映射。

{
    "companies": {
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text","fields": {
                        "keyword": {
                            "type": "keyword","ignore_above": 256
                        }
                    }
                },"companyName": {
                    "type": "text","id": {
                    "type": "text","ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

从上面可以明显看出,未使用InnerField批注中的后缀,也未使用ignore_above值,因为它的认值为-1,并且如果尝试将ignore_above设置为-1,Elasticsearch将完全删除此字段。 / p>

我能够获得上述批注的映射的唯一方法是自己明确设置映射。

@Autowired private ElasticsearchOperations operations;

Document mapping = operations.indexOps(CompanyEntity.class).createMapping();
operations.indexOps(CompanyEntity.class).putMapping(mapping); 

产生预期的映射:

{
    "companies": {
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text","fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    }
                },"ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

这很好,但是我感到有点奇怪,因为我在官方Spring Data Elasticsearch docs中找不到这种方法的任何细节。而且JavaDocs几乎没有任何细节。

这是从Spring Data将映射安装到Elasticsearch中的正确方法吗?

解决方法

如果要使用const format = require("string-template"); const str = "We are from {BusinessName}. This month we sold {SalesQuantityPounds} of {SalesItem} for a total profit of {SalesTotalQuarter4}."; const res = format(str,{ "BusinessName": "Acme International","SalesQuantityPounds": "198","SalesItem": "567","SalesTotalQuarter4": "$ 74370" }); 进行操作,则这是创建映射的正确方法。在4.1版中,将有一个附加方法

ElasticsearchOperations

结合了创建和编写映射的两个步骤。

boolean putMapping(Class<?> clazz) ElasticsearchOperations-或更好的实现-是对Spring Data中Elasticsearch的较低级别访问Elasticsearch-创建索引,放置映射,编写实体等。这些是基本操作。您可以完全控制要执行的操作,但是执行这些操作是您的责任。

基于此的是存储库支持。如果您定义了存储库接口

IndexOperations

您要在其中一门课程中插入的

interface CompanyRepository extends ElasticsearchRepository<CompanyEntity,String>{}

然后在应用程序启动时,Spring Data Elasticsearch将创建此接口的实现,并将检查由实体的@Autowired CompanyRepository repository; 注释定义的索引是否存在。如果不是,它将创建索引并编写映射-@Document批注具有参数@Document,默认情况下为true。

因此,对于自动创建,您必须使用存储库,存储库支持还利用createIndex提供诸如从方法名称派生查询,分页支持等功能。