'SpringData ElasticSearch insert/update multiple documents
My goal is to do insert and update multiple documents in ElasticSearch using ElasticsearchRepository
.
public interface EmployeeInfoRepository extends ElasticsearchRepository<EmployeeInfo, String> {
}
However, whenever I call saveAll(entities)
, the number of document is unchanged but it creates new indexes for those entities.
employeeInfoRepository.saveAll(employeeInfos);
If I insert 1000 elements, at first it will have 1000 docs and 1000 indexes, which is what I expected. Then I call saveAll
two more times, it still has 1000 docs but now the number of indexes increases to 3000.
How can I update it properly?
It would be the best if it's just as easy as calling saveAll
and the rest is handled by SpringBootData ElasticSearch.
Update 1:
- There is no change with the data, however when I run
saveAll
, thestorage_size
keeps increasing. Not sure if it creates the indexes again and still keeps the old indexes.
Solution 1:[1]
If you define the id in your document class, elasticsearch updates/inserts the document with the same document id value. Here an example:
@Document(indexName = "example_index")
public class Example {
@Id
@Field(type = FieldType.Long)
private long id;
}
Of course, you have to handle the logic for a unique id in your project.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | frascu |