use of org.elasticsearch.action.search.SearchRequest in project jnosql-diana-driver by eclipse.
the class EntityConverter method executeStatement.
private static void executeStatement(DocumentQuery query, RestHighLevelClient client, String index, QueryConverterResult select, List<DocumentEntity> entities) throws IOException {
SearchRequest searchRequest = new SearchRequest(index);
searchRequest.types(query.getDocumentCollection());
if (select.hasQuery()) {
setQueryBuilder(query, select, searchRequest);
}
SearchResponse response = client.search(searchRequest);
Stream.of(response.getHits()).flatMap(h -> Stream.of(h.getHits())).map(ElasticsearchEntry::of).filter(ElasticsearchEntry::isNotEmpty).map(ElasticsearchEntry::toEntity).forEach(entities::add);
}
use of org.elasticsearch.action.search.SearchRequest in project main by JohnPeng739.
the class ElasticAccessorRest method search.
private <T extends Base> SearchResponse search(List<GeneralAccessor.ConditionTuple> tuples, Class<T> clazz, Pagination pagination) throws UserInterfaceDalErrorException {
SearchSourceBuilder builder = new SearchSourceBuilder();
if (tuples == null || tuples.isEmpty()) {
builder.query(QueryBuilders.matchAllQuery());
} else {
BoolQueryBuilder query = QueryBuilders.boolQuery();
tuples.forEach(tuple -> query.must(QueryBuilders.termQuery(tuple.field, tuple.value)));
builder.query(query);
}
if (pagination != null) {
builder.from((pagination.getPage() - 1) * pagination.getSize());
builder.size(pagination.getSize());
}
SearchRequest request = new SearchRequest(index);
request.types(clazz.getName());
request.source(builder);
try {
SearchResponse response = client.search(request);
return response;
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error("Search fail from elastic.", ex);
}
throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
}
}
use of org.elasticsearch.action.search.SearchRequest in project apm-agent-java by elastic.
the class ElasticsearchRestClientInstrumentationIT method testDocumentScenario.
@Test
public void testDocumentScenario() throws IOException, ExecutionException, InterruptedException {
// Index a document
IndexResponse ir = doIndex(new IndexRequest(INDEX, DOC_TYPE, DOC_ID).source(jsonBuilder().startObject().field(FOO, BAR).endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
assertThat(ir.status().getStatus()).isEqualTo(201);
List<Span> spans = reporter.getSpans();
assertThat(spans).hasSize(1);
validateSpanContent(spans.get(0), String.format("Elasticsearch: PUT /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 201, "PUT");
// Search the index
reporter.reset();
SearchRequest searchRequest = new SearchRequest(INDEX);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery(FOO, BAR));
sourceBuilder.from(0);
sourceBuilder.size(5);
searchRequest.source(sourceBuilder);
SearchResponse sr = doSearch(searchRequest);
assertThat(sr.getHits().totalHits).isEqualTo(1L);
assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAR);
spans = reporter.getSpans();
assertThat(spans).hasSize(1);
Span searchSpan = spans.get(0);
validateSpanContent(searchSpan, String.format("Elasticsearch: GET /%s/_search", INDEX), 200, "GET");
validateDbContextContent(searchSpan, "{\"from\":0,\"size\":5,\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");
// Now update and re-search
reporter.reset();
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put(FOO, BAZ);
UpdateResponse ur = doUpdate(new UpdateRequest(INDEX, DOC_TYPE, DOC_ID).doc(jsonMap).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
assertThat(ur.status().getStatus()).isEqualTo(200);
sr = doSearch(new SearchRequest(INDEX));
assertThat(sr.getHits().getAt(0).getSourceAsMap().get(FOO)).isEqualTo(BAZ);
spans = reporter.getSpans();
assertThat(spans).hasSize(2);
boolean updateSpanFound = false;
for (Span span : spans) {
if (span.getNameAsString().contains("_update")) {
updateSpanFound = true;
break;
}
}
assertThat(updateSpanFound).isTrue();
// Finally - delete the document
reporter.reset();
DeleteResponse dr = doDelete(new DeleteRequest(INDEX, DOC_TYPE, DOC_ID));
validateSpanContent(spans.get(0), String.format("Elasticsearch: DELETE /%s/%s/%s", INDEX, DOC_TYPE, DOC_ID), 200, "DELETE");
}
use of org.elasticsearch.action.search.SearchRequest in project apm-agent-java by elastic.
the class AbstractEs6_4ClientInstrumentationTest method testMultiSearchRequest_validateSpanContentAndDbContext.
@Test
public void testMultiSearchRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
createDocument();
reporter.reset();
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
SearchRequest firstSearchRequest = new SearchRequest(INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery(FOO, BAR));
firstSearchRequest.source(searchSourceBuilder);
multiSearchRequest.add(firstSearchRequest);
MultiSearchResponse response = doMultiSearch(multiSearchRequest);
List<Span> spans = reporter.getSpans();
assertThat(spans).hasSize(1);
Span span = spans.get(0);
validateSpanContent(span, "Elasticsearch: POST /_msearch", 200, "POST");
verifyMultiSearchSpanContent(span);
deleteDocument();
}
use of org.elasticsearch.action.search.SearchRequest in project apm-agent-java by elastic.
the class AbstractEs6_4ClientInstrumentationTest method testRollupSearch_validateSpanContentAndDbContext.
@Test
public void testRollupSearch_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
createDocument();
reporter.reset();
SearchRequest rollupSearchRequest = new SearchRequest(INDEX);
SearchSourceBuilder rollupSearchBuilder = new SearchSourceBuilder();
rollupSearchBuilder.query(QueryBuilders.termQuery(FOO, BAR));
rollupSearchBuilder.from(0);
rollupSearchBuilder.size(5);
rollupSearchRequest.source(rollupSearchBuilder);
SearchResponse response = doRollupSearch(rollupSearchRequest);
verifyTotalHits(response.getHits());
List<Span> spans = reporter.getSpans();
assertThat(spans).hasSize(1);
Span span = spans.get(0);
validateSpanContent(span, String.format("Elasticsearch: POST /%s/_rollup_search", INDEX), 200, "POST");
validateDbContextContent(span, "{\"from\":0,\"size\":5,\"query\":{\"term\":{\"foo\":{\"value\":\"bar\",\"boost\":1.0}}}}");
deleteDocument();
}
Aggregations