下面列出了怎么用org.elasticsearch.action.get.GetRequest的API类实例代码及写法,或者点击链接到github查看源代码。
public static void get(Map<String, Object> m) throws Exception{
GetRequest getRequest = new GetRequest(
"haha",
"doc",
"2");
String[] includes = new String[]{"message","user","*Date"};
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext =
new FetchSourceContext(true, includes, excludes);
getRequest.fetchSourceContext(fetchSourceContext);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
String index = getResponse.getIndex();
String type = getResponse.getType();
String id = getResponse.getId();
if (getResponse.isExists()) {
long version = getResponse.getVersion();
String sourceAsString = getResponse.getSourceAsString();
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap);
} else {
}
}
/**
* Get detector job for update/delete AD job.
* If AD job exist, will return error message; otherwise, execute function.
*
* @param clusterService ES cluster service
* @param client ES node client
* @param detectorId detector identifier
* @param channel ES rest channel
* @param function AD function
*/
public void getDetectorJob(
ClusterService clusterService,
NodeClient client,
String detectorId,
RestChannel channel,
AnomalyDetectorFunction function
) {
if (clusterService.state().metadata().indices().containsKey(ANOMALY_DETECTOR_JOB_INDEX)) {
GetRequest request = new GetRequest(ANOMALY_DETECTOR_JOB_INDEX).id(detectorId);
client.get(request, ActionListener.wrap(response -> onGetAdJobResponseForWrite(response, channel, function), exception -> {
logger.error("Fail to get anomaly detector job: " + detectorId, exception);
try {
channel.sendResponse(new BytesRestResponse(channel, exception));
} catch (IOException e) {
logger.error("Fail to send exception" + detectorId, e);
}
}));
} else {
function.execute();
}
}
private void parseRequestInfo(Object request, ElasticSearchEnhanceInfo enhanceInfo) {
// search request
if (request instanceof SearchRequest) {
parseSearchRequest(request, enhanceInfo);
return;
}
// get request
if (request instanceof GetRequest) {
parseGetRequest(request, enhanceInfo);
return;
}
// index request
if (request instanceof IndexRequest) {
parseIndexRequest(request, enhanceInfo);
return;
}
// update request
if (request instanceof UpdateRequest) {
parseUpdateRequest(request, enhanceInfo);
return;
}
// delete request
if (request instanceof DeleteRequest) {
parseDeleteRequest(request, enhanceInfo);
}
}
@Test
public void findDetectorMapping_successful() throws IOException {
List<Map<String, String>> tagsList = new ArrayList<>();
val detectorUuid = "aeb4d849-847a-45c0-8312-dc0fcf22b639";
String id = "adsvade8^szx";
Long LastModifiedTimeInMillis = new Long(1554828886);
Long CreatedTimeInMillis = new Long(1554828886);
GetResponse getResponse = mockGetResponse(id);
when(legacyElasticSearchClient.get(any(GetRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(getResponse);
DetectorMapping detectorMapping = repoUnderTest.findDetectorMapping(id);
verify(legacyElasticSearchClient, atLeastOnce()).get(any(GetRequest.class), eq(RequestOptions.DEFAULT));
assertNotNull("Response can't be null", detectorMapping);
assertEquals(id, detectorMapping.getId());
assertEquals("test-user", detectorMapping.getUser().getId());
assertEquals(LastModifiedTimeInMillis, Long.valueOf(detectorMapping.getLastModifiedTimeInMillis()));
assertEquals(CreatedTimeInMillis, Long.valueOf(detectorMapping.getCreatedTimeInMillis()));
assertTrue(detectorMapping.isEnabled());
assertEquals(UUID.fromString(detectorUuid), detectorMapping.getDetector().getUuid());
}
@Override
public String getDocumentText(DocumentRepository aRepository,
ElasticSearchProviderTraits aTraits, String aCollectionId, String aDocumentId)
throws IOException
{
if (!aCollectionId.equals(aTraits.getIndexName())) {
throw new IllegalArgumentException(
"Requested collection name does not match connection collection name");
}
GetRequest getRequest = new GetRequest(aTraits.getIndexName(), aTraits.getObjectType(),
aDocumentId);
try (RestHighLevelClient client = makeClient(aTraits)) {
// Send get query
Map<String, Object> result = client.get(getRequest).getSourceAsMap();
Map<String, String> document = (Map) result.get(ELASTIC_HIT_DOC_KEY);
return (document.get(DOC_TEXT_KEY));
}
}
@Test
public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception {
String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}";
IndexRequest indexRequest = new IndexRequest("people");
indexRequest.source(jsonObject, XContentType.JSON);
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
String id = response.getId();
GetRequest getRequest = new GetRequest("people");
getRequest.id(id);
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println(getResponse.getSourceAsString());
DeleteRequest deleteRequest = new DeleteRequest("people");
deleteRequest.id(id);
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
assertEquals(Result.DELETED, deleteResponse.getResult());
}
/**
* @return boolean
* @Author pancm
* @Description 根据id查询
* @Date 2019/3/21
* @Param []
**/
public static Map<String, Object> queryById(String index, String type, String id) throws IOException {
if (index == null || type == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
try {
GetRequest request = new GetRequest();
request.index(index);
request.type(type);
request.id(id);
GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
// 如果存在该数据则返回对应的结果
if (getResponse.isExists()) {
map = getResponse.getSourceAsMap();
}
} finally {
if (isAutoClose) {
close();
}
}
return map;
}
/**
* Retrieve an indexed Fact by its UUID. Returns NULL if Fact cannot be fetched from ElasticSearch.
*
* @param id UUID of indexed Fact
* @return Indexed Fact or NULL if not available
*/
public FactDocument getFact(UUID id) {
if (id == null) return null;
GetResponse response;
try {
GetRequest request = new GetRequest(INDEX_NAME, TYPE_NAME, id.toString());
response = clientFactory.getClient().get(request, RequestOptions.DEFAULT);
} catch (ElasticsearchException | IOException ex) {
throw logAndExit(ex, String.format("Could not perform request to fetch Fact with id = %s.", id));
}
if (response.isExists()) {
LOGGER.info("Successfully fetched Fact with id = %s.", id);
return decodeFactDocument(id, response.getSourceAsBytes());
} else {
// Fact isn't indexed in ElasticSearch, log warning and return null.
LOGGER.warning("Could not fetch Fact with id = %s. Fact not indexed?", id);
return null;
}
}
private Integer getCurrentVersion() throws IOException {
IndexResponse resp =
getClient().index(new IndexRequest(settings.getIndexId()).id(MetadataDataMapping.METADATA_ROW_ID),
RequestOptions.DEFAULT);
if (getClient().exists(new GetRequest(settings.getIndexId()), RequestOptions.DEFAULT)) {
// Long versionString = resp.getVersion();
// if (versionString == null) {
// throw new ElasticsearchException(String.format(
// "Database inconsistency. Version can't be found in row %s/%s/%s",
// settings.getIndexId(),
// MetadataDataMapping.METADATA_TYPE_NAME,
// MetadataDataMapping.METADATA_ROW_ID));
// }
return ((Long) resp.getVersion()).intValue();
} else {
return null;
}
}
@SuppressWarnings("unchecked")
@Test
public void addnewUuidOnConnect() throws IOException {
adminHandler.createSchema();
clientSettings.setUuid("lofasz janos");
adminHandler.createSchema();
GetResponse resp = getEmbeddedClient()
.get(new GetRequest(clientSettings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME,
MetadataDataMapping.METADATA_ROW_ID), RequestOptions.DEFAULT);
Map<String, Object> map = resp.getSourceAsMap();
Assertions.assertNotNull(map.get(MetadataDataMapping.METADATA_CREATION_TIME_FIELD.getName()));
Assertions.assertNotNull(map.get(MetadataDataMapping.METADATA_UUIDS_FIELD.getName()));
Assertions.assertNotNull(map.get(MetadataDataMapping.METADATA_UPDATE_TIME_FIELD.getName()));
List<String> object = (List<String>) map.get(MetadataDataMapping.METADATA_UUIDS_FIELD.getName());
Assertions.assertEquals(2, object.size());
MatcherAssert.assertThat(object, CoreMatchers.hasItem("lofasz janos"));
}
@Test
public void connectTransportMode() throws InterruptedException, IOException {
settings.setNodeConnectionMode(ElasticsearchSettingsKeys.CONNECTION_MODE_TRANSPORT_CLIENT);
adminHandler.init();
Map<String, Object> data = new HashMap<>();
data.put("test", "test-string");
IndexResponse idx = dataHandler.persist(data);
Thread.sleep(2000);
String ret = getEmbeddedClient().get(new GetRequest(idx.getIndex(), idx.getType(),
idx.getId()), RequestOptions.DEFAULT).getSourceAsString();
Assertions.assertNotNull(ret);
}
@Test
public void connectEmbeddedMode() throws Exception {
settings.setNodeConnectionMode(ElasticsearchSettingsKeys.CONNECTION_MODE_EMBEDDED_SERVER);
adminHandler.init();
Map<String, Object> data = new HashMap<>();
data.put("test", "test-string");
IndexResponse idx = dataHandler.persist(data);
Thread.sleep(2000);
String ret = dataHandler.getClient().get(new GetRequest(idx.getIndex(), idx.getId()), RequestOptions.DEFAULT)
.getSourceAsString();
Assertions.assertNotNull(ret);
adminHandler.destroy();
try {
FileUtils.deleteDirectory(new File("./elasticsearch"));
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
getRequest.operationThreaded(true);
getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
getRequest.routing(request.param("routing")); // order is important, set it after routing, so it will set the routing
getRequest.parent(request.param("parent"));
getRequest.preference(request.param("preference"));
getRequest.realtime(request.paramAsBoolean("realtime", null));
// don't get any fields back...
getRequest.fields(Strings.EMPTY_ARRAY);
// TODO we can also just return the document size as Content-Length
client.get(getRequest, new RestResponseListener<GetResponse>(channel) {
@Override
public RestResponse buildResponse(GetResponse response) {
if (!response.isExists()) {
return new BytesRestResponse(NOT_FOUND);
} else {
return new BytesRestResponse(OK);
}
}
});
}
void parseExistingDocPercolate(PercolateRequest percolateRequest, RestRequest restRequest, RestChannel restChannel, final Client client) {
String index = restRequest.param("index");
String type = restRequest.param("type");
percolateRequest.indices(Strings.splitStringByCommaToArray(restRequest.param("percolate_index", index)));
percolateRequest.documentType(restRequest.param("percolate_type", type));
GetRequest getRequest = new GetRequest(index, type,
restRequest.param("id"));
getRequest.routing(restRequest.param("routing"));
getRequest.preference(restRequest.param("preference"));
getRequest.refresh(restRequest.paramAsBoolean("refresh", getRequest.refresh()));
getRequest.realtime(restRequest.paramAsBoolean("realtime", null));
getRequest.version(RestActions.parseVersion(restRequest));
getRequest.versionType(VersionType.fromString(restRequest.param("version_type"), getRequest.versionType()));
percolateRequest.getRequest(getRequest);
percolateRequest.routing(restRequest.param("percolate_routing"));
percolateRequest.preference(restRequest.param("percolate_preference"));
percolateRequest.source(RestActions.getRestContent(restRequest));
percolateRequest.indicesOptions(IndicesOptions.fromRequest(restRequest, percolateRequest.indicesOptions()));
executePercolate(percolateRequest, restChannel, client);
}
/**
* Maps document Object to Json and creates and new index request
*
* @param indexName
* - index name
* @param documentType
* - document type
* @param uid
* - unique identifier
* @param object
* - object that represents the structure of a document for indexing
* @return
* @return IndexResponse
* @throws IOException
*/
private static void index(String indexName, String documentType, String uid, String document) {
try {
GetRequest getRequest = new GetRequest(indexName, documentType, uid).fetchSourceContext(fetchSourceContext).storedFields("_none_");
if(highLevelClient.exists(getRequest, getReadHeaders()))
{
UpdateRequest request = new UpdateRequest(indexName, documentType, uid);
request.doc(document, XContentType.JSON);
logger.info("Document (uid: " + uid + ") has been updated");
}
else
{
IndexRequest indexRequest = new IndexRequest();
indexRequest .index(indexName).type(documentType).id(uid).source(document,XContentType.JSON);
logger.info("Document (uid: " + uid + ") has been " +
highLevelClient.index(indexRequest, getWriteHeaders()).getResult().toString().toLowerCase());
}
} catch (IOException io) {
logger.error("Method index has experienced an IO error\n" + io);
}
}
@Override
public String get(String workflowInstanceId, String fieldToGet) {
GetRequest request = new GetRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId)
.fetchSourceContext(
new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY));
GetResponse response = elasticSearchClient.get(request).actionGet();
if (response.isExists()) {
Map<String, Object> sourceAsMap = response.getSourceAsMap();
if (sourceAsMap.containsKey(fieldToGet)) {
return sourceAsMap.get(fieldToGet).toString();
}
}
logger.info("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, indexName);
return null;
}
@Override
public String get(String workflowInstanceId, String fieldToGet) {
String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
GetRequest request = new GetRequest(workflowIndexName, docType, workflowInstanceId);
GetResponse response;
try {
response = elasticSearchClient.get(request);
} catch (IOException e) {
logger
.error("Unable to get Workflow: {} from ElasticSearch index: {}", workflowInstanceId, workflowIndexName,
e);
return null;
}
if (response.isExists()) {
Map<String, Object> sourceAsMap = response.getSourceAsMap();
if (sourceAsMap.get(fieldToGet) != null) {
return sourceAsMap.get(fieldToGet).toString();
}
}
logger.debug("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, workflowIndexName);
return null;
}
@Override
public String get(String workflowInstanceId, String fieldToGet) {
String docType = StringUtils.isBlank(docTypeOverride) ? WORKFLOW_DOC_TYPE : docTypeOverride;
GetRequest request = new GetRequest(workflowIndexName, docType, workflowInstanceId)
.fetchSourceContext(new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY));
GetResponse response = elasticSearchClient.get(request).actionGet();
if (response.isExists()) {
Map<String, Object> sourceAsMap = response.getSourceAsMap();
if (sourceAsMap.get(fieldToGet) != null) {
return sourceAsMap.get(fieldToGet).toString();
}
}
LOGGER.debug("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, workflowIndexName);
return null;
}
@Test
public void test_simple_write() throws Exception {
final TikaDocument document = new DocumentFactory().withIdentifier(new PathIdentifier()).create(get("test-file.txt"));
final ParsingReader reader = new ParsingReader(new ByteArrayInputStream("test".getBytes()));
document.setReader(reader);
spewer.write(document);
GetResponse documentFields = es.client.get(new GetRequest(TEST_INDEX, "doc", document.getId()));
assertThat(documentFields.isExists()).isTrue();
assertThat(documentFields.getId()).isEqualTo(document.getId());
assertEquals(new HashMap<String, String>() {{
put("name", "Document");
}}, documentFields.getSourceAsMap().get("join"));
ArgumentCaptor<Message> argument = ArgumentCaptor.forClass(Message.class);
verify(publisher).publish(eq(Channel.NLP), argument.capture());
assertThat(argument.getValue().content).includes(entry(Field.DOC_ID, document.getId()));
}
@Test
public void test_embedded_document() throws Exception {
Path path = get(getClass().getResource("/docs/embedded_doc.eml").getPath());
final TikaDocument document = new Extractor().extract(path);
spewer.write(document);
GetResponse documentFields = es.client.get(new GetRequest(TEST_INDEX, "doc", document.getId()));
assertTrue(documentFields.isExists());
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.multiMatchQuery("simple.tiff", "content"));
searchRequest.source(searchSourceBuilder);
SearchResponse response = es.client.search(searchRequest);
assertThat(response.getHits().totalHits).isGreaterThan(0);
//assertThat(response.getHits().getAt(0).getId()).endsWith("embedded.pdf");
verify(publisher, times(2)).publish(eq(Channel.NLP), any(Message.class));
}
/**
* 根据ID查询
*
* @param id
* @return com.example.common.ResponseBean
* @throws IOException
* @author wliduo[[email protected]]
* @date 2019/8/15 14:10
*/
@GetMapping("/book/{id}")
public ResponseBean getById(@PathVariable("id") String id) throws IOException {
// GetRequest
GetRequest getRequest = new GetRequest(Constant.INDEX, id);
// 查询ES
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
BookDto bookDto = JSON.parseObject(getResponse.getSourceAsString(), BookDto.class);
return new ResponseBean(HttpStatus.OK.value(), "查询成功", bookDto);
}
/**
* Returns the checkpoint for the model.
*
* @deprecated use getModelCheckpoint with listener instead
*
* @param modelId ID of the model
* @return model checkpoint, or empty if not found
*/
@Deprecated
public Optional<String> getModelCheckpoint(String modelId) {
return clientUtil
.<GetRequest, GetResponse>timedRequest(new GetRequest(indexName, DOC_TYPE, modelId), logger, client::get)
.filter(GetResponse::isExists)
.map(GetResponse::getSource)
.map(source -> (String) source.get(FIELD_MODEL));
}
private void parseGetRequest(Object request, ElasticSearchEnhanceInfo enhanceInfo) {
GetRequest getRequest = (GetRequest) request;
enhanceInfo.setIndices(StringUtil.join(',', getRequest.indices()));
enhanceInfo.setTypes(getRequest.type());
if (TRACE_DSL) {
enhanceInfo.setSource(getRequest.toString());
}
}
private void getAnomalyDetectorJobForWrite(AnomalyDetectorJob job) {
GetRequest getRequest = new GetRequest(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX).id(detectorId);
client
.get(
getRequest,
ActionListener.wrap(response -> onGetAnomalyDetectorJobForWrite(response, job), exception -> onFailure(exception))
);
}
public void getAnomalyDetector(String adID, ActionListener<Optional<AnomalyDetector>> listener) {
Entry<AnomalyDetector, Instant> detectorAndTime = currentDetectors.get(adID);
if (detectorAndTime != null) {
detectorAndTime.setValue(clock.instant());
listener.onResponse(Optional.of(detectorAndTime.getKey()));
return;
}
GetRequest request = new GetRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX, adID);
clientUtil.<GetRequest, GetResponse>asyncRequest(request, client::get, onGetResponse(adID, listener));
}
@Test
public void getModelCheckpoint_returnEmpty_whenDocNotFound() {
doReturn(Optional.of(getResponse))
.when(clientUtil)
.timedRequest(anyObject(), anyObject(), Matchers.<BiConsumer<GetRequest, ActionListener<GetResponse>>>anyObject());
when(getResponse.isExists()).thenReturn(false);
Optional<String> result = checkpointDao.getModelCheckpoint(modelId);
assertFalse(result.isPresent());
}
@Test
public void testRunAdJobWithEndRunExceptionNowAndGetJobException() {
LockModel lock = new LockModel("indexName", "jobId", Instant.now(), 10, false);
Exception exception = new EndRunException(jobParameter.getName(), randomAlphaOfLength(5), true);
doAnswer(invocation -> {
ActionListener<GetResponse> listener = invocation.getArgument(2);
listener.onFailure(new RuntimeException("test"));
return null;
}).when(clientUtil).asyncRequest(any(GetRequest.class), any(), any());
runner.handleAdException(jobParameter, lockService, lock, Instant.now().minusMillis(1000 * 60), Instant.now(), exception);
verify(anomalyResultHandler).indexAnomalyResult(any());
assertEquals(1, testAppender.countMessage("JobRunner failed to get detector job"));
}
@Test
public void testRunAdJobWithEndRunExceptionNowAndFailToGetJob() {
LockModel lock = new LockModel("indexName", "jobId", Instant.now(), 10, false);
Exception exception = new EndRunException(jobParameter.getName(), randomAlphaOfLength(5), true);
doThrow(new RuntimeException("fail to get AD job")).when(clientUtil).asyncRequest(any(GetRequest.class), any(), any());
runner.handleAdException(jobParameter, lockService, lock, Instant.now().minusMillis(1000 * 60), Instant.now(), exception);
verify(anomalyResultHandler).indexAnomalyResult(any());
assertEquals(1, testAppender.countMessage("JobRunner failed to stop AD job"));
}
@SneakyThrows
private void initDependencies() {
Mockito.when(elasticsearchUtil.getSourceBuilder(any(QueryBuilder.class))).thenReturn(new SearchSourceBuilder());
Mockito.when(elasticsearchUtil.getSearchRequest(any(SearchSourceBuilder.class), anyString(), anyString())).thenReturn(new SearchRequest());
Mockito.when(elasticsearchUtil.index(any(IndexRequest.class), anyString())).thenReturn(indexResponse);
Mockito.when(objectMapperUtil.convertToString(any())).thenReturn("");
Mockito.when(objectMapperUtil.convertToObject(anyString(), any())).thenReturn(detector);
Mockito.when(legacyElasticSearchClient.search(any(SearchRequest.class), any(RequestOptions.class))).thenReturn(searchResponse);
Mockito.when(legacyElasticSearchClient.get(any(GetRequest.class), any(RequestOptions.class))).thenReturn(getResponse);
Mockito.when(legacyElasticSearchClient.delete(any(DeleteRequest.class), any(RequestOptions.class))).thenReturn(deleteResponse);
Mockito.when(legacyElasticSearchClient.update(any(UpdateRequest.class), any(RequestOptions.class))).thenReturn(new UpdateResponse());
}
@Test
public void disableDetectorMapping() throws IOException {
val id = "adsvade8^szx";
GetResponse getResponse = mockGetResponse(id);
when(legacyElasticSearchClient.get(any(GetRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(getResponse);
repoUnderTest.disableDetectorMapping(id);
verify(legacyElasticSearchClient, atLeastOnce()).get(any(GetRequest.class), eq(RequestOptions.DEFAULT));
}