Java Code Examples for org.elasticsearch.action.update.UpdateRequest#type()

The following examples show how to use org.elasticsearch.action.update.UpdateRequest#type() . 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: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * 更新操作
 * 
 * @throws IOException
 */
private static void update() throws IOException {
	String type = "_doc";
	String index = "test1";
	// 唯一编号
	String id = "1";
	UpdateRequest upateRequest = new UpdateRequest();
	upateRequest.id(id);
	upateRequest.index(index);
	upateRequest.type(type);

	// 依旧可以使用Map这种集合作为更新条件
	Map<String, Object> jsonMap = new HashMap<>();
	jsonMap.put("uid", 12345);
	jsonMap.put("phone", 123456789019L);
	jsonMap.put("msgcode", 2);
	jsonMap.put("sendtime", "2019-03-14 01:57:04");
	jsonMap.put("message", "xuwujing study Elasticsearch");
	upateRequest.doc(jsonMap);
	// upsert 方法表示如果数据不存在,那么就新增一条
	upateRequest.docAsUpsert(true);
	client.update(upateRequest, RequestOptions.DEFAULT);
	System.out.println("更新成功!");

}
 
Example 2
Source File: ElasticsearchChannel.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static String toString(UpdateRequest request) {
  String res = "update {[" + request.index() + "][" + request.type() + "][" + request.id() + "]";
  if (request.docAsUpsert()) {
    res += (", doc_as_upsert[" + request.docAsUpsert() + "]");
  }
  if (request.doc() != null) {
    res += (", doc[" + request.doc() + "]");
  }
  if (request.script() != null) {
    res += (", script[" + request.script() + "]");
  }
  if (request.upsertRequest() != null) {
    res += (", upsert[" + request.upsertRequest() + "]");
  }
  if (request.scriptedUpsert()) {
    res += (", scripted_upsert[" + request.scriptedUpsert() + "]");
  }
  if (request.detectNoop()) {
    res += (", detect_noop[" + request.detectNoop() + "]");
  }
  if (request.fields() != null) {
    res += (", fields[" + Arrays.toString(request.fields()) + "]");
  }
  return res + "}";
}
 
Example 3
Source File: ElasticSearch.java    From hsweb-learning with Apache License 2.0 5 votes vote down vote up
private static void UpdateRequest(Client client) throws IOException, InterruptedException, ExecutionException {

        //当更新的内容不存在的时候会添加  
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index("twitter");
        updateRequest.type("tweet");
        updateRequest.id("3");
        XContentBuilder jsonBuilder =XContentFactory.jsonBuilder();
        updateRequest.doc(jsonBuilder
                .startObject()
                .field("message","我是林志颖啊")
                .field("user","lin")
                .endObject());
        UpdateResponse response= client.update(updateRequest).get();  
           
    /*   
     *  //当更新的内容不存在时候,不会添加 
        UpdateResponse response = client.prepareUpdate("twitter","tweet","1") 
                        .setDoc(jsonBuilder() 
                                 .startObject() 
                                   .field("users4","xue") 
                                 .endObject()) 
                                 .get();*/

        System.out.println(response.getVersion());


    }
 
Example 4
Source File: CommonWebpageDAO.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新网页
 *
 * @param webpage 网页
 * @return
 */
public boolean update(Webpage webpage) throws ExecutionException, InterruptedException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index(INDEX_NAME);
    updateRequest.type(TYPE_NAME);
    updateRequest.id(webpage.getId());
    updateRequest.doc(gson.toJson(webpage));
    UpdateResponse response = client.update(updateRequest).get();
    return response.getResult() == UpdateResponse.Result.UPDATED;
}
 
Example 5
Source File: CommonWebpageDAO.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 批量更新网页
 *
 * @param webpageList 网页列表
 * @return
 */
public boolean update(List<Webpage> webpageList) throws ExecutionException, InterruptedException {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (Webpage webpage : webpageList) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(INDEX_NAME);
        updateRequest.type(TYPE_NAME);
        updateRequest.id(webpage.getId());
        updateRequest.doc(gson.toJson(webpage));
        bulkRequest.add(updateRequest);
    }
    BulkResponse bulkResponse = bulkRequest.get();
    return bulkResponse.hasFailures();
}
 
Example 6
Source File: UpdateDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void testForClient() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("index");
    updateRequest.type("type");
    updateRequest.id("1");
    updateRequest.doc(XContentFactory.jsonBuilder()
            .startObject()
            .field("gender", "male")
            .endObject());
    client.update(updateRequest).get();
}
 
Example 7
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新网页
 *
 * @param webpage 网页
 * @return
 */
public boolean update(Webpage webpage) throws ExecutionException, InterruptedException {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index(INDEX_NAME);
    updateRequest.type(TYPE_NAME);
    updateRequest.id(webpage.getId());
    updateRequest.doc(gson.toJson(webpage));
    UpdateResponse response = client.update(updateRequest).get();
    return response.getResult() == UpdateResponse.Result.UPDATED;
}
 
Example 8
Source File: CommonWebpageDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 批量更新网页
 *
 * @param webpageList 网页列表
 * @return
 */
public boolean update(List<Webpage> webpageList) throws ExecutionException, InterruptedException {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (Webpage webpage : webpageList) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(INDEX_NAME);
        updateRequest.type(TYPE_NAME);
        updateRequest.id(webpage.getId());
        updateRequest.doc(gson.toJson(webpage));
        bulkRequest.add(updateRequest);
    }
    BulkResponse bulkResponse = bulkRequest.get();
    return bulkResponse.hasFailures();
}
 
Example 9
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
public void testUpdate() throws Exception {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.index("index");
    updateRequest.type("type");
    updateRequest.id("1");
    updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject());
    UpdateResponse response = client.update(updateRequest).get();
    System.out.println(response.toString());

    client.prepareUpdate("ttl", "doc", "1").setScript(new Script("ctx._source.gender = \"male\"", ScriptService.ScriptType.INLINE, null, null))
            .get();

    client.prepareUpdate("ttl", "doc", "1").setDoc(XContentFactory.jsonBuilder().startObject().field("gender", "male").endObject()).get();
}