use of org.apache.nifi.elasticsearch.SearchResponse in project nifi by apache.
the class ElasticSearch5ClientService_IT method testBasicSearch.
@Test
public void testBasicSearch() throws Exception {
String query = "{\n" + "\t\"size\": 10,\n" + "\t\"query\": {\n" + "\t\t\"match_all\": {}\n" + "\t},\n" + "\t\"aggs\": {\n" + "\t\t\"term_counts\": {\n" + "\t\t\t\"terms\": {\n" + "\t\t\t\t\"field\": \"msg\",\n" + "\t\t\t\t\"size\": 5\n" + "\t\t\t}\n" + "\t\t}\n" + "\t}\n" + "}";
SearchResponse response = service.search(query, "messages", "message");
Assert.assertNotNull("Response was null", response);
Assert.assertEquals("Wrong count", response.getNumberOfHits(), 15);
Assert.assertFalse("Timed out", response.isTimedOut());
Assert.assertNotNull("Hits was null", response.getHits());
Assert.assertEquals("Wrong number of hits", 10, response.getHits().size());
Assert.assertNotNull("Aggregations are missing", response.getAggregations());
Assert.assertEquals("Aggregation count is wrong", 1, response.getAggregations().size());
Map<String, Object> termCounts = (Map<String, Object>) response.getAggregations().get("term_counts");
Assert.assertNotNull("Term counts was missing", termCounts);
List<Map<String, Object>> buckets = (List<Map<String, Object>>) termCounts.get("buckets");
Assert.assertNotNull("Buckets branch was empty", buckets);
Map<String, Integer> expected = new HashMap<>();
expected.put("one", 1);
expected.put("two", 2);
expected.put("three", 3);
expected.put("four", 4);
expected.put("five", 5);
for (Map<String, Object> aggRes : buckets) {
String key = (String) aggRes.get("key");
Integer docCount = (Integer) aggRes.get("doc_count");
Assert.assertEquals(String.format("%s did not match", key), expected.get(key), docCount);
}
}
use of org.apache.nifi.elasticsearch.SearchResponse in project nifi by apache.
the class JsonQueryElasticsearch method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
FlowFile input = null;
if (context.hasIncomingConnection()) {
input = session.get();
if (input == null && context.hasNonLoopConnection()) {
return;
}
}
try {
final String query = getQuery(input, context, session);
final String index = context.getProperty(INDEX).evaluateAttributeExpressions(input).getValue();
final String type = context.getProperty(TYPE).evaluateAttributeExpressions(input).getValue();
final String queryAttr = context.getProperty(QUERY_ATTRIBUTE).isSet() ? context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(input).getValue() : null;
SearchResponse response = clientService.search(query, index, type);
Map<String, String> attributes = new HashMap<>();
attributes.put(CoreAttributes.MIME_TYPE.key(), "application/json");
if (!StringUtils.isBlank(queryAttr)) {
attributes.put(queryAttr, query);
}
List<FlowFile> hitsFlowFiles = handleHits(response.getHits(), context, session, input, attributes);
List<FlowFile> aggsFlowFiles = handleAggregations(response.getAggregations(), context, session, input, attributes);
final String transitUri = clientService.getTransitUrl(index, type);
if (hitsFlowFiles.size() > 0) {
session.transfer(hitsFlowFiles, REL_HITS);
for (FlowFile ff : hitsFlowFiles) {
session.getProvenanceReporter().send(ff, transitUri);
}
}
if (aggsFlowFiles.size() > 0) {
session.transfer(aggsFlowFiles, REL_AGGREGATIONS);
for (FlowFile ff : aggsFlowFiles) {
session.getProvenanceReporter().send(ff, transitUri);
}
}
if (input != null) {
session.transfer(input, REL_ORIGINAL);
}
} catch (Exception ex) {
getLogger().error("Error processing flowfile.", ex);
if (input != null) {
session.transfer(input, REL_FAILURE);
}
context.yield();
}
}
use of org.apache.nifi.elasticsearch.SearchResponse in project nifi by apache.
the class TestElasticSearchClientService method search.
@Override
public SearchResponse search(String query, String index, String type) throws IOException {
if (throwErrorInSearch) {
throw new IOException();
}
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> hits = (List<Map<String, Object>>) mapper.readValue(HITS_RESULT, List.class);
Map<String, Object> aggs = returnAggs ? (Map<String, Object>) mapper.readValue(AGGS_RESULT, Map.class) : null;
SearchResponse response = new SearchResponse(hits, aggs, 15, 5, false);
return response;
}
Aggregations