Search in sources :

Example 6 with FieldType

use of org.apache.metron.indexing.dao.search.FieldType 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 7 with FieldType

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

the class ElasticsearchMetaAlertDaoTest method testInvalidInit.

@Test(expected = IllegalArgumentException.class)
public void testInvalidInit() {
    IndexDao dao = new IndexDao() {

        @Override
        public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException {
            return null;
        }

        @Override
        public GroupResponse group(GroupRequest groupRequest) throws InvalidSearchException {
            return null;
        }

        @Override
        public void init(AccessConfig config) {
        }

        @Override
        public Document getLatest(String guid, String sensorType) throws IOException {
            return null;
        }

        @Override
        public Iterable<Document> getAllLatest(List<GetRequest> getRequests) throws IOException {
            return null;
        }

        @Override
        public void update(Document update, Optional<String> index) throws IOException {
        }

        @Override
        public void batchUpdate(Map<Document, Optional<String>> updates) throws IOException {
        }

        @Override
        public Map<String, FieldType> getColumnMetadata(List<String> indices) throws IOException {
            return null;
        }
    };
    ElasticsearchMetaAlertDao metaAlertDao = new ElasticsearchMetaAlertDao();
    metaAlertDao.init(dao);
}
Also used : SearchRequest(org.apache.metron.indexing.dao.search.SearchRequest) Optional(java.util.Optional) GroupRequest(org.apache.metron.indexing.dao.search.GroupRequest) ArrayList(java.util.ArrayList) List(java.util.List) AccessConfig(org.apache.metron.indexing.dao.AccessConfig) Document(org.apache.metron.indexing.dao.update.Document) HashMap(java.util.HashMap) Map(java.util.Map) IndexDao(org.apache.metron.indexing.dao.IndexDao) FieldType(org.apache.metron.indexing.dao.search.FieldType) Test(org.junit.Test)

Aggregations

FieldType (org.apache.metron.indexing.dao.search.FieldType)7 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 InvalidSearchException (org.apache.metron.indexing.dao.search.InvalidSearchException)3 SearchRequest (org.apache.metron.indexing.dao.search.SearchRequest)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 SearchResponse (org.apache.metron.indexing.dao.search.SearchResponse)2 SortField (org.apache.metron.indexing.dao.search.SortField)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 AccessConfig (org.apache.metron.indexing.dao.AccessConfig)1 IndexDao (org.apache.metron.indexing.dao.IndexDao)1 GroupRequest (org.apache.metron.indexing.dao.search.GroupRequest)1 GroupResponse (org.apache.metron.indexing.dao.search.GroupResponse)1 SearchResult (org.apache.metron.indexing.dao.search.SearchResult)1 Document (org.apache.metron.indexing.dao.update.Document)1