Search in sources :

Example 11 with SearchRequest

use of org.apache.metron.indexing.dao.search.SearchRequest in project metron by apache.

the class SearchServiceImplTest method searchShouldProperlySearchWithUserSettingsFacetFields.

@Test
public void searchShouldProperlySearchWithUserSettingsFacetFields() throws Exception {
    AlertsUIUserSettings alertsUIUserSettings = new AlertsUIUserSettings();
    alertsUIUserSettings.setFacetFields(Arrays.asList("source:type", "ip_dst_addr"));
    when(alertsUIService.getAlertsUIUserSettings()).thenReturn(Optional.of(alertsUIUserSettings));
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert"));
    searchRequest.setFacetFields(new ArrayList<>());
    searchService.search(searchRequest);
    SearchRequest expectedSearchRequest = new SearchRequest();
    expectedSearchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert"));
    expectedSearchRequest.setFacetFields(Arrays.asList("source:type", "ip_dst_addr"));
    verify(dao).search(eq(expectedSearchRequest));
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) AlertsUIUserSettings(org.apache.metron.rest.model.AlertsUIUserSettings) Test(org.junit.Test)

Example 12 with SearchRequest

use of org.apache.metron.indexing.dao.search.SearchRequest in project metron by apache.

the class SearchServiceImplTest method searchShouldProperlySearch.

@Test
public void searchShouldProperlySearch() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setIndices(Arrays.asList("bro"));
    searchRequest.setFacetFields(Arrays.asList("ip_src_addr"));
    searchService.search(searchRequest);
    SearchRequest expectedSearchRequest = new SearchRequest();
    expectedSearchRequest.setIndices(Arrays.asList("bro"));
    expectedSearchRequest.setFacetFields(Arrays.asList("ip_src_addr"));
    verify(dao).search(eq(expectedSearchRequest));
    verifyNoMoreInteractions(dao);
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) Test(org.junit.Test)

Example 13 with SearchRequest

use of org.apache.metron.indexing.dao.search.SearchRequest in project metron by apache.

the class SearchServiceImplTest method searchShouldProperlySearchDefaultFacetFields.

@Test
public void searchShouldProperlySearchDefaultFacetFields() throws Exception {
    when(environment.getProperty(SEARCH_FACET_FIELDS_SPRING_PROPERTY, String.class, "")).thenReturn("source:type,ip_src_addr");
    when(alertsUIService.getAlertsUIUserSettings()).thenReturn(Optional.empty());
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert"));
    searchRequest.setFacetFields(new ArrayList<>());
    searchService.search(searchRequest);
    SearchRequest expectedSearchRequest = new SearchRequest();
    expectedSearchRequest.setIndices(Arrays.asList("bro", "snort", "metaalert"));
    expectedSearchRequest.setFacetFields(Arrays.asList("source:type", "ip_src_addr"));
    verify(dao).search(eq(expectedSearchRequest));
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) Test(org.junit.Test)

Example 14 with SearchRequest

use of org.apache.metron.indexing.dao.search.SearchRequest in project metron by apache.

the class ElasticsearchDaoTest method searchShouldSortByGivenFields.

@Test
public void searchShouldSortByGivenFields() throws Exception {
    // setup the column metadata
    Map<String, FieldType> columnMetadata = new HashMap<>();
    columnMetadata.put("sortByStringDesc", FieldType.TEXT);
    columnMetadata.put("sortByIntAsc", FieldType.INTEGER);
    // setup the dao
    setup(RestStatus.OK, 25, columnMetadata);
    // "sort by" fields for the search request
    SortField[] expectedSortFields = { sortBy("sortByStringDesc", SortOrder.DESC), sortBy("sortByIntAsc", SortOrder.ASC), sortBy("sortByUndefinedDesc", SortOrder.DESC) };
    // create a metron search request
    final List<String> indices = Arrays.asList("bro", "snort");
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setSize(2);
    searchRequest.setIndices(indices);
    searchRequest.setFrom(5);
    searchRequest.setSort(Arrays.asList(expectedSortFields));
    searchRequest.setQuery("some query");
    // submit the metron search request
    SearchResponse searchResponse = dao.search(searchRequest);
    assertNotNull(searchResponse);
    // capture the elasticsearch search request that was created
    ArgumentCaptor<org.elasticsearch.action.search.SearchRequest> argument = ArgumentCaptor.forClass(org.elasticsearch.action.search.SearchRequest.class);
    verify(requestSubmitter).submitSearch(argument.capture());
    org.elasticsearch.action.search.SearchRequest request = argument.getValue();
    // transform the request to JSON for validation
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(ElasticsearchUtils.toJSON(request).orElse("???"));
    // validate the sort fields
    JSONArray sortFields = (JSONArray) json.get("sort");
    assertEquals(3, sortFields.size());
    {
        // sort by string descending
        JSONObject aSortField = (JSONObject) sortFields.get(0);
        JSONObject sortBy = (JSONObject) aSortField.get("sortByStringDesc");
        assertEquals("desc", sortBy.get("order"));
        assertEquals("_last", sortBy.get("missing"));
        assertEquals("text", sortBy.get("unmapped_type"));
    }
    {
        // sort by integer ascending
        JSONObject aSortField = (JSONObject) sortFields.get(1);
        JSONObject sortByIntAsc = (JSONObject) aSortField.get("sortByIntAsc");
        assertEquals("asc", sortByIntAsc.get("order"));
        assertEquals("_first", sortByIntAsc.get("missing"));
        assertEquals("integer", sortByIntAsc.get("unmapped_type"));
    }
    {
        // sort by unknown type
        JSONObject aSortField = (JSONObject) sortFields.get(2);
        JSONObject sortByUndefinedDesc = (JSONObject) aSortField.get("sortByUndefinedDesc");
        assertEquals("desc", sortByUndefinedDesc.get("order"));
        assertEquals("_last", sortByUndefinedDesc.get("missing"));
        assertEquals("other", sortByUndefinedDesc.get("unmapped_type"));
    }
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) SortField(org.apache.metron.indexing.dao.search.SortField) FieldType(org.apache.metron.indexing.dao.search.FieldType) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Example 15 with SearchRequest

use of org.apache.metron.indexing.dao.search.SearchRequest in project metron by apache.

the class ElasticsearchDaoTest method searchShouldThrowExceptionWhenMaxResultsAreExceeded.

@Test(expected = InvalidSearchException.class)
public void searchShouldThrowExceptionWhenMaxResultsAreExceeded() throws Exception {
    int maxSearchResults = 20;
    setup(RestStatus.OK, maxSearchResults);
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setSize(maxSearchResults + 1);
    searchRequest.setQuery("");
    dao.search(searchRequest);
// exception expected - size > max
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) Test(org.junit.Test)

Aggregations

SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)29 Test (org.junit.Test)26 SearchResponse (org.apache.metron.indexing.dao.search.SearchResponse)17 SearchResult (org.apache.metron.indexing.dao.search.SearchResult)10 SortField (org.apache.metron.indexing.dao.search.SortField)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 FieldType (org.apache.metron.indexing.dao.search.FieldType)3 GetRequest (org.apache.metron.indexing.dao.search.GetRequest)3 InvalidSearchException (org.apache.metron.indexing.dao.search.InvalidSearchException)3 JSONObject (org.json.simple.JSONObject)3 IOException (java.io.IOException)2 JSONArray (org.json.simple.JSONArray)2 JSONParser (org.json.simple.parser.JSONParser)2 List (java.util.List)1 Optional (java.util.Optional)1 ElasticsearchDao (org.apache.metron.elasticsearch.dao.ElasticsearchDao)1 AccessConfig (org.apache.metron.indexing.dao.AccessConfig)1 IndexDao (org.apache.metron.indexing.dao.IndexDao)1