org.elasticsearch.search.fetch.subphase.FetchSourceContext Java Examples

The following examples show how to use org.elasticsearch.search.fetch.subphase.FetchSourceContext. 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: 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 #2
Source File: ElasticsearchQueryUtilsTest.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Test
public void getProjectionTest()
{
    logger.info("getProjectionTest - enter");

    List<String> expectedProjection = new ArrayList<>();
    mapping.getFields().forEach(field -> expectedProjection.add(field.getName()));

    // Get the actual projection and compare to the expected one.
    FetchSourceContext context = ElasticsearchQueryUtils.getProjection(mapping);
    List<String> actualProjection = ImmutableList.copyOf(context.includes());

    logger.info("Projections - Expected: {}, Actual: {}", expectedProjection, actualProjection);
    assertEquals("Projections do not match", expectedProjection, actualProjection);

    logger.info("getProjectionTest - exit");
}
 
Example #3
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 #4
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 #5
Source File: ElasticsearchQueryUtils.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a projection (using the schema) on which fields should be included in the search index request. For
 * complex type STRUCT, there is no need to include each individual nested field in the projection. Since the
 * schema contains all nested fields in the STRUCT, only the name of the STRUCT field is added to the projection
 * allowing Elasticsearch to return the entire object including all nested fields.
 * @param schema is the schema containing the requested projection.
 * @return a projection wrapped in a FetchSourceContext object.
 */
protected static FetchSourceContext getProjection(Schema schema)
{
    List<String> includedFields = new ArrayList<>();

    for (Field field : schema.getFields()) {
        includedFields.add(field.getName());
    }

    logger.info("Included fields: " + includedFields);

    return new FetchSourceContext(true, Strings.toStringArray(includedFields), Strings.EMPTY_ARRAY);
}
 
Example #6
Source File: RestHandlerUtils.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the request came from Kibana, if so we want to return the UI Metadata from the document.
 * If the request came from the client then we exclude the UI Metadata from the search result.
 *
 * @param request rest request
 * @return instance of {@link org.elasticsearch.search.fetch.subphase.FetchSourceContext}
 */
public static FetchSourceContext getSourceContext(RestRequest request) {
    String userAgent = Strings.coalesceToEmpty(request.header("User-Agent"));
    if (!userAgent.contains(KIBANA_USER_AGENT)) {
        return new FetchSourceContext(true, Strings.EMPTY_ARRAY, UI_METADATA_EXCLUDE);
    } else {
        return null;
    }
}
 
Example #7
Source File: AbstractESTest.java    From elasticsearch-migration with Apache License 2.0 5 votes vote down vote up
@SneakyThrows
protected String getFromIndex(String indexName, String type, String id) {
    final ActionFuture<GetResponse> response = client.get(new GetRequest(indexName, type, id).fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
    client.admin().indices().prepareFlush(indexName).setWaitIfOngoing(true).setForce(true).execute().get();
    final GetResponse getFields = response.get();

    assertThat("Response should be done", response.isDone(), is(true));
    assertThat("Get " + id + " should exist (" + indexName + ", " + type + ")", getFields.isExists(), is(true));
    assertThat("Source field should not be empty", getFields.isSourceEmpty(), is(false));

    final String sourceAsString = getFields.getSourceAsString();

    assertThat("response source should not be null", sourceAsString, notNullValue());
    return sourceAsString;
}
 
Example #8
Source File: ElasticSearchUtil.java    From ranger with Apache License 2.0 5 votes vote down vote up
public MultiGetItemResponse[] fetch(RestHighLevelClient client, String index, SearchHit... hits) throws IOException {
    if(0 == hits.length) {
        return new MultiGetItemResponse[0];
    }
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    for (SearchHit hit : hits) {
        MultiGetRequest.Item item = new MultiGetRequest.Item(index, null, hit.getId());
        item.fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
        multiGetRequest.add(item);
    }
    return client.multiGet(multiGetRequest, RequestOptions.DEFAULT).getResponses();
}
 
Example #9
Source File: ElasticSearchUtils.java    From bdt with Apache License 2.0 5 votes vote down vote up
/**
 * Indexes a document.
 *
 * @param indexName
 * @param id          unique identifier of the document
 * @throws Exception
 */
public boolean existsDocument(String indexName, String id) {
    GetRequest request = new GetRequest(indexName, id);

    request.fetchSourceContext(new FetchSourceContext(false));
    request.storedFields("_none_");

    try {
        return client.exists(request, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new ElasticsearchException("Error indexing document");
    }
}
 
Example #10
Source File: RestHandlerUtilsTests.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public void testGetSourceContext() {
    RestRequest request = new FakeRestRequest();
    FetchSourceContext context = RestHandlerUtils.getSourceContext(request);
    assertArrayEquals(new String[] { "ui_metadata" }, context.excludes());
}
 
Example #11
Source File: RestHandlerUtilsTests.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
public void testGetSourceContextForKibana() {
    FakeRestRequest.Builder builder = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY);
    builder.withHeaders(ImmutableMap.of("User-Agent", ImmutableList.of("Kibana", randomAlphaOfLength(10))));
    FetchSourceContext context = RestHandlerUtils.getSourceContext(builder.build());
    assertNull(context);
}
 
Example #12
Source File: ElasticsearchSpewer.java    From datashare with GNU Affero General Public License v3.0 4 votes vote down vote up
private boolean isDuplicate(String docId) throws IOException {
    GetRequest getRequest = new GetRequest(indexName, esCfg.indexType, docId);
    getRequest.fetchSourceContext(new FetchSourceContext(false));
    getRequest.storedFields("_none_");
    return client.exists(getRequest, RequestOptions.DEFAULT);
}
 
Example #13
Source File: DefaultElasticSearchService.java    From vertx-elasticsearch-service with Apache License 2.0 4 votes vote down vote up
@Override
public void multiGet(final List<MultiGetQueryOptions> multiGetQueryOptions,
                     final MultiGetOptions options,
                     final Handler<AsyncResult<com.hubrick.vertx.elasticsearch.model.MultiGetResponse>> resultHandler) {

    final MultiGetRequestBuilder builder = client.prepareMultiGet();

    if (options != null) {
        if (options.getRefresh() != null) {
            builder.setRefresh(options.getRefresh());
        }
        if (options.getRealtime() != null) {
            builder.setRealtime(options.getRealtime());
        }
        if (options.getPreference() != null) {
            builder.setPreference(options.getPreference());
        }
    }

    for (MultiGetQueryOptions multiGetQueryOptionsItem : multiGetQueryOptions) {
        final MultiGetRequest.Item item = new MultiGetRequest.Item(multiGetQueryOptionsItem.getIndex(), multiGetQueryOptionsItem.getType(), multiGetQueryOptionsItem.getId());
        if (multiGetQueryOptionsItem.getParent() != null) item.parent(multiGetQueryOptionsItem.getParent());
        if (multiGetQueryOptionsItem.getRouting() != null) item.routing(multiGetQueryOptionsItem.getRouting());
        if (multiGetQueryOptionsItem.getStoredFields() != null)
            item.storedFields(multiGetQueryOptionsItem.getStoredFields().toArray(new String[0]));
        if (multiGetQueryOptionsItem.getFetchSource() != null) {
            item.fetchSourceContext(
                    new FetchSourceContext(
                            multiGetQueryOptionsItem.getFetchSource(),
                            multiGetQueryOptionsItem.getFetchSourceIncludes().toArray(new String[0]),
                            multiGetQueryOptionsItem.getFetchSourceExcludes().toArray(new String[0])
                    )
            );
        }

        builder.add(item);
    }

    builder.execute(new ActionListener<MultiGetResponse>() {
        @Override
        public void onResponse(final MultiGetResponse multiGetResponse) {
            resultHandler.handle(Future.succeededFuture(mapToMultiGetResponse(multiGetResponse)));
        }

        @Override
        public void onFailure(final Exception e) {
            handleFailure(resultHandler, e);
        }
    });
}