Search in sources :

Example 16 with SearchSourceBuilder

use of org.opensearch.search.builder.SearchSourceBuilder in project anomaly-detection by opensearch-project.

the class ParseUtilsTests method testGenerateInternalFeatureQuery.

public void testGenerateInternalFeatureQuery() throws IOException {
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(null, Instant.now());
    long startTime = randomLong();
    long endTime = randomLong();
    SearchSourceBuilder builder = ParseUtils.generateInternalFeatureQuery(detector, startTime, endTime, TestHelpers.xContentRegistry());
    for (Feature feature : detector.getFeatureAttributes()) {
        assertTrue(builder.toString().contains(feature.getId()));
    }
}
Also used : Feature(org.opensearch.ad.model.Feature) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 17 with SearchSourceBuilder

use of org.opensearch.search.builder.SearchSourceBuilder in project anomaly-detection by opensearch-project.

the class ParseUtilsTests method testAddUserRoleFilterWithNullUserBackendRole.

public void testAddUserRoleFilterWithNullUserBackendRole() {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    addUserBackendRolesFilter(new User(randomAlphaOfLength(5), null, ImmutableList.of(randomAlphaOfLength(5)), ImmutableList.of(randomAlphaOfLength(5))), searchSourceBuilder);
    assertEquals("{\"query\":{\"bool\":{\"must\":[{\"nested\":{\"query\":{\"terms\":{\"user.backend_roles.keyword\":[]," + "\"boost\":1.0}},\"path\":\"user\",\"ignore_unmapped\":false,\"score_mode\":\"none\",\"boost\":1.0}}]," + "\"adjust_pure_negative\":true,\"boost\":1.0}}}", searchSourceBuilder.toString());
}
Also used : User(org.opensearch.commons.authuser.User) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 18 with SearchSourceBuilder

use of org.opensearch.search.builder.SearchSourceBuilder in project anomaly-detection by opensearch-project.

the class ParseUtilsTests method testAddUserRoleFilterWithEmptyUserBackendRole.

public void testAddUserRoleFilterWithEmptyUserBackendRole() {
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    addUserBackendRolesFilter(new User(randomAlphaOfLength(5), ImmutableList.of(), ImmutableList.of(randomAlphaOfLength(5)), ImmutableList.of(randomAlphaOfLength(5))), searchSourceBuilder);
    assertEquals("{\"query\":{\"bool\":{\"must\":[{\"nested\":{\"query\":{\"terms\":{\"user.backend_roles.keyword\":[]," + "\"boost\":1.0}},\"path\":\"user\",\"ignore_unmapped\":false,\"score_mode\":\"none\",\"boost\":1.0}}]," + "\"adjust_pure_negative\":true,\"boost\":1.0}}}", searchSourceBuilder.toString());
}
Also used : User(org.opensearch.commons.authuser.User) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 19 with SearchSourceBuilder

use of org.opensearch.search.builder.SearchSourceBuilder in project anomaly-detection by opensearch-project.

the class ParseUtilsTests method testBatchFeatureQuery.

public void testBatchFeatureQuery() throws IOException {
    String index = randomAlphaOfLength(5);
    Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    Feature feature1 = TestHelpers.randomFeature(true);
    Feature feature2 = TestHelpers.randomFeature(false);
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableList.of(index), ImmutableList.of(feature1, feature2), null, now, 1, false, null);
    long startTime = now.minus(10, ChronoUnit.DAYS).toEpochMilli();
    long endTime = now.plus(10, ChronoUnit.DAYS).toEpochMilli();
    SearchSourceBuilder searchSourceBuilder = ParseUtils.batchFeatureQuery(detector, null, startTime, endTime, TestHelpers.xContentRegistry());
    assertEquals("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"range\":{\"" + detector.getTimeField() + "\":{\"from\":" + startTime + ",\"to\":" + endTime + ",\"include_lower\":true,\"include_upper\":false,\"format\":\"epoch_millis\",\"boost\"" + ":1.0}}},{\"bool\":{\"must\":[{\"term\":{\"user\":{\"value\":\"kimchy\",\"boost\":1.0}}}],\"filter\":" + "[{\"term\":{\"tag\":{\"value\":\"tech\",\"boost\":1.0}}}],\"must_not\":[{\"range\":{\"age\":{\"from\":10," + "\"to\":20,\"include_lower\":true,\"include_upper\":true,\"boost\":1.0}}}],\"should\":[{\"term\":{\"tag\":" + "{\"value\":\"wow\",\"boost\":1.0}}},{\"term\":{\"tag\":{\"value\":\"elasticsearch\",\"boost\":1.0}}}]," + "\"adjust_pure_negative\":true,\"minimum_should_match\":\"1\",\"boost\":1.0}}],\"adjust_pure_negative" + "\":true,\"boost\":1.0}},\"aggregations\":{\"feature_aggs\":{\"composite\":{\"size\":10000,\"sources\":" + "[{\"date_histogram\":{\"date_histogram\":{\"field\":\"" + detector.getTimeField() + "\",\"missing_bucket\":false,\"order\":\"asc\"," + "\"fixed_interval\":\"60s\"}}}]},\"aggregations\":{\"" + feature1.getId() + "\":{\"value_count\":{\"field\":\"ok\"}}}}}}", searchSourceBuilder.toString());
}
Also used : Instant(java.time.Instant) Feature(org.opensearch.ad.model.Feature) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 20 with SearchSourceBuilder

use of org.opensearch.search.builder.SearchSourceBuilder in project anomaly-detection by opensearch-project.

the class SearchTopAnomalyResultTransportActionTests method testListenerWithInvalidAggregation.

@SuppressWarnings("unchecked")
public void testListenerWithInvalidAggregation() {
    ActionListener<SearchTopAnomalyResultResponse> mockListener = mock(ActionListener.class);
    SearchTopAnomalyResultTransportAction.TopAnomalyResultListener listener = action.new TopAnomalyResultListener(mockListener, new SearchSourceBuilder(), 1000, 10, SearchTopAnomalyResultTransportAction.OrderType.SEVERITY, "custom-result-index-name");
    // an empty list won't have an entry for 'MULTI_BUCKETS_FIELD' as needed to parse out
    // the expected result buckets, and thus should fail
    Aggregations aggs = new Aggregations(new ArrayList<>());
    SearchResponse response = generateMockSearchResponse(aggs);
    ArgumentCaptor<Exception> failureCaptor = ArgumentCaptor.forClass(Exception.class);
    listener.onResponse(response);
    verify(mockListener, times(1)).onFailure(failureCaptor.capture());
    assertTrue(failureCaptor.getValue() != null);
}
Also used : Aggregations(org.opensearch.search.aggregations.Aggregations) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)397 SearchRequest (org.opensearch.action.search.SearchRequest)214 SearchResponse (org.opensearch.action.search.SearchResponse)151 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)67 ArrayList (java.util.ArrayList)59 IOException (java.io.IOException)58 ActionListener (org.opensearch.action.ActionListener)55 List (java.util.List)50 Map (java.util.Map)42 HashMap (java.util.HashMap)39 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)39 Client (org.opensearch.client.Client)35 ClusterService (org.opensearch.cluster.service.ClusterService)35 LogManager (org.apache.logging.log4j.LogManager)34 Logger (org.apache.logging.log4j.Logger)34 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)34 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)34 Collectors (java.util.stream.Collectors)33 Optional (java.util.Optional)32 TermQueryBuilder (org.opensearch.index.query.TermQueryBuilder)32