Java Code Examples for org.elasticsearch.action.index.IndexResponse#getType()
The following examples show how to use
org.elasticsearch.action.index.IndexResponse#getType() .
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 |
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 |
@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 |
@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: TransportClient.java From elasticsearch-jest-example with MIT License | 5 votes |
/** * 打印索引信息 * @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 5
Source File: EsHighLevelRestTest1.java From java-study with Apache License 2.0 | 4 votes |
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(); } } }
Example 6
Source File: ESJavaRDDFT.java From deep-spark with Apache License 2.0 | 4 votes |
/** * Imports dataset * * @throws java.io.IOException */ private static void dataSetImport() throws IOException, ExecutionException, IOException, InterruptedException, ParseException { JSONParser parser = new JSONParser(); URL url = Resources.getResource(DATA_SET_NAME); Object obj = parser.parse(new FileReader(url.getFile())); JSONObject jsonObject = (JSONObject) obj; IndexResponse responseBook = client.prepareIndex(ES_INDEX_BOOK, ES_TYPE_INPUT, "id") .setSource(jsonObject.toJSONString()) .execute() .actionGet(); String json2 = "{" + "\"message\":\"" + MESSAGE_TEST + "\"" + "}"; IndexResponse response2 = client.prepareIndex(ES_INDEX_MESSAGE, ES_TYPE_MESSAGE).setCreate(true) .setSource(json2).setReplicationType(ReplicationType.ASYNC) .execute() .actionGet(); String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; IndexResponse response = client.prepareIndex(ES_INDEX, ES_TYPE).setCreate(true) .setSource(json) .execute() .actionGet(); String index = response.getIndex(); String _type = response.getType(); String _id = response.getId(); try { CountResponse countResponse = client.prepareCount(ES_INDEX).setTypes(ES_TYPE) .execute() .actionGet(); SearchResponse searchResponse = client.prepareSearch(ES_INDEX_BOOK).setTypes(ES_TYPE_INPUT) .execute() .actionGet(); //searchResponse.getHits().hits(); //assertEquals(searchResponse.getCount(), 1); } catch (AssertionError | Exception e) { cleanup(); e.printStackTrace(); } }