Search in sources :

Example 1 with Search

use of io.searchbox.core.Search in project opennms by OpenNMS.

the class AlarmEventToIndexTest method jestClientAlarmToESTest.

/**
 * simple test to create an alarm change event which will create a new alarm in the alarm index
 * and create an alarm change event in the alarm change index
 */
@Test
public void jestClientAlarmToESTest() {
    LOG.debug("***************** start of test jestClientAlarmToESTest");
    EventToIndex eventToIndex = new EventToIndex();
    JestClient jestClient = null;
    try {
        // Get Jest client
        String esusername = "";
        String espassword = "";
        String elasticsearchUrl = "http://localhost:9200";
        RestClientFactory restClientFactory = new RestClientFactory(elasticsearchUrl, esusername, espassword);
        IndexNameFunction indexNameFunction = new IndexNameFunction("yyyy.MM");
        NodeCache nodeCache = new MockNodeCache();
        eventToIndex.setRestClientFactory(restClientFactory);
        eventToIndex.setNodeCache(nodeCache);
        eventToIndex.setIndexNameFunction(indexNameFunction);
        eventToIndex.setLogEventDescription(true);
        eventToIndex.setArchiveRawEvents(true);
        eventToIndex.setArchiveAlarms(true);
        eventToIndex.setArchiveAlarmChangeEvents(true);
        eventToIndex.setArchiveOldAlarmValues(true);
        eventToIndex.setArchiveNewAlarmValues(true);
        // create an alarm change event
        EventBuilder eb = new EventBuilder(ALARM_ACKNOWLEDGED_EVENT, EVENT_SOURCE_NAME);
        // copy in all values as json in params
        eb.addParam("oldalarmvalues", TEST_ALARM_JSON_1);
        eb.addParam("newalarmvalues", TEST_ALARM_JSON_1);
        Event event = eb.getEvent();
        event.setDbid(100);
        event.setNodeid((long) 34);
        // forward event to Elasticsearch
        eventToIndex.forwardEvents(Collections.singletonList(event));
        // waiting INDEX_WAIT_SECONDS seconds for index
        try {
            TimeUnit.SECONDS.sleep(INDEX_WAIT_SECONDS);
        } catch (InterruptedException e) {
        }
        // send query to check that alarm has been created
        jestClient = restClientFactory.getJestClient();
        // search for resulting alarm
        String query = "{\n" + "\n       \"query\": {" + "\n         \"match\": {" + "\n         \"alarmid\": \"807\"" + "\n          }" + "\n        }" + "\n     }";
        LOG.debug("alarm check search query: " + query);
        Search search = new Search.Builder(query).addIndex("opennms-*").build();
        SearchResult sresult = jestClient.execute(search);
        LOG.debug("received search sresult: " + sresult.getJsonString() + "\n   response code:" + sresult.getResponseCode() + "\n   error message: " + sresult.getErrorMessage());
        assertEquals(200, sresult.getResponseCode());
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(sresult.getJsonString());
        JSONObject resultValues = (JSONObject) obj;
        JSONObject hits = (JSONObject) resultValues.get("hits");
        LOG.debug("search result hits:total=" + hits.get("total"));
        assertEquals(Long.valueOf(1), hits.get("total"));
        // waiting INDEX_WAIT_SECONDS seconds for index
        try {
            TimeUnit.SECONDS.sleep(INDEX_WAIT_SECONDS);
        } catch (InterruptedException e) {
        }
        // search for resulting alarm change event
        String eventquery = "{\n" + "\n       \"query\": {" + "\n         \"match\": {" + "\n         \"id\": \"100\"" + "\n          }" + "\n        }" + "\n     }";
        LOG.debug("event check search query: " + eventquery);
        Search eventsearch = new Search.Builder(eventquery).addIndex("opennms-*").build();
        SearchResult eventsresult = jestClient.execute(eventsearch);
        LOG.debug("received search eventsresult: " + eventsresult.getJsonString() + "\n   response code:" + eventsresult.getResponseCode() + "\n   error message: " + eventsresult.getErrorMessage());
        assertEquals(200, eventsresult.getResponseCode());
        Object obj2 = parser.parse(eventsresult.getJsonString());
        JSONObject eventsresultValues = (JSONObject) obj2;
        JSONObject eventhits = (JSONObject) eventsresultValues.get("hits");
        LOG.debug("search result event hits:total=" + eventhits.get("total"));
        assertEquals(Long.valueOf(1), eventhits.get("total"));
        JSONArray eventhitsvalues = (JSONArray) eventhits.get("hits");
        LOG.debug("   eventhitsvalues: " + eventhitsvalues.toJSONString());
        JSONObject hitObj = (JSONObject) eventhitsvalues.get(0);
        LOG.debug("   hitObj: " + hitObj.toJSONString());
        String typeStr = hitObj.get("_type").toString();
        LOG.debug("search result index type=" + typeStr);
        assertEquals(EVENT_INDEX_TYPE, typeStr);
        JSONObject sourceObj = (JSONObject) hitObj.get("_source");
        LOG.debug("   sourceObj: " + sourceObj.toJSONString());
        String eventUeiStr = sourceObj.get("eventuei").toString();
        LOG.debug("search result event eventueistr=" + eventUeiStr);
        assertEquals(ALARM_ACKNOWLEDGED_EVENT, eventUeiStr);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        // shutdown client
        if (jestClient != null)
            jestClient.shutdownClient();
        if (eventToIndex != null)
            eventToIndex.close();
    }
    LOG.debug("***************** end of test jestClientAlarmToESTest");
}
Also used : IndexNameFunction(org.opennms.plugins.elasticsearch.rest.IndexNameFunction) NodeCache(org.opennms.plugins.elasticsearch.rest.NodeCache) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONArray(org.json.simple.JSONArray) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) JSONObject(org.json.simple.JSONObject) RestClientFactory(org.opennms.plugins.elasticsearch.rest.RestClientFactory) Search(io.searchbox.core.Search) Event(org.opennms.netmgt.xml.event.Event) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) EventToIndex(org.opennms.plugins.elasticsearch.rest.EventToIndex) Test(org.junit.Test)

Example 2 with Search

use of io.searchbox.core.Search in project opennms by OpenNMS.

the class AlarmElasticsearch5IT method getNumberOfAlarmsInEsWithUei.

private static int getNumberOfAlarmsInEsWithUei(InetSocketAddress esHttpAddr, String uei) throws IOException {
    JestClient client = null;
    try {
        JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig.Builder(String.format("http://%s:%d", esHttpAddr.getHostString(), esHttpAddr.getPort())).multiThreaded(true).build());
        client = factory.getObject();
        SearchResult response = client.execute(new Search.Builder(new SearchSourceBuilder().query(QueryBuilders.matchQuery("eventuei", EventConstants.IMPORT_FAILED_UEI)).toString()).addIndex("opennms-alarms*").build());
        LOG.debug("SEARCH RESPONSE: {}", response.toString());
        return response.getTotal();
    } finally {
        if (client != null) {
            client.shutdownClient();
        }
    }
}
Also used : HttpClientConfig(io.searchbox.client.config.HttpClientConfig) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) JestClientFactory(io.searchbox.client.JestClientFactory) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 3 with Search

use of io.searchbox.core.Search in project opennms by OpenNMS.

the class AbstractSyslogTestCase method pollForElasticsearchEventsUsingJest.

protected static void pollForElasticsearchEventsUsingJest(Supplier<InetSocketAddress> esTransportAddr, int numMessages) {
    with().pollInterval(15, SECONDS).await().atMost(5, MINUTES).until(() -> {
        JestClient client = null;
        try {
            JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig.Builder(String.format("http://%s:%d", esTransportAddr.get().getHostString(), esTransportAddr.get().getPort())).multiThreaded(true).build());
            client = factory.getObject();
            SearchResult response = client.execute(new Search.Builder(new SearchSourceBuilder().query(QueryBuilders.matchQuery("eventuei", "uei.opennms.org/vendor/cisco/syslog/SEC-6-IPACCESSLOGP/aclDeniedIPTraffic")).toString()).addIndex("opennms*").build());
            LOG.debug("SEARCH RESPONSE: {}", response.toString());
            // Sometimes, the first warm-up message is successful so treat both message counts as valid
            assertTrue("ES search hits was not equal to " + numMessages + ": " + response.getTotal(), (numMessages == response.getTotal()));
        // assertEquals("Event UEI did not match", "uei.opennms.org/vendor/cisco/syslog/SEC-6-IPACCESSLOGP/aclDeniedIPTraffic", response.getHits().getAt(0).getSource().get("eventuei"));
        // assertEquals("Event IP address did not match", "4.2.2.2", response.getHits().getAt(0).getSource().get("ipaddr"));
        } catch (Throwable e) {
            LOG.warn(e.getMessage(), e);
            return false;
        } finally {
            if (client != null) {
                client.shutdownClient();
            }
        }
        return true;
    });
}
Also used : HttpClientConfig(io.searchbox.client.config.HttpClientConfig) Search(io.searchbox.core.Search) SearchResult(io.searchbox.core.SearchResult) JestClient(io.searchbox.client.JestClient) JestClientFactory(io.searchbox.client.JestClientFactory) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 4 with Search

use of io.searchbox.core.Search in project xwiki-platform by xwiki.

the class DefaultDataManager method searchInstalls.

@Override
public JsonObject searchInstalls(String indexType, String fullQuery, Map<String, Object> parameters) throws Exception {
    Search.Builder searchBuilder = new Search.Builder(fullQuery).addIndex(JestClientManager.INDEX).addType(indexType);
    // Add parameters and handle specifically the Search Type.
    if (parameters.containsKey(Parameters.SEARCH_TYPE)) {
        SearchType searchType = this.converterManager.convert(SearchType.class, parameters.get(Parameters.SEARCH_TYPE));
        searchBuilder.setSearchType(searchType);
    }
    for (Map.Entry<String, Object> parameterEntry : parameters.entrySet()) {
        if (!parameterEntry.getKey().equals(Parameters.SEARCH_TYPE)) {
            searchBuilder.setParameter(parameterEntry.getKey(), parameterEntry.getValue());
        }
    }
    return executeActionQuery(searchBuilder.build(), fullQuery).getJsonObject();
}
Also used : Search(io.searchbox.core.Search) JsonObject(com.google.gson.JsonObject) SearchType(io.searchbox.params.SearchType) Map(java.util.Map)

Example 5 with Search

use of io.searchbox.core.Search in project xwiki-platform by xwiki.

the class DatePingDataProvider method provideData.

@Override
public Map<String, Object> provideData() {
    Map<String, Object> jsonMap = new HashMap<>();
    try {
        String instanceId = this.instanceIdManager.getInstanceId().toString();
        Search search = new Search.Builder(constructSearchJSON(instanceId)).addIndex(JestClientManager.INDEX).addType(JestClientManager.TYPE).setSearchType(SearchType.COUNT).build();
        JestResult result = this.jestClientManager.getClient().execute(search);
        if (!result.isSucceeded()) {
            this.logger.warn(ERROR_MESSAGE, result.getErrorMessage());
            return jsonMap;
        }
        @SuppressWarnings("unchecked") Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations");
        // Get the current server time and the first timestamp of the ping for this instance id and compute the
        // since days from them.
        @SuppressWarnings("unchecked") Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME);
        Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE);
        @SuppressWarnings("unchecked") Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE);
        Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE);
        if (serverTimeObject != null && firstPingDateObject != null) {
            long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D);
            jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays);
            long firstPingDate = Math.round((double) firstPingDateObject);
            jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate);
        } else {
            // This means it's the first ping and thus there was no previous _timestamp. Thus we set the since Days
            // to 0.
            jsonMap.put(PROPERTY_SINCE_DAYS, 0);
        }
    } catch (Exception e) {
        // If this fails we just don't send this information but we still send the other piece of information.
        // However we log a warning since it's a problem that needs to be seen and looked at.
        this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e));
    }
    return jsonMap;
}
Also used : HashMap(java.util.HashMap) Search(io.searchbox.core.Search) JSONObject(net.sf.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) JestResult(io.searchbox.client.JestResult)

Aggregations

Search (io.searchbox.core.Search)45 SearchResult (io.searchbox.core.SearchResult)24 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)23 Test (org.junit.Test)15 Hit (io.searchbox.core.SearchResult.Hit)12 JestClient (io.searchbox.client.JestClient)11 SearchSourceBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder)10 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)9 JsonObject (com.google.gson.JsonObject)8 Map (java.util.Map)8 IOException (java.io.IOException)7 List (java.util.List)7 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 IndexMapping (org.graylog2.indexer.IndexMapping)6 Optional (java.util.Optional)5 BoolQueryBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder)5 QueryBuilder (org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder)5 QueryBuilders (org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilders)5