ElasticsearchRestTemplate 无法将多个文档保存到不同的索引

ElasticsearchRestTemplate can't save multiple documents to different index

我有一个测试域class

public class TestDocument {

    private final String id;

    private final String strField;

    private final Integer intField;

    public TestDocument(final String id, final String strField, final Integer intField) {
        this.id = id;
        this.strField = strField;
        this.intField = intField;
    }


}

现在我用 3 个文档调用 ElasticsearchRestTemplate.save 方法并想保存到 3 个不同的索引中。

@Service
public class TestEsService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @PostConstruct
    public void testSave() {
        final TestDocument d1 = new TestDocument("id_1", "str1", 1);
        final TestDocument d2 = new TestDocument("id_2", "str2", 2);
        final TestDocument d3 = new TestDocument("id_3", "str3", 3);

        this.save(List.of(d1, d2, d3));
    }

    public void save(final List<TestDocument> documents) {

        final IndexCoordinates indexCoordinates = IndexCoordinates.of("index_1", "index_2", "index_3");
       
        this.elasticsearchRestTemplate.save(documents, indexCoordinates);

    }

}

执行以上代码后。我检查了我的本地弹性搜索。 curl -H 'Content-Type: application/json' 'http://localhost:9200/_cat/indices?pretty' -s

我的 ES 中只有一个索引。 yellow open index_1 17ppJ9vJRUGIVHYBKKxXog 1 1 3 0 5.5kb 5.5kb

并查看这个index_1索引的数据:

curl 'http://localhost:9200/index_1/_search?pretty'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_1",
        "_type" : "_doc",
        "_id" : "id_1",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.test.entity.TestDocument",
          "id" : "id_1",
          "strField" : "str1",
          "intField" : 1
        }
      },
      {
        "_index" : "index_1",
        "_type" : "_doc",
        "_id" : "id_2",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.test.entity.TestDocument",
          "id" : "id_2",
          "strField" : "str2",
          "intField" : 2
        }
      },
      {
        "_index" : "index_1",
        "_type" : "_doc",
        "_id" : "id_3",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.test.entity.TestDocument",
          "id" : "id_3",
          "strField" : "str3",
          "intField" : 3
        }
      }
    ]
  }
}

深入研究代码后:

我在 RequestFactory.bulkRequest 中找到了线索:

queries.forEach(query -> {

            if (query instanceof IndexQuery) {
                bulkRequest.add(indexRequest((IndexQuery) query, index));
            } else if (query instanceof UpdateQuery) {
                bulkRequest.add(updateRequest((UpdateQuery) query, index));
            }
        });

实际上IndexRequest()通过index.getIndexName();方法获取索引名称:

public IndexRequest indexRequest(IndexQuery query, IndexCoordinates index) {

        String indexName = index.getIndexName();
        IndexRequest indexRequest;

其中 IndexCoordinates.getIndexName() return 只有第一个索引名称。


    public String getIndexName() {
        return indexNames[0];
    }

这是一个错误吗?我应该向 spring-data-elasticsearch Github 问题报告吗?

IndexCoordinates 中的多个名称在访问使用多个索引名称的 Elasticsearch API 时使用,例如在多个索引中搜索数据时,但不用于写入访问。

如果您想将 3 个实体保存到 3 个索引,您需要使用不同的 IndexCoordinates 进行 3 次调用 - 每个调用都有一个索引名称。