Spring 数据 Elasticsearch 动态更改 indexName

Spring data Elasticsearch change indexName dynamically

我正在尝试使用 spring data elastisearch 保存一些数据。我需要为不同的客户端创建相同的索引。前任。如果我有索引 my-index,我需要为客户端 A 和 B 创建 my-index-A、my-index-B。但是注解 @Document 仅适用于静态 indexName 或非线程安全的 spEL。

我的问题是,如果我创建索引并手动搜索(ElasticsearchTemplate.createIndex()、NativeSearchQueryBuilder().withIndices()),并在实体 class.[=13= 上删除这一行]

@Document(indexName = "my-index-A")

实体还能收到它的值吗?换句话说,注解

@Id
@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
private String aid;

@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
private String userId;

@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
private String entityId;

@Field(index = FieldIndex.not_analyzed, type = FieldType.String)
private String userName;

还能用吗?

TL;DR

Spring-如果您从 class.

中删除 @Document 注释,Data-Elasticseach 将不再工作

解释:

如果您从 class 中删除 @Document,在读取或写入(确定索引名称、类型和 ID 时)时,多个 elasticsearch 操作将失败,因为 ElasticsearchTemplate.getPersistentEntityFor(Class clazz) 严重依赖于此注解。

解决方案

我已经成功地 read/write 使用不同的索引使用一个带注释的 class 和一个虚拟注释 @Document(indexName = "dummy", createIndex = false) 并显式设置索引使用 elasticsearchTemplate 的所有 read/write 操作的名称。

证明

写作

    ElasticEntity foo = new ElasticEntity();
    foo.setAid("foo-a-id");
    foo.setEntityId("foo-entity-id");
    foo.setUserName("foo-user-name");
    foo.setUserId("foo-user-id");

    IndexQuery fooIdxQuery = new IndexQueryBuilder()
            .withIndexName("idx-foo")
            .withObject(foo)
            .build();

    String fooId = template.index(fooIdxQuery);

    ElasticEntity bar = new ElasticEntity();
    bar.setAid("bar-a-id");
    bar.setEntityId("bar-entity-id");
    bar.setUserName("bar-user-name");
    bar.setUserId("bar-user-id");

    IndexQuery barIdxQuery = new IndexQueryBuilder()
            .withIndexName("idx-bar")
            .withObject(bar)
            .build();

    String barId = template.index(barIdxQuery);

应该将对象存储在不同的索引中。

curl http://localhost:9200/idx-*/_search?pretty 双重检查给出:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx-bar",
        "_type" : "elasticentity",
        "_id" : "bar-a-id",
        "_score" : 1.0,
        "_source" : {
          "aid" : "bar-a-id",
          "userId" : "bar-user-id",
          "entityId" : "bar-entity-id",
          "userName" : "bar-user-name"
        }
      },
      {
        "_index" : "idx-foo",
        "_type" : "elasticentity",
        "_id" : "foo-a-id",
        "_score" : 1.0,
        "_source" : {
          "aid" : "foo-a-id",
          "userId" : "foo-user-id",
          "entityId" : "foo-entity-id",
          "userName" : "foo-user-name"
        }
      }
    ]
  }
}

如您所见,响应中的索引名称和 _id 是正确的。

Reading 也可以使用以下代码(您需要根据需要更改查询并将索引设置为当前客户端)

SearchQuery searchQuery = new NativeSearchQueryBuilder()
              .withQuery(matchAllQuery())
              .withIndices("idx-foo", "idx-bar")
              .build();

List<ElasticEntity> elasticEntities = template.queryForList(searchQuery, ElasticEntity.class);
logger.trace(elasticEntities.toString());

映射也有效,因为 logger 在结果中产生完全填充的 classes:

[ElasticEntity(aid=bar-a-id, userId=bar-user-id, entityId=bar-entity-id, userName=bar-user-name), ElasticEntity(aid=foo-a-id, userId=foo-user-id, entityId=foo-entity-id, userName=foo-user-name)]

希望对您有所帮助!