我正在使用spring-data-elasticsearch来做CRUD操作。
我有一个自定义存储库,它扩展了ElasticsearchRepository。
最终,ElasticsearchRepository扩展了CrudRepository,这意味着更新现有记录是可能的。
问题是,你如何做到这一点?我还没有找到名为"update()“的方法
我认为这样做是可行的(从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
使用调试器检查它会显示:
[book][0] [book][123455]: version conflict, current [1447792071681], provided [1447792071681]Book对象如下所示:
@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}
我的存储库代码甚至更简单:
import org.springframework.data.elasticsearch.entities.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface BookRepository extends ElasticsearchRepository<Book, Long> {
}我必须提供更新方法吗?
编辑:
不要紧。我将更新更改为:
//update
book.setName("THIS IS A COMPLETELY NEW TITLE");
book.setVersion(System.currentTimeMillis());
repository.save(book); 它更新了记录。
发布于 2016-12-27 21:16:31
您可以使用UpdateQuery和ElasticSearchTemplate更新任何部分文档。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);发布于 2017-03-14 14:41:18
我将索引文档更新为以下代码片段:
IndexRequest indexRequest = new IndexRequest(INDEX_NAME,INDEX_NAME,docid);
indexRequest.source(fldName, fldValue);
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(INDEX_NAME);
updateRequest.type(INDEX_NAME);
updateRequest.id(docid);
updateRequest.doc(indexRequest);
try {
UpdateResponse res=client.update(updateRequest).get();
logger.info("update es {}:{}",fe,res.getGetResult());
} catch (Exception e) {
logger.error("update",e);
throw e;
}发布于 2017-07-08 01:19:00
第二次更新失败,因为您正在尝试更新一个版本未更改的实体。你得到的错误信息是告诉你,“嘿,你不能将同一个版本保存两次!”试试这个:
//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");
book.setVersion(System.currentTimeMillis()); // you're saving a new version
repository.save(book); https://stackoverflow.com/questions/33766485
复制相似问题