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"));
}
}
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);
}
Aggregations