Java Code Examples for org.elasticsearch.action.index.IndexResponse#getVersion()

The following examples show how to use org.elasticsearch.action.index.IndexResponse#getVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticSearch.java    From hsweb-learning with Apache License 2.0 6 votes vote down vote up
private static void CreateIndex(Client client){
    String json = "{" +
            "\"user\":\"daiyutage\"," +
            "\"postDate\":\"2013-01-30\"," +
            "\"message\":\"trying out Elasticsearch\"" +
            "}";

    IndexResponse response = client.prepareIndex("twitter", "tweet","2")
            .setSource(json)
            .get();
    // Index name
    String _index = response.getIndex();
    System.out.println("index:"+_index);
    // Type name  
    String _type = response.getType();
    System.out.println("_type:"+_type);
    // Document ID (generated or not)  
    String _id = response.getId();
    // Version (if it's the first time you index this document, you will get: 1)  
    long _version = response.getVersion();
    System.out.println("_version:"+_version);
    // isCreated() is true if the document is a new one, false if it has been updated  
    boolean created = response.isCreated();

}
 
Example 2
Source File: TestTransportClient.java    From jframe with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndex() throws IOException {
    IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(XContentFactory.jsonBuilder().startObject()
            .field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject()).get();
    // Index name
    String _index = response.getIndex();
    // Type name
    String _type = response.getType();
    // Document ID (generated or not)
    String _id = response.getId();
    // Version (if it's the first time you index this document, you will
    // get: 1)
    long _version = response.getVersion();
    // isCreated() is true if the document is a new one, false if it has
    // been updated
    boolean created = response.isCreated();
    System.out.println(response.toString());

}
 
Example 3
Source File: ESClientTest.java    From emotional_analysis with Apache License 2.0 5 votes vote down vote up
@Test
    public void test1() throws Exception {
        XContentBuilder source = createJson4();
        // 存json入索引中
        IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(source).get();
//        // 结果获取
        String index = response.getIndex();
        String type = response.getType();
        String id = response.getId();
        long version = response.getVersion();
        boolean created = response.isCreated();
        System.out.println(index + " : " + type + ": " + id + ": " + version + ": " + created);
    }
 
Example 4
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static long doIndexRequest(ActionRequestBuilder<?, ? extends IndexResponse, ?> request) throws IOException {
	IndexResponse response = request.execute().actionGet();
	boolean ok = response.status().equals(RestStatus.CREATED);
	if (!ok) {
		throw new IOException("Document not created: " + request.get().getClass().getName());
	}
	return response.getVersion();
}
 
Example 5
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
 * 打印索引信息
 * @param response
 */
private static void printIndexInfo(IndexResponse response) {
	System.out.println("****************index ***********************");
	// Index name
	String _index = response.getIndex();
	// Type name
	String _type = response.getType();
	// Document ID (generated or not)
	String _id = response.getId();
	// Version (if it's the first time you index this document, you will get: 1)
	long _version = response.getVersion();
	System.out.println(_index+","+_type+","+_id+","+_version);
}
 
Example 6
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 4 votes vote down vote up
private static void insert() throws IOException {
	String index = "test1";
	String type = "_doc";
	// 唯一编号
	String id = "1";
	IndexRequest request = new IndexRequest(index, type, id);
	/*
	 * 第一种方式,通过jsonString进行创建
	 */
	// json
	String jsonString = "{" + "\"uid\":\"1234\","+ "\"phone\":\"12345678909\","+ "\"msgcode\":\"1\"," + "\"sendtime\":\"2019-03-14 01:57:04\","
			+ "\"message\":\"xuwujing study Elasticsearch\"" + "}";
	request.source(jsonString, XContentType.JSON);

	/*
	 * 第二种方式,通过map创建,,会自动转换成json的数据
	 */
	Map<String, Object> jsonMap = new HashMap<>();
	jsonMap.put("uid", 1234);
	jsonMap.put("phone", 12345678909L);
	jsonMap.put("msgcode", 1);
	jsonMap.put("sendtime", "2019-03-14 01:57:04");
	jsonMap.put("message", "xuwujing study Elasticsearch");
	request.source(jsonMap);

	/*
	 * 第三种方式 : 通过XContentBuilder对象进行创建
	 */

	XContentBuilder builder = XContentFactory.jsonBuilder();
	builder.startObject();
	{
		builder.field("uid", 1234);
		builder.field("phone", 12345678909L);
		builder.field("msgcode", 1);
		builder.timeField("sendtime", "2019-03-14 01:57:04");
		builder.field("message", "xuwujing study Elasticsearch");
	}
	builder.endObject();
	request.source(builder);

	IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

	//如果是200则表示成功,否则就是失败
	if(200 == indexResponse.status().getStatus()){

	}

	// 对响应结果进行处理
	String index1 = indexResponse.getIndex();
	String type1 = indexResponse.getType();
	String id1 = indexResponse.getId();
	long version = indexResponse.getVersion();
	// 如果是新增/修改的话的话
	if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {

	} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {

	}
	ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
	if (shardInfo.getTotal() != shardInfo.getSuccessful()) {

	}
	if (shardInfo.getFailed() > 0) {
		for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
			String reason = failure.reason();
		}
	}
}