Java Code Examples for org.elasticsearch.action.update.UpdateResponse#getId()

The following examples show how to use org.elasticsearch.action.update.UpdateResponse#getId() . 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: TransportClient.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
/**
 * 更新索引
 * @param index
 * @param type
 * @param id
 */
private static void updateIndexByDoc(String index, String type, String id) throws Exception{
	Client client = createTransportClient();
	UpdateResponse response = client.prepareUpdate(index, type, id)
			.setDoc(jsonBuilder()
					.startObject()
					.field("name", "Hadoop权威指南(第3版 修订版)")
					.field("author", "Tom White")
					.field("pubinfo", "清华大学出版社")
					.field("pubtime", "2015-01-01")
					.field("desc", "《Hadoop权威指南(第3版 修订版)》通过丰富的案例学习来解释Hadoop的幕后机理,阐述了Hadoop如何解决现实生活中的具体问题。第3版覆盖Hadoop的最新动态,包括新增的MapReduceAPI,以及MapReduce2及其灵活性更强的执行模型(YARN)")
					.endObject())
			.execute()
			.actionGet();
	System.out.println("索引是否更新:"+response.isCreated());
	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 2
Source File: ElasticSearchController.java    From springboot-learn with MIT License 5 votes vote down vote up
@PutMapping("update/book/novel")
public ResponseEntity add(@RequestParam(name = "id") String id,
                          @RequestParam(name = "title", required = false) String title,
                          @RequestParam(name = "author", required = false) String author,
                          @RequestParam(name = "wordCount", required = false) String wordCount,
                          @RequestParam(name = "publishDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date publishDate) {
    try {
        UpdateRequest updateRequest = new UpdateRequest(INDEX, TYPE, id);
        // 构建文档
        XContentBuilder contentBuilder = XContentFactory.jsonBuilder()
                .startObject();
        if (title != null) {
            contentBuilder.field("title", title);
        }
        if (author != null) {
            contentBuilder.field("author", author);
        }
        if (wordCount != null) {
            contentBuilder.field("word_count", wordCount);
        }
        if (publishDate != null) {
            contentBuilder.field("publish_date", publishDate.getTime());
        }
        contentBuilder.endObject();
        updateRequest.doc(contentBuilder);
        UpdateResponse result = transportClient.update(updateRequest).get();
        return new ResponseEntity(result.getId(), HttpStatus.OK);
    } catch (Exception e) {
        LOGGER.error("update error", e);
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
 
Example 3
Source File: SpiderInfoDAO.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 更新爬虫模板
 *
 * @param spiderInfo 爬虫模板实体
 * @return 爬虫模板id
 * @throws ExecutionException
 * @throws InterruptedException
 */
public String update(SpiderInfo spiderInfo) throws Exception {
    Preconditions.checkArgument(StringUtils.isNotBlank(spiderInfo.getId()), "待更新爬虫模板id不可为空");
    UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, TYPE_NAME, spiderInfo.getId());
    updateRequest.doc(gson.toJson(spiderInfo));
    UpdateResponse updateResponse = null;
    try {
        updateResponse = client.update(updateRequest).get();
        return updateResponse.getId();
    } catch (ExecutionException e) {
        e.printStackTrace();
        throw new Exception("没有此ID的模板,请删除ID字段的值或者使用正确的id值");
    }
}
 
Example 4
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
 *
 * @param index
 * @param type
 * @param id
 * @throws Exception
 */
private static void upsertIndex(String index,String type,String id) throws Exception{
	Client client = createTransportClient();
	IndexRequest indexRequest = new IndexRequest(index, type, id)
			.source(jsonBuilder()
					.startObject()
					.field("name", "Hadoop权威指南(第3版 修订版)")
					.field("author", "Tom White")
					.field("pubinfo", "清华大学出版社")
					.field("pubtime", "2015-01-01")
					.field("desc", "《Hadoop权威指南(第3版 修订版)》通过丰富的案例学习来解释Hadoop的幕后机理,阐述了Hadoop如何解决现实生活中的具体问题。第3版覆盖Hadoop的最新动态,包括新增的MapReduceAPI,以及MapReduce2及其灵活性更强的执行模型(YARN)")
					.endObject());
	UpdateRequest updateRequest = new UpdateRequest(index, type, id)
			.doc(jsonBuilder()
					.startObject()
					.field("author", "华东师范大学数据科学与工程学院")
					.endObject())
			.upsert(indexRequest);
	UpdateResponse response = client.update(updateRequest).get();
	System.out.println("索引是否更新:"+response.isCreated());
	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: TransportClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
	 * 更新索引  https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
	 * @param index
	 * @param type
	 * @param id
	 */
	private static void updateIndexByScript(String index, String type, String id) throws Exception{
		Client client = createTransportClient();
		UpdateResponse response = client.prepareUpdate(index, type, id)
				.setScript("ctx._source.author = \"闫洪磊\";" +
						"ctx._source.name = \"Activiti实战\";" +
						"ctx._source.pubinfo = \"机械工业出版社\";" +
						"ctx._source.pubtime = \"2015-01-01\";" +
						"ctx._source.desc = \"《Activiti实战 》立足于实践,不仅让读者知其然,全面掌握Activiti架构、功能、用法、技巧和最佳实践,广度足够;而且让读者知其所以然,深入理解Activiti的源代码实现、设计模式和PVM,深度也足够。《Activiti实战 》一共四个部分:准备篇(1~2章)介绍了Activiti的概念、特点、应用、体系结构,以及开发环境的搭建和配置;基础篇(3~4章)首先讲解了Activiti Modeler、Activiti Designer两种流程设计工具的详细使用,然后详细讲解了BPMN2.0规范;实战篇(5~14章)系统讲解了Activiti的用法、技巧和最佳实践,包含流程定义、流程实例、任务、子流程、多实例、事件以及监听器等;高级篇(15~21)通过集成WebService、规则引擎、JPA、ESB等各种服务和中间件来阐述了Activiti不仅仅是引擎,实际上是一个BPM平台,最后还通过源代码对它的设计模式及PVM进行了分析。\"", ScriptService.ScriptType.INLINE)
//				.setScript("ctx._source.author = \"闫洪磊\"", ScriptService.ScriptType.INLINE)
//				.setScript("ctx._source.name = \"Activiti实战\"", ScriptService.ScriptType.INLINE)
//				.setScript("ctx._source.pubinfo = \"机械工业出版社\"", ScriptService.ScriptType.INLINE)
//				.setScript("ctx._source.pubtime = \"2015-01-01\"", ScriptService.ScriptType.INLINE)
//				.setScript("ctx._source.desc = \"《Activiti实战 》立足于实践,不仅让读者知其然,全面掌握Activiti架构、功能、用法、技巧和最佳实践,广度足够;而且让读者知其所以然,深入理解Activiti的源代码实现、设计模式和PVM,深度也足够。《Activiti实战 》一共四个部分:准备篇(1~2章)介绍了Activiti的概念、特点、应用、体系结构,以及开发环境的搭建和配置;基础篇(3~4章)首先讲解了Activiti Modeler、Activiti Designer两种流程设计工具的详细使用,然后详细讲解了BPMN2.0规范;实战篇(5~14章)系统讲解了Activiti的用法、技巧和最佳实践,包含流程定义、流程实例、任务、子流程、多实例、事件以及监听器等;高级篇(15~21)通过集成WebService、规则引擎、JPA、ESB等各种服务和中间件来阐述了Activiti不仅仅是引擎,实际上是一个BPM平台,最后还通过源代码对它的设计模式及PVM进行了分析。\"", ScriptService.ScriptType.INLINE)
				.execute()
				.actionGet();
		System.out.println("索引是否更新:"+response.isCreated());
		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: SpiderInfoDAO.java    From Gather-Platform with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 更新爬虫模板
 *
 * @param spiderInfo 爬虫模板实体
 * @return 爬虫模板id
 * @throws ExecutionException
 * @throws InterruptedException
 */
public String update(SpiderInfo spiderInfo) throws ExecutionException, InterruptedException {
    Preconditions.checkArgument(StringUtils.isNotBlank(spiderInfo.getId()), "待更新爬虫模板id不可为空");
    UpdateRequest updateRequest = new UpdateRequest(INDEX_NAME, TYPE_NAME, spiderInfo.getId());
    updateRequest.doc(gson.toJson(spiderInfo));
    UpdateResponse updateResponse = client.update(updateRequest).get();
    return updateResponse.getId();
}