Search in sources :

Example 1 with ElasticsearchClient

use of org.apache.metron.elasticsearch.client.ElasticsearchClient in project metron by apache.

the class ElasticsearchRequestSubmitterTest method setup.

public ElasticsearchRequestSubmitter setup(SearchResponse response) throws IOException {
    // mocks
    RestHighLevelClient highLevelClient = mock(RestHighLevelClient.class);
    ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevelClient);
    // the client should return the given search response
    when(highLevelClient.search(any())).thenReturn(response);
    return new ElasticsearchRequestSubmitter(client);
}
Also used : RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient)

Example 2 with ElasticsearchClient

use of org.apache.metron.elasticsearch.client.ElasticsearchClient in project metron by apache.

the class ElasticsearchUpdateDaoTest method setup.

@BeforeEach
public void setup() {
    accessConfig = new AccessConfig();
    retrieveLatestDao = mock(ElasticsearchRetrieveLatestDao.class);
    RestHighLevelClient highLevel = mock(RestHighLevelClient.class);
    ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevel);
    updateDao = new ElasticsearchUpdateDao(client, accessConfig, retrieveLatestDao);
}
Also used : RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ElasticsearchClient

use of org.apache.metron.elasticsearch.client.ElasticsearchClient in project metron by apache.

the class ElasticsearchUpdateIntegrationTest method installIndexTemplate.

/**
 * Install the index template to ensure that "guid" is of type "keyword". The
 * {@link org.apache.metron.elasticsearch.dao.ElasticsearchRetrieveLatestDao} cannot find
 * documents if the type is not "keyword".
 *
 * See https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-term-query.html
 */
private static void installIndexTemplate() throws IOException {
    HttpEntity broEntity = new NStringEntity(indexTemplate, ContentType.APPLICATION_JSON);
    ElasticsearchClient client = ElasticsearchClientFactory.create(globalConfig);
    Response response = client.getLowLevelClient().performRequest("PUT", "/_template/test_template", Collections.emptyMap(), broEntity);
    assertThat(response.getStatusLine().getStatusCode(), CoreMatchers.equalTo(200));
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient)

Example 4 with ElasticsearchClient

use of org.apache.metron.elasticsearch.client.ElasticsearchClient in project metron by apache.

the class ElasticsearchDaoTest method setup.

private void setup(RestStatus status, int maxSearchResults, Map<String, FieldType> metadata) throws Exception {
    // setup the mock search hits
    SearchHit hit1 = mock(SearchHit.class);
    when(hit1.getId()).thenReturn("id1");
    when(hit1.getSource()).thenReturn(new HashMap<String, Object>() {

        {
            put("field", "value1");
        }
    });
    when(hit1.getScore()).thenReturn(0.1f);
    SearchHit hit2 = mock(SearchHit.class);
    when(hit2.getId()).thenReturn("id2");
    when(hit2.getSource()).thenReturn(new HashMap<String, Object>() {

        {
            put("field", "value2");
        }
    });
    when(hit2.getScore()).thenReturn(0.2f);
    // search hits
    SearchHit[] hits = { hit1, hit2 };
    SearchHits searchHits = mock(SearchHits.class);
    when(searchHits.getHits()).thenReturn(hits);
    when(searchHits.getTotalHits()).thenReturn(Integer.toUnsignedLong(hits.length));
    // search response which returns the search hits
    org.elasticsearch.action.search.SearchResponse response = mock(org.elasticsearch.action.search.SearchResponse.class);
    when(response.status()).thenReturn(status);
    when(response.getHits()).thenReturn(searchHits);
    // provides column metadata
    ElasticsearchColumnMetadataDao columnMetadataDao = mock(ElasticsearchColumnMetadataDao.class);
    when(columnMetadataDao.getColumnMetadata(any())).thenReturn(metadata);
    // returns the search response
    requestSubmitter = mock(ElasticsearchRequestSubmitter.class);
    when(requestSubmitter.submitSearch(any())).thenReturn(response);
    RestHighLevelClient highLevel = mock(RestHighLevelClient.class);
    ElasticsearchClient client = new ElasticsearchClient(mock(RestClient.class), highLevel);
    // provides configuration
    AccessConfig config = mock(AccessConfig.class);
    when(config.getMaxSearchResults()).thenReturn(maxSearchResults);
    ElasticsearchSearchDao elasticsearchSearchDao = new ElasticsearchSearchDao(client, config, columnMetadataDao, requestSubmitter);
    ElasticsearchRetrieveLatestDao elasticsearchRetrieveLatestDao = new ElasticsearchRetrieveLatestDao(client);
    ElasticsearchUpdateDao elasticsearchUpdateDao = new ElasticsearchUpdateDao(client, config, elasticsearchRetrieveLatestDao);
    dao = new ElasticsearchDao(client, config, elasticsearchSearchDao, elasticsearchUpdateDao, elasticsearchRetrieveLatestDao, columnMetadataDao, requestSubmitter);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) RestClient(org.elasticsearch.client.RestClient) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) org.apache.metron.indexing.dao.search(org.apache.metron.indexing.dao.search) JSONObject(org.json.simple.JSONObject) SearchHits(org.elasticsearch.search.SearchHits) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient)

Example 5 with ElasticsearchClient

use of org.apache.metron.elasticsearch.client.ElasticsearchClient in project metron by apache.

the class ElasticsearchSearchIntegrationTest method setup.

@BeforeAll
public static void setup() throws Exception {
    globalConfig = new HashMap<String, Object>() {

        {
            put("es.clustername", "metron");
            put("es.port", "9200");
            put("es.ip", "localhost");
            put("es.date.format", dateFormat);
        }
    };
    accessConfig = new AccessConfig();
    accessConfig.setMaxSearchResults(100);
    accessConfig.setMaxSearchGroups(100);
    accessConfig.setGlobalConfigSupplier(() -> globalConfig);
    indexComponent = startIndex();
    ElasticsearchClient esClient = ElasticsearchClientFactory.create(globalConfig);
    lowLevelClient = esClient.getLowLevelClient();
    highLevelClient = esClient.getHighLevelClient();
    dao = new ElasticsearchDao();
    dao.init(accessConfig);
    // The data is all static for searches, so we can set it up beforehand, and it's faster
    loadTestData();
}
Also used : ElasticsearchDao(org.apache.metron.elasticsearch.dao.ElasticsearchDao) JSONObject(org.json.simple.JSONObject) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) ElasticsearchClient(org.apache.metron.elasticsearch.client.ElasticsearchClient) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

ElasticsearchClient (org.apache.metron.elasticsearch.client.ElasticsearchClient)5 AccessConfig (org.apache.metron.indexing.dao.AccessConfig)3 RestClient (org.elasticsearch.client.RestClient)3 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)3 JSONObject (org.json.simple.JSONObject)2 HttpEntity (org.apache.http.HttpEntity)1 NStringEntity (org.apache.http.nio.entity.NStringEntity)1 ElasticsearchDao (org.apache.metron.elasticsearch.dao.ElasticsearchDao)1 org.apache.metron.indexing.dao.search (org.apache.metron.indexing.dao.search)1 Response (org.elasticsearch.client.Response)1 SearchHit (org.elasticsearch.search.SearchHit)1 SearchHits (org.elasticsearch.search.SearchHits)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1