.setVersionType(version == null ? VersionType.FORCE : VersionType.EXTERNAL) .execute() .actionGet();
elasticsearchClient.prepareIndex(indexName, be.getType(), be.getId()).setSource(be.getJsonMap()) .setVersion(be.getVersion() == null ? 1 : be.getVersion().longValue()) .setVersionType(be.getVersion() == null ? VersionType.FORCE : VersionType.EXTERNAL));
/** * Write a json document into the search index. * Writing using a XContentBuilder is the most efficient way to add content to elasticsearch * * @param jsonMap * the json document to be indexed in elasticsearch * @param id * the unique identifier of a document * @param indexName * the name of the index * @param typeName * the type of the index */ public IndexResponse writeSource(String indexName, XContentBuilder json, String id, String typeName, long version, VersionType versionType) { // put this to the index IndexResponse r = elasticsearchClient.prepareIndex(indexName, typeName, id).setSource(json) .setVersion(version).setVersionType(versionType).execute() .actionGet(); // documentation about the versioning is available at // https://www.elastic.co/blog/elasticsearch-versioning-support // TODO: error handling return r; }
.setVersion(1) .setCreate(false) // enforces OpType.INDEX .setVersionType(VersionType.EXTERNAL_GTE));
private IndexRequestBuilder prepareIndex(IndexQuery query){ try { String indexName = isBlank(query.getIndexName())? retrieveIndexNameFromPersistentEntity(query.getObject().getClass())[0] : query.getIndexName(); String type = isBlank(query.getType())? retrieveTypeFromPersistentEntity(query.getObject().getClass())[0] : query.getType(); IndexRequestBuilder indexRequestBuilder = client.prepareIndex(indexName,type,query.getId()) .setSource(objectMapper.writeValueAsString(query.getObject())); if(query.getVersion() != null){ indexRequestBuilder.setVersion(query.getVersion()); indexRequestBuilder.setVersionType(EXTERNAL); } return indexRequestBuilder; } catch (IOException e) { throw new ElasticsearchException("failed to index the document [id: " + query.getId() +"]",e); } }
if (options.getConsistencyLevel() != null) builder.setConsistencyLevel(options.getConsistencyLevel()); if (options.getVersion() != null) builder.setVersion(options.getVersion()); if (options.getVersionType() != null) builder.setVersionType(options.getVersionType()); if (options.getTimestamp() != null) builder.setTimestamp(options.getTimestamp()); if (options.getTtl() != null) builder.setTTL(options.getTtl());