org.elasticsearch.action.get.GetRequest Java Examples

The following examples show how to use org.elasticsearch.action.get.GetRequest. 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: EsUtil.java    From java-study with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
}
 
Example #2
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: AnomalyDetectorActionHandler.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example #4
Source File: ElasticsearchSpewerTest.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
@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()));
}
 
Example #5
Source File: ElasticsearchSpewerTest.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
@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));
}
 
Example #6
Source File: Test.java    From dht-spider with MIT License 6 votes vote down vote up
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 {

    }
}
 
Example #7
Source File: ElasticSearchRestDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #8
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #9
Source File: Indexer.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #10
Source File: RestPercolateAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #11
Source File: RestHeadAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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);
            }
        }
    });
}
 
Example #12
Source File: ElasticSearchProvider.java    From inception with Apache License 2.0 6 votes vote down vote up
@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));
    }
}
 
Example #13
Source File: EmbeddedServerIT.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #14
Source File: ElasticsearchAdminHandlerIT.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #15
Source File: ElasticsearchAdminHandlerIT.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #16
Source File: ElasticSearchManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@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());
}
 
Example #17
Source File: ElasticsearchAdminHandler.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
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;
        }
    }
 
Example #18
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
/**
 * 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;
  }
}
 
Example #19
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@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());

}
 
Example #20
Source File: TransportProxyClientInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #21
Source File: RestGetAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@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));
    getRequest.ignoreErrorsOnGeneratedFields(request.paramAsBoolean("ignore_errors_on_generated_fields", false));

    String sField = request.param("fields");
    if (sField != null) {
        String[] sFields = Strings.splitStringByCommaToArray(sField);
        if (sFields != null) {
            getRequest.fields(sFields);
        }
    }

    getRequest.version(RestActions.parseVersion(request));
    getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType()));

    getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));

    client.get(getRequest, new RestBuilderListener<GetResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            response.toXContent(builder, request);
            builder.endObject();
            if (!response.isExists()) {
                return new BytesRestResponse(NOT_FOUND, builder);
            } else {
                return new BytesRestResponse(OK, builder);
            }
        }
    });
}
 
Example #22
Source File: PercolateRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    startTime = in.readVLong();
    documentType = in.readString();
    routing = in.readOptionalString();
    preference = in.readOptionalString();
    source = in.readBytesReference();
    docSource = in.readBytesReference();
    if (in.readBoolean()) {
        getRequest = new GetRequest(null);
        getRequest.readFrom(in);
    }
    onlyCount = in.readBoolean();
}
 
Example #23
Source File: ElasticsearchExecutorInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private void recordeESattributes(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {

        if (recordESVersion) {
            if (target instanceof ClusterInfoAccessor) {
                //record elasticsearch version and cluster name.
                recorder.recordAttribute(ElasticsearchConstants.ARGS_VERSION_ANNOTATION_KEY, ((ClusterInfoAccessor) target)._$PINPOINT$_getClusterInfo());
            }
        }
        if (recordDsl) {

            if (args[0] instanceof SearchRequest) {
                SearchRequest request = (SearchRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(request.source().toString(), 256));
            } else if (args[0] instanceof GetRequest) {
//                GetRequest request = (GetRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            } else if (args[0] instanceof IndexRequest) {
//                IndexRequest request = (IndexRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            } else if (args[0] instanceof DeleteRequest) {
//                DeleteRequest request = (DeleteRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            } else if (args[0] instanceof UpdateRequest) {
//                UpdateRequest request = (UpdateRequest) args[0];
                recorder.recordAttribute(ElasticsearchConstants.ARGS_DSL_ANNOTATION_KEY, StringUtils.abbreviate(args[0].toString(), 256));
            }
        }


    }
 
Example #24
Source File: ElasticsearchAdminHandlerIT.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Test
public void createNewDatabase() throws InterruptedException, IOException {
    adminHandler.createSchema();

    IndicesClient indices = getEmbeddedClient().indices();

    boolean index = indices.exists(new GetIndexRequest(clientSettings.getIndexId()), RequestOptions.DEFAULT);
    Assertions.assertTrue(index);

    GetResponse resp = getEmbeddedClient()
            .get(new GetRequest(clientSettings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME,
                    MetadataDataMapping.METADATA_ROW_ID), RequestOptions.DEFAULT);

    Assertions.assertEquals(1, resp.getSourceAsMap().get(MetadataDataMapping.METADATA_VERSION_FIELD.getName()));
}
 
Example #25
Source File: ElasticSearchIndexer.java    From james-project with Apache License 2.0 5 votes vote down vote up
public Mono<GetResponse> get(DocumentId id, RoutingKey routingKey) {
    return Mono.fromRunnable(() -> {
            Preconditions.checkNotNull(id);
            Preconditions.checkNotNull(routingKey);
        })
        .then(client.get(new GetRequest(aliasName.getValue())
                .type(NodeMappingFactory.DEFAULT_MAPPING_NAME)
                .id(id.asString())
                .routing(routingKey.asString()),
            RequestOptions.DEFAULT));
}
 
Example #26
Source File: ElasticsearchAdminHandler.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private void addUuidToMetadataIfNeeded(String uuid) throws ElasticsearchException, IOException {
    GetResponse resp = getClient().get(
            new GetRequest(settings.getIndexId()).id(MetadataDataMapping.METADATA_ROW_ID), RequestOptions.DEFAULT);
    // GetResponse resp = getClient().prepareGet(settings.getIndexId(),
    // MetadataDataMapping.METADATA_TYPE_NAME,
    // MetadataDataMapping.METADATA_ROW_ID).get();

    Object retValues = resp.getSourceAsMap().get(MetadataDataMapping.METADATA_UUIDS_FIELD.getName());
    List<String> values;

    if (retValues instanceof String) {
        values = new LinkedList<>();
        values.add((String) retValues);
    } else if (retValues instanceof List<?>) {
        values = (List<String>) retValues;
    } else {
        throw new ConfigurationError("Invalid %s field type %s should have String or java.util.Collection<String>",
                MetadataDataMapping.METADATA_UUIDS_FIELD, retValues.getClass());
    }

    // add new uuid if needed
    if (!values.stream().anyMatch(m -> m.equals(uuid))) {
        Map<String, Object> uuids = new HashMap<>();
        values.add(uuid);
        uuids.put(MetadataDataMapping.METADATA_UUIDS_FIELD.getName(), values);
        uuids.put(MetadataDataMapping.METADATA_UPDATE_TIME_FIELD.getName(),
                Calendar.getInstance(DateTimeZone.UTC.toTimeZone()));
        getClient().update(new UpdateRequest().index(settings.getIndexId()).id("1").doc(uuids),
                RequestOptions.DEFAULT);
        // getClient().prepareUpdate(settings.getIndexId(),
        // MetadataDataMapping.METADATA_TYPE_NAME, "1").setDoc(uuids)
        // .get();
        logger.info("UUID {} is added to the {} type", uuid, MetadataDataMapping.METADATA_TYPE_NAME);
    }
}
 
Example #27
Source File: ElasticsearchLockProviderTest.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@Override
protected void assertUnlocked(String lockName) {
    GetRequest gr = new GetRequest(SCHEDLOCK_DEFAULT_INDEX, SCHEDLOCK_DEFAULT_TYPE, lockName);
    try {
        GetResponse res = highLevelClient.get(gr, RequestOptions.DEFAULT);
        Map<String, Object> m = res.getSourceAsMap();
        assertThat(new Date((Long) m.get(LOCK_UNTIL))).isBeforeOrEqualsTo(now());
        assertThat(new Date((Long) m.get(LOCKED_AT))).isBeforeOrEqualsTo(now());
        assertThat((String) m.get(LOCKED_BY)).isNotBlank();
        assertThat((String) m.get(NAME)).isEqualTo(lockName);
    } catch (IOException e) {
        fail("Call to embedded ES failed.");
    }
}
 
Example #28
Source File: TransportWriterVariant.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Override
public TestClient getTestClient(Config config)
    throws IOException {
  final ElasticsearchTransportClientWriter transportClientWriter = new ElasticsearchTransportClientWriter(config);
  final TransportClient transportClient = transportClientWriter.getTransportClient();
  return new TestClient() {
    @Override
    public GetResponse get(GetRequest getRequest)
        throws IOException {
      try {
        return transportClient.get(getRequest).get();
      } catch (Exception e) {
        throw new IOException(e);
      }
    }

    @Override
    public void recreateIndex(String indexName)
        throws IOException {
      DeleteIndexRequestBuilder dirBuilder = transportClient.admin().indices().prepareDelete(indexName);
      try {
        DeleteIndexResponse diResponse = dirBuilder.execute().actionGet();
      } catch (IndexNotFoundException ie) {
        System.out.println("Index not found... that's ok");
      }

      CreateIndexRequestBuilder cirBuilder = transportClient.admin().indices().prepareCreate(indexName);
      CreateIndexResponse ciResponse = cirBuilder.execute().actionGet();
      Assert.assertTrue(ciResponse.isAcknowledged(), "Create index succeeeded");
    }

    @Override
    public void close()
        throws IOException {
      transportClientWriter.close();
    }
  };
}
 
Example #29
Source File: ElasticsearchIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void getRequestBody() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:index")
                .to("elasticsearch-rest://elasticsearch?operation=Index&indexName=twitter&hostAddresses=" + getElasticsearchHost());

            from("direct:get")
                .to("elasticsearch-rest://elasticsearch?operation=GetById&indexName=twitter&hostAddresses=" + getElasticsearchHost());
        }
    });

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();

        GetRequest request = new GetRequest(PREFIX + "foo");

        IndexRequest idxreq = new IndexRequest(PREFIX + "foo")
                .id(PREFIX + "testId")
                .source(PREFIX + "content", PREFIX + "hello");

        String documentId = template.requestBody("direct:index", idxreq, String.class);

        GetResponse response = template.requestBody("direct:get", request.id(documentId), GetResponse.class);

        Assert.assertNotNull(response);
        Assert.assertEquals(PREFIX + "hello", response.getSourceAsMap().get(PREFIX + "content"));
    } finally {
        camelctx.close();
    }
}
 
Example #30
Source File: ScriptService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void queryScriptIndex(GetIndexedScriptRequest request, final ActionListener<GetResponse> listener) {
    String scriptLang = validateScriptLanguage(request.scriptLang());
    GetRequest getRequest = new GetRequest(request, SCRIPT_INDEX).type(scriptLang).id(request.id())
            .version(request.version()).versionType(request.versionType())
            .preference("_local"); //Set preference for no forking
    client.get(getRequest, listener);
}