Spring boot 如何在Java中使用RESTAPI对弹性搜索中的对象数组进行索引
我使用ES7.5.1存储数据,并使用RESTAPI Spring boot。 它可以很好地处理单个对象,但当我试图传递一个对象数组时,它抛出了错误的请求或不可接受的错误406 代码: 您需要使用批量API。 此API接受JSON NDJSON结构中的对象数组 在单个API调用中执行多个索引或删除操作。这样可以减少开销,并大大提高索引速度Spring boot 如何在Java中使用RESTAPI对弹性搜索中的对象数组进行索引,spring-boot,elasticsearch,Spring Boot,elasticsearch,我使用ES7.5.1存储数据,并使用RESTAPI Spring boot。 它可以很好地处理单个对象,但当我试图传递一个对象数组时,它抛出了错误的请求或不可接受的错误406 代码: 您需要使用批量API。 此API接受JSON NDJSON结构中的对象数组 在单个API调用中执行多个索引或删除操作。这样可以减少开销,并大大提高索引速度 我们可以使用RestHighLevelClient将批量数据索引到弹性数据中 Maven依赖项: <dependency> <
我们可以使用RestHighLevelClient将批量数据索引到弹性数据中 Maven依赖项:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elastic.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elastic.version}</version>
</dependency>
用法:
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public void storeBulkDataInElastic(List<UserBean> dataBeanList) throws IOException
{
BulkRequest bulkRequest = new BulkRequest();
ObjectMapper objectMapper = new ObjectMapper();
dataBeanList.forEach(data -> {
IndexRequest indexRequest = new IndexRequest("ElasticIndex", "ElasticType", "ElasticId").source(objectMapper.convertValue(data, Map.class));
bulkRequest.add(indexRequest);
});
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
}
提供的此信息不足以提供帮助,请提供更多信息。感谢您的快速响应,我是弹性搜索新手。请指定所需信息。您使用的弹性版本是哪个?ES版本是7.5.1
@Bean
protected RestHighLevelClient createInstance() throws Exception
{
try
{
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", "9200", "http")));
}
catch (Exception e)
{
LOG.error("Error while creating RestHighLevelClient", e);
}
return restHighLevelClient;
}
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public void storeBulkDataInElastic(List<UserBean> dataBeanList) throws IOException
{
BulkRequest bulkRequest = new BulkRequest();
ObjectMapper objectMapper = new ObjectMapper();
dataBeanList.forEach(data -> {
IndexRequest indexRequest = new IndexRequest("ElasticIndex", "ElasticType", "ElasticId").source(objectMapper.convertValue(data, Map.class));
bulkRequest.add(indexRequest);
});
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
}