Search in sources :

Example 1 with SearchResponse

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);
    }
}
Also used : HashMap(java.util.HashMap) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) SearchResponse(org.apache.nifi.elasticsearch.SearchResponse) Test(org.junit.Test)

Example 2 with SearchResponse

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();
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) SearchResponse(org.apache.nifi.elasticsearch.SearchResponse)

Example 3 with SearchResponse

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;
}
Also used : List(java.util.List) IOException(java.io.IOException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SearchResponse(org.apache.nifi.elasticsearch.SearchResponse)

Aggregations

SearchResponse (org.apache.nifi.elasticsearch.SearchResponse)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 Test (org.junit.Test)1