弹簧数据弹性搜索和更新 [英] spring-data-elasticsearch and update
问题描述
我正在使用spring-data-elasticsearch进行CRUD操作。
i'm using spring-data-elasticsearch to do CRUD operations.
我有一个自定义存储库,该存储库扩展了ElasticsearchRepository。
I have a custom Repository that extends ElasticsearchRepository.
最终,ElasticsearchRepository扩展了CrudRepository,这意味着可以更新现有记录。
Ultimately ElasticsearchRepository extends CrudRepository which implies updating an existing record is possible.
问题是,您如何做到这一点?我还没有找到一种叫做 update()的方法
The question is, how do you accomplish this? I haven't found a method called "update()"
我认为执行以下操作是可行的(从 https://github.com/BioMedCentralLtd/spring-data-elasticsearch-sample-application )
I thought doing the following would work (code stolen from https://github.com/BioMedCentralLtd/spring-data-elasticsearch-sample-application)
//create
Book book = new Book();
book.setId("123455");
book.setName("Spring Data Elasticsearch");
book.setVersion(System.currentTimeMillis());
repository.save(book);
//update
book.setName("THIS IS A COMPLETELY NEW TITLE");
repository.save(book);
但是第二次保存会引发InvocationTargetException
However the 2nd save throws an InvocationTargetException
通过调试器对其进行检查显示:
Examining it with the debugger shows:
[book][0] [book][123455]: version conflict, current [1447792071681], provided [1447792071681]
Book对象类似于:
The Book object looks like:
@Document(indexName = "book",type = "book" , shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1")
public class Book {
@Id
private String id;
private String name;
private Long price;
@Version
private Long version;
public Map<Integer, Collection<String>> getBuckets() {
return buckets;
}
public void setBuckets(Map<Integer, Collection<String>> buckets) {
this.buckets = buckets;
}
@Field(type = FieldType.Nested)
private Map<Integer, Collection<String>> buckets = new HashMap();
public Book(){}
public Book(String id, String name,Long version) {
this.id = id;
this.name = name;
this.version = version;
}
getters and setters removed for space
}
我的存储库代码甚至更简单:
My Repository code is even simpler:
import org.springframework.data.elasticsearch.entities.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface BookRepository extends ElasticsearchRepository<Book, Long> {
}
我必须提供一种更新方法吗?
Do I have to provide an update method?
编辑:
没关系。我将更新更改为:
Nevermind. I changed the update to:
//update
book.setName("THIS IS A COMPLETELY NEW TITLE");
book.setVersion(System.currentTimeMillis());
repository.save(book);
并更新了记录。
推荐答案
您可以使用UpdateQuery和ElasticSearchTemplate来更新任何部分文档。例如
You can use UpdateQuery and ElasticSearchTemplate to update any partial document. e.g
final UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(mainIndexName);
updateRequest.type(typeName);
updateRequest.id(id);
updateRequest.doc(XContentFactory.jsonBuilder().startObject()
.field("accountType", accountType)
.endObject());
final UpdateQuery updateQuery = new UpdateQueryBuilder().withId(id)
.withClass(<DocumentClass>).withUpdateRequest(updateRequest).build();
UpdateResponse updateResponse = elasticSearchTemplate.update(updateQuery);
这篇关于弹簧数据弹性搜索和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!