Spring 数据 Elasticsearch 动态更改 indexName

标签 spring elasticsearch spring-data spring-data-elasticsearch

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

我的问题是,如果我手动创建索引和搜索(ElasticsearchTemplate.createIndex()、NativeSearchQueryBuilder().withIndices()),并删除实体类上的这一行。

@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;博士

如果删除 @Document,Spring-Data-Elasticseach 将不再起作用来自您类(class)的注释。

说明:

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

解决方案

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

证明

写作

    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 是正确的。

使用以下代码也可以阅读(您需要根据需要更改查询并将索引设置为当前客户端)
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在结果中产生完全填充的类:
[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)]

希望这有帮助!

关于Spring 数据 Elasticsearch 动态更改 indexName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53389760/

相关文章:

java - 异常 : java. lang.IllegalArgumentException : Pointcut is not well-formed Error?

grails - 如何在ElasticSearch + Grails中使用具有空JSON属性的must_not?

Spring Data Cassandra : "No property findAll for type User"

elasticsearch - 添加副本字段并将其设置为小写

spring-data-elasticsearch 搜索多个索引

spring-boot - 在采用过时值的事务中执行的 Spring native 查询

java - Spring beans 的键为 Map<String,Foo>

java - 将 Spring Security 与 JPA 结合使用

java - Spring AMQP错误: Listener method could not be invoked with the incoming message

elasticsearch - 无需重新索引即可集成 Elasticsearch 和 Stanford NLP