Java Code Examples for org.elasticsearch.action.get.GetResponse#getSource()

The following examples show how to use org.elasticsearch.action.get.GetResponse#getSource() . 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: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private static com.hubrick.vertx.elasticsearch.model.GetResult mapToGetResult(GetResponse getResponse) {
    final com.hubrick.vertx.elasticsearch.model.GetResult getResult = new com.hubrick.vertx.elasticsearch.model.GetResult()
            .setId(getResponse.getId())
            .setIndex(getResponse.getIndex())
            .setType(getResponse.getType())
            .setVersion(getResponse.getVersion())
            .setExists(getResponse.isExists());

    if (getResponse.getFields() != null) {
        getResult.setFields(
                getResponse.getFields()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().getValues()))
        );
    }

    if (getResponse.getSource() != null) {
        getResult.setSource(new JsonObject(getResponse.getSource()));
    }

    return getResult;
}
 
Example 2
Source File: AuditLoginsCounterBolt.java    From Kafka-Storm-ElasticSearch with Apache License 2.0 6 votes vote down vote up
private int getCounterValue(String host_user){

        try{
        	GetResponse response = client.prepareGet(index, type, host_user)
        	        .execute()
        	        .actionGet();
        	
        	Map<String, Object> test = response.getSource();
        	
        	return (int)test.get("counter");
        }
        catch (Exception e){
        	System.out.println("Error in get elasticSearch get: maybe index is still not created");
        	return 0;
        }
    }
 
Example 3
Source File: ScriptService.java    From elasticsearch-river-web with Apache License 2.0 6 votes vote down vote up
private String getScriptContent(final String lang, final String script, final ScriptType scriptType) {
    switch (scriptType) {
    case INLINE:
        return script;
    case FILE:
        if (Files.exists(Paths.get(script))) {
            return FileUtil.readText(new File(script));
        } else {
            return FileUtil.readText(script);
        }
    case INDEXED:
        final GetResponse response = esClient.prepareGet(SCRIPT_INDEX, lang, script).execute().actionGet();
        if (!response.isExists()) {
            throw new ScriptExecutionException("/" + SCRIPT_INDEX + "/" + lang + "/" + script + " does not exist.");
        }
        final Map<String, Object> source = response.getSource();
        if (source != null) {
            return (String) source.get("script");
        }
        break;
    default:
        break;
    }
    return null;
}
 
Example 4
Source File: ElasticsearchUtil.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
/**
 * 通过ID获取数据
 * @param index  索引,类似数据库
 * @param type   类型,类似表
 * @param id     数据ID
 * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
 * @return 结果
 */
public static Map<String, Object> searchDataById(String index, String type, String id, String fields) {
    GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);
    if (StringUtils.isNotEmpty(fields)) {
        getRequestBuilder.setFetchSource(fields.split(","), null);
    }
    GetResponse getResponse = getRequestBuilder.execute().actionGet();
    return getResponse.getSource();
}
 
Example 5
Source File: ElasticsearchRestResource.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
@Path("/get")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getData(@QueryParam("indexId") String indexId) throws Exception {
    GetResponse response = producerTemplate
            .requestBody("elasticsearch-rest://elasticsearch?operation=GetById&indexName=test", indexId, GetResponse.class);

    if (response.getSource() == null) {
        return Response.status(404).build();
    }
    return Response.ok().entity(response.getSource().get("test-key")).build();
}
 
Example 6
Source File: DocumentController.java    From blog-sample with Apache License 2.0 5 votes vote down vote up
/**
 * 查看数据
 * @author jitwxs
 * @since 2018/10/9 15:21
 */
@GetMapping("/{index}/{type}/{id}")
public ResultBean getDocument(@PathVariable String index, @PathVariable String type, @PathVariable String id) {
    GetResponse response = transportClient.prepareGet(index, type, id).get();

    Map<String, Object> source = response.getSource();

    return ResultBean.success("查询成功!", source);
}
 
Example 7
Source File: Application.java    From ElasticSearch with MIT License 5 votes vote down vote up
@GetMapping("/get/book/novel")
public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id) {
    GetResponse response = client.prepareGet(BOOK_INDEX, BOOK_TYPE_NOVEL, id).get();
    if (!response.isExists()) {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity(response.getSource(), HttpStatus.OK);
}
 
Example 8
Source File: ESOpt.java    From common-project with Apache License 2.0 5 votes vote down vote up
/**
 * 通过ID获取数据
 * @param index  索引,类似数据库
 * @param type   类型,类似表
 * @param id     数据ID
 * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
 * @return
 */
public static Map<String, Object> searchDataById(String index, String type, String id, String fields) {
    GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);

    if (!StringUtils.isEmpty(fields)) {
        getRequestBuilder.setFetchSource(fields.split(","), null);
    }

    GetResponse getResponse = getRequestBuilder.execute().actionGet();

    return getResponse.getSource();
}
 
Example 9
Source File: ElasticSearchController.java    From springboot-learn with MIT License 5 votes vote down vote up
@GetMapping("/get/book/novel")
public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id) {
    if (id.isEmpty()) {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
    // 查询结果
    GetResponse result = transportClient.prepareGet(INDEX, TYPE, id)
            .get();
    if (!result.isExists()) {
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity(result.getSource(), HttpStatus.OK);
}
 
Example 10
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns a Document representing the specified document ID (combination of resource and context), or null when no
 * such Document exists yet.
 */
@Override
protected SearchDocument getDocument(String id) throws IOException {
	GetResponse response = client.prepareGet(indexName, documentType, id).execute().actionGet();
	if (response.isExists()) {
		return new ElasticsearchDocument(response.getId(), response.getType(), response.getIndex(),
				response.getVersion(), response.getSource(), geoContextMapper);
	}
	// no such Document
	return null;
}