ElasticsearchDao implements IndexDao { @Override public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException { return this.searchDao.search(searchRequest); } ElasticsearchDao(ElasticsearchClient client,
      AccessConfig config,
      ElasticsearchSearchDao searchDao,
      ElasticsearchUpdateDao updateDao,
      ElasticsearchRetrieveLatestDao retrieveLatestDao,
      ElasticsearchColumnMetadataDao columnMetadataDao,
      ElasticsearchRequestSubmitter requestSubmitter
  );  ElasticsearchDao(); AccessConfig getAccessConfig(); void setAccessConfig(AccessConfig accessConfig); @Override synchronized void init(AccessConfig config); @Override SearchResponse search(SearchRequest searchRequest); @Override GroupResponse group(GroupRequest groupRequest); @Override Document getLatest(final String guid, final String sensorType); @Override Iterable<Document> getAllLatest(
      final List<GetRequest> getRequests); @Override Document update(Document update, Optional<String> index); @Override Map<Document, Optional<String>> batchUpdate(Map<Document, Optional<String>> updates); @Override Document patch(RetrieveLatestDao retrieveLatestDao, PatchRequest request, Optional<Long> timestamp); @Override Document addCommentToAlert(CommentAddRemoveRequest request); @Override Document removeCommentFromAlert(CommentAddRemoveRequest request); @Override Map<String, FieldType> getColumnMetadata(List<String> indices); @Override Optional<Map<String, Object>> getLatestResult(GetRequest request); @Override Document addCommentToAlert(CommentAddRemoveRequest request, Document latest); @Override Document removeCommentFromAlert(CommentAddRemoveRequest request, Document latest); ElasticsearchDao withRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy); ElasticsearchClient getClient();  }
@Test public void searchShouldSortByGivenFields() throws Exception { Map<String, FieldType> columnMetadata = new HashMap<>(); columnMetadata.put("sortByStringDesc", FieldType.TEXT); columnMetadata.put("sortByIntAsc", FieldType.INTEGER); setup(RestStatus.OK, 25, columnMetadata); SortField[] expectedSortFields = { sortBy("sortByStringDesc", SortOrder.DESC), sortBy("sortByIntAsc", SortOrder.ASC), sortBy("sortByUndefinedDesc", SortOrder.DESC) }; 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"); SearchResponse searchResponse = dao.search(searchRequest); assertNotNull(searchResponse); 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(); JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(ElasticsearchUtils.toJSON(request).orElse("???")); JSONArray sortFields = (JSONArray) json.get("sort"); assertEquals(3, sortFields.size()); { 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")); } { 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")); } { 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")); } }
@Test public void searchShouldWildcardIndices() throws Exception { setup(RestStatus.OK, 25); SortField[] expectedSortFields = { sortBy("sortByStringDesc", SortOrder.DESC), sortBy("sortByIntAsc", SortOrder.ASC), sortBy("sortByUndefinedDesc", SortOrder.DESC) }; 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"); SearchResponse searchResponse = dao.search(searchRequest); assertNotNull(searchResponse); 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(); JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(ElasticsearchUtils.toJSON(request).orElse("???")); String[] expected = {"bro_index*", "snort_index*"}; assertArrayEquals(expected, request.indices()); }
@Test public void searchShouldThrowExceptionWhenMaxResultsAreExceeded() throws Exception { int maxSearchResults = 20; setup(RestStatus.OK, maxSearchResults); SearchRequest searchRequest = new SearchRequest(); searchRequest.setSize(maxSearchResults + 1); searchRequest.setQuery(""); assertThrows(InvalidSearchException.class, () -> dao.search(searchRequest)); }