GetResponse gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(true).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(false).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(false).get(); Assert.assertFalse(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(true).get(); Assert.assertFalse(gr.isExists()); try { tc.threadPool().getThreadContext().putHeader("sg_impersonate_as", "worf"); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); } finally { ctx.close(); Header header = encodeBasicHeader("worf", "worf"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) { Header header = encodeBasicHeader("worf", "worf111"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) {
GetResponse gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(true).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("vulcan", "secrets", "s1").setRealtime(false).get(); Assert.assertTrue(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(false).get(); Assert.assertFalse(gr.isExists()); gr =tc.prepareGet("searchguard", "sg", "config").setRealtime(true).get(); Assert.assertFalse(gr.isExists()); try { tc.threadPool().getThreadContext().putHeader("sg_impersonate_as", "worf"); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); } finally { ctx.close(); Header header = encodeBasicHeader("worf", "worf"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) { Header header = encodeBasicHeader("worf", "worf111"); tc.threadPool().getThreadContext().putHeader(header.getName(), header.getValue()); gr = tc.prepareGet("vulcan", "secrets", "s1").get(); Assert.fail(); } catch (ElasticsearchSecurityException e) {
try get = txClient.prepareGet(metadata.getSchema().toLowerCase(), metadata.getTableName(), keyAsString) .setOperationThreaded(false).execute().get();
/** * 获取单个对象(ID) * * @param transportClient * @throws IOException */ private static void query(TransportClient transportClient) throws IOException { GetResponse getResponse = transportClient.prepareGet("product_index", "product", "1").get(); logger.info("--------------------------------:" + getResponse.getSourceAsString()); }
@Override public GetResponse get(String indexName, String type, String id) { GetResponse response = client.prepareGet(indexName, type, id).execute().actionGet(); return response; } }
/** * http://localhost:8080/get/book/novel?id=2 * 获取Es中数据 * @param id * @return */ @GetMapping(value = "/get/book/novel") @ResponseBody public ResponseEntity getBook(@RequestParam("id") String id) { if (StringUtils.isEmpty(id)) { return new ResponseEntity("ID不能为空!", HttpStatus.NOT_FOUND); } GetResponse result = client.prepareGet("book", "novel", id).get(); if (!result.isExists()) { return new ResponseEntity("资源没有找到!", HttpStatus.NOT_FOUND); } return new ResponseEntity(result.getSource(), HttpStatus.OK); }
/** * 通过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(); }
@Override public Object find(String id) { GetResponse response =transportClient.prepareGet(index, type, id).get(); System.out.println("response="+response); Map<String, Object> result = response.getSource(); if(result != null) { result.put("_id", response.getId()); } return result; }
/** * 获取数据 * ElasticSearch提供了根据索引名称,类别,文档ID来获取数据 */ @Test public void testGet(){ GetResponse getResponse=client.prepareGet("twitter", "tweet", "1").get(); System.out.println(getResponse.getSourceAsString()); }
/** * Check if a document exists in E.S. * * @param index * Index Name * @param id * Document ID * @return true/false * @throws ElasticsearchException */ private Boolean doesExist(String index, String id) { if (StringUtils.isEmpty(id)) { return false; } GetResponse response = client.prepareGet(index, index, id).setRefresh(true).get(); return response.isExists(); }
/** * 查询一条索引 * @param index * @param type * @param id */ public static GetResponse prepareGet(String index, String type, String id){ GetResponse response = getInstance().prepareGet(index, type, id).setOperationThreaded(true).get(); return response; }
@Override public Metadata searchById(Optional<String> index, String id) throws NotFoundException { log.debug("Search in ElasticSearch by ID [index: {}, ID: {}]", index, id); // Validation if (StringUtils.isEmpty(id)) throw new IllegalArgumentException("id" + ERROR_NOT_NULL_OR_EMPTY); try { String indexFormatted = formatIndex(index); GetResponse response = client.prepareGet(indexFormatted, indexFormatted, id).get(); log.trace("Search one document in ElasticSearch [index: {}, ID: {}] : response= {}", indexFormatted, id, response); if (!response.isExists()) { throw new NotFoundException( "Document [index: " + indexFormatted + ", ID: " + id + "] not found"); } Metadata metadata = convert(response.getIndex(), response.getId(), response.getSourceAsMap()); log.debug("Search in ElasticSearch by ID [index: {}, ID: {}]: {}", index, id, metadata); return metadata; } catch (NotFoundException ex) { log.warn("Error while searching into ElasticSearch [index: {}, ID: {}]", index, id, ex); throw ex; } catch (Exception ex) { log.error("Error while searching into ElasticSearch [index: {}, ID: {}]", index, id, ex); throw new TechnicalException( "Error while searching into ElasticSearch: " + ex.getMessage()); } }
/** * 根据id查询 * * @param id 主键id * @param index 索引 * @param type 类型 * @return 返回查询得到的 */ public String searchById(String index, String type, String id) { String jsonString = null; try { GetResponse response = client.prepareGet() .setId(id) .setIndex(index) .setType(type) .execute() .actionGet(); jsonString = response.getSourceAsString(); } catch (Exception e) { EsLogUtils.error("{},ES查询报错:索引:{};id:{};{}", type, index, id, e.getMessage()); return jsonString; } return jsonString; }
/** * 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; }
private GetResponse getParentDocument(IndexInfo indexInfo, String elementId) { try { GetResponse response = getClient() .prepareGet(indexInfo.getIndexName(), ELEMENT_TYPE, elementId) .setFields(getParentDocumentFields()) .execute() .get(); if (!response.isExists()) { return null; } return response; } catch (Exception ex) { throw new VertexiumException("Could not get parent document: " + elementId, ex); } }
private GetResponse getParentDocument(IndexInfo indexInfo, String elementId) { try { GetResponse response = getClient() .prepareGet(indexInfo.getIndexName(), ELEMENT_TYPE, elementId) .setFields(getParentDocumentFields()) .execute() .get(); if (!response.isExists()) { return null; } return response; } catch (Exception ex) { throw new SecureGraphException("Could not get parent document: " + elementId, ex); } }
String id = ElasticsearchMetadataUtil.getId(metadata); GetRequestBuilder getRequestBuilder = elasticsearchClientManager.client().prepareGet(index, type, id); getRequestBuilder.setFields("*", "_timestamp"); getRequestBuilder.setFetchSource(true);
@Override public List<StreamsDatum> process(StreamsDatum entry) { List<StreamsDatum> result = new ArrayList<>(); if (entry == null || entry.getMetadata() == null) { return result; } Map<String, Object> metadata = entry.getMetadata(); String index = ElasticsearchMetadataUtil.getIndex(metadata, config); String type = ElasticsearchMetadataUtil.getType(metadata, config); String id = ElasticsearchMetadataUtil.getId(entry); GetRequestBuilder getRequestBuilder = elasticsearchClientManager.client().prepareGet(index, type, id); getRequestBuilder.setFields("*", "_timestamp"); getRequestBuilder.setFetchSource(true); GetResponse getResponse = getRequestBuilder.get(); if ( getResponse == null || !getResponse.isExists() || getResponse.isSourceEmpty() ) { return result; } entry.setDocument(getResponse.getSource()); if ( getResponse.getField("_timestamp") != null) { DateTime timestamp = new DateTime(((Long) getResponse.getField("_timestamp").getValue()).longValue()); entry.setTimestamp(timestamp); } result.add(entry); return result; }
.prepareGet(metadata.getSchema().toLowerCase(), metadata.getTableName(), keyAsString).setOperationThreaded(false).execute().get();
try get = txClient.prepareGet(metadata.getSchema().toLowerCase(), metadata.getTableName(), keyAsString) .setOperationThreaded(false).execute().get();