Java源码示例:org.elasticsearch.search.fetch.subphase.FetchSourceContext
示例1
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 {
}
}
示例2
@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");
}
示例3
@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;
}
示例4
@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;
}
示例5
/**
* 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);
}
示例6
/**
* 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;
}
}
示例7
@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;
}
示例8
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();
}
示例9
/**
* 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");
}
}
示例10
public void testGetSourceContext() {
RestRequest request = new FakeRestRequest();
FetchSourceContext context = RestHandlerUtils.getSourceContext(request);
assertArrayEquals(new String[] { "ui_metadata" }, context.excludes());
}
示例11
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);
}
示例12
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);
}
示例13
@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);
}
});
}