我使用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);
并更新了记录。
答案 0 :(得分:1)
您可以使用UpdateQuery和ElasticSearchTemplate更新任何部分文档。例如
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);
答案 1 :(得分:1)
我更新了索引文档,如下代码片段:
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;
}
答案 2 :(得分:1)
第二次更新失败,因为您尝试更新版本未更改的实体。你得到的错误信息是ES告诉你,“嘿,你不能保存两次相同的版本!”试试这个:
//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);
答案 3 :(得分:0)
我认为ElasticSearch与JSON存储类似:
if(exist) {
update it;// push json to cover it
} else {
add it;// new save();
}
当Id / Entity存在时,它将更新JSON,或者它将添加它。
答案 4 :(得分:0)
XContentType contentType =
org.elasticsearch.client.Requests.INDEX_CONTENT_TYPE;
public XContentBuilder getBuilder(User assign){
try {
XContentBuilder builder = XContentFactory.contentBuilder(contentType);
builder.startObject();
Map<String,?> assignMap=objectMap.convertValue(assign, Map.class);
builder.field("assignee",assignMap);
return builder;
} catch (IOException e) {
log.error("custom field index",e);
}
IndexRequest indexRequest = new IndexRequest();
indexRequest.source(getBuilder(assign));
UpdateQuery updateQuery = new UpdateQueryBuilder()
.withType(<IndexType>)
.withIndexName(<IndexName>)
.withId(String.valueOf(id))
.withClass(<IndexClass>)
.withIndexRequest(indexRequest)
.build();