MikeThomsen commented on code in PR #6487: URL: https://github.com/apache/nifi/pull/6487#discussion_r989524375
########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-client-service/src/test/java/org/apache/nifi/elasticsearch/integration/ElasticSearchClientService_IT.java: ########## @@ -0,0 +1,741 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License") you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.elasticsearch.integration; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.nifi.elasticsearch.DeleteOperationResponse; +import org.apache.nifi.elasticsearch.ElasticSearchClientService; +import org.apache.nifi.elasticsearch.ElasticSearchClientServiceImpl; +import org.apache.nifi.elasticsearch.ElasticsearchException; +import org.apache.nifi.elasticsearch.IndexOperationRequest; +import org.apache.nifi.elasticsearch.IndexOperationResponse; +import org.apache.nifi.elasticsearch.SearchResponse; +import org.apache.nifi.elasticsearch.TestControllerServiceProcessor; +import org.apache.nifi.elasticsearch.UpdateOperationResponse; +import org.apache.nifi.security.util.StandardTlsConfiguration; +import org.apache.nifi.security.util.TemporaryKeyStoreBuilder; +import org.apache.nifi.security.util.TlsConfiguration; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public class ElasticSearchClientService_IT extends AbstractElasticsearch_IT { + private TestRunner runner; + private ElasticSearchClientServiceImpl service; + + static final String INDEX = "messages"; + public static String TYPE; + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static TlsConfiguration generatedTlsConfiguration; + private static TlsConfiguration truststoreTlsConfiguration; + + @BeforeAll + static void beforeAll() throws IOException { + startTestcontainer(); + TYPE = getElasticMajorVersion() == 6 ? "_doc" : ""; + System.out.println( + String.format("%n%n%n%n%n%n%n%n%n%n%n%n%n%n%nTYPE: %s%n%s%s%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n", + TYPE, IMAGE.getRepository(), IMAGE.getVersionPart()) + ); + + generatedTlsConfiguration = new TemporaryKeyStoreBuilder().build(); + truststoreTlsConfiguration = new StandardTlsConfiguration( + null, + null, + null, + generatedTlsConfiguration.getTruststorePath(), + generatedTlsConfiguration.getTruststorePassword(), + generatedTlsConfiguration.getTruststoreType() + ); + + setupTestData(); + } + + @AfterAll + public static void afterAll() throws IOException { + tearDownTestData(); + stopTestcontainer(); + } + + @BeforeEach + void before() throws Exception { + runner = TestRunners.newTestRunner(TestControllerServiceProcessor.class); + service = new ElasticSearchClientServiceImpl(); + runner.addControllerService("Client Service", service); + runner.setProperty(service, ElasticSearchClientService.HTTP_HOSTS, ELASTIC_HOST); + runner.setProperty(service, ElasticSearchClientService.CONNECT_TIMEOUT, "10000"); + runner.setProperty(service, ElasticSearchClientService.SOCKET_TIMEOUT, "60000"); + runner.setProperty(service, ElasticSearchClientService.RETRY_TIMEOUT, "60000"); + runner.setProperty(service, ElasticSearchClientService.SUPPRESS_NULLS, ElasticSearchClientService.ALWAYS_SUPPRESS.getValue()); + runner.setProperty(service, ElasticSearchClientService.USERNAME, "elastic"); + runner.setProperty(service, ElasticSearchClientService.PASSWORD, ELASTIC_USER_PASSWORD); + + try { + runner.enableControllerService(service); + } catch (Exception ex) { + throw ex; + } + + service.refresh(null, null); + } + + @AfterEach + void after() throws Exception { + service.onDisabled(); + } + + private String prettyJson(Object o) throws JsonProcessingException { + return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(o); + } + + private class MapBuilder { + private Map<String, Object> toBuild; + + public MapBuilder() { + toBuild = new HashMap<>(); + } + + public MapBuilder put(String key, Object value) { + toBuild.put(key, value); + return this; + } + + public Map<String, Object> build() { + return toBuild; + } + } + + @Test + void testBasicSearch() throws Exception { + Map<String, Object> temp = new MapBuilder() + .put("size", 10) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder() + .put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()) + .build()) + .build(); + String query = prettyJson(temp); + + SearchResponse response = service.search(query, INDEX, TYPE, null); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(10, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations are missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNull(response.getScrollId(), "Unexpected ScrollId"); + assertNull(response.getSearchAfter(), "Unexpected Search_After"); + assertNull(response.getPitId(), "Unexpected pitId"); + + Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + List<Map<String, Object>> buckets = (List<java.util.Map<String, Object>>) termCounts.get("buckets"); + assertNotNull(buckets, "Buckets branch was empty"); + Map<String, Object> expected = new MapBuilder() + .put("one", 1) + .put("two", 2) + .put("three", 3) + .put("four", 4) + .put("five", 5) + .build(); + + buckets.forEach( aggRes -> { + String key = (String) aggRes.get("key"); + Integer docCount = (Integer) aggRes.get("doc_count"); + assertEquals(expected.get(key), docCount, "${key} did not match."); + }); + } + + @Test + void testBasicSearchRequestParameters() throws Exception { + Map<String, Object> temp = new MapBuilder() + .put("size", 10) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder() + .put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()) + .build()) + .build(); + String query = prettyJson(temp); + + + SearchResponse response = service.search(query, "messages", TYPE, createParameters("preference", "_local")); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(10, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations are missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + + Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + List<Map<String, Object>> buckets = (List<Map<String, Object>>) termCounts.get("buckets"); + assertNotNull(buckets, "Buckets branch was empty"); + Map<String, Object> expected = new MapBuilder() + .put("one", 1) + .put("two", 2) + .put("three", 3) + .put("four", 4) + .put("five", 5) + .build(); + + buckets.forEach( (aggRes) -> { + String key = (String) aggRes.get("key"); + Integer docCount = (Integer) aggRes.get("doc_count"); + assertEquals(expected.get(key), docCount, String.format("%s did not match.", key)); + }); + } + + @Test + void testV5SearchWarnings() throws JsonProcessingException { + int version = getElasticMajorVersion(); + assumeTrue(version == 5, "Requires version 5 (no search API deprecations yet for 8.x)"); + + String query = prettyJson(new MapBuilder() + .put("size", 1) + .put("query", new MapBuilder().put("query_string", new MapBuilder() + .put("query", 1) + .put("all_fields", true) + .build()).build()) + .build()); + final SearchResponse response = service.search(query, INDEX, TYPE, null); + assertTrue(!response.getWarnings().isEmpty(), "Missing warnings"); + } + + @Test + public void testV7SearchWarnings() throws JsonProcessingException { + assumeTrue(getElasticMajorVersion() == 7, + "Requires Elasticsearch 7"); + String query = prettyJson(new MapBuilder() + .put("size", 1) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .build()); + String type = "a-type"; + final SearchResponse response = service.search(query, INDEX, type, null); + assertTrue(!response.getWarnings().isEmpty(), "Missing warnings"); + } + + @Test + void testScroll() throws JsonProcessingException { + final String query = prettyJson(new MapBuilder() + .put("size", 2) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder() + .put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()) + .build()) + .build()); + + // initiate the scroll + final SearchResponse response = service.search(query, INDEX, TYPE, Collections.singletonMap("scroll", "10s")); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(2, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations are missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNotNull(response.getScrollId(), "ScrollId missing"); + assertNull(response.getSearchAfter(), "Unexpected Search_After"); + assertNull(response.getPitId(), "Unexpected pitId"); + + final Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + assertEquals(5, ((List)termCounts.get("buckets")).size(), "Buckets count is wrong"); + + // scroll the next page + Map<String, String> parameters = createParameters("scroll_id", response.getScrollId(), "scroll", "10s"); + final SearchResponse scrollResponse = service.scroll(prettyJson(parameters)); + assertNotNull(scrollResponse, "Scroll Response was null"); + + assertEquals(15, scrollResponse.getNumberOfHits(), "Wrong count"); + assertFalse(scrollResponse.isTimedOut(), "Timed out"); + assertNotNull(scrollResponse.getHits(), "Hits was null"); + assertEquals(2, scrollResponse.getHits().size(), "Wrong number of hits"); + assertNotNull(scrollResponse.getAggregations(), "Aggregations missing"); + assertEquals(0, scrollResponse.getAggregations().size(), "Aggregation count is wrong"); + assertNotNull(scrollResponse.getScrollId(), "ScrollId missing"); + assertNull(scrollResponse.getSearchAfter(), "Unexpected Search_After"); + assertNull(scrollResponse.getPitId(), "Unexpected pitId"); + + assertNotEquals(scrollResponse.getHits(), response.getHits(), () -> "Same results"); + + // delete the scroll + DeleteOperationResponse deleteResponse = service.deleteScroll(scrollResponse.getScrollId()); + assertNotNull(deleteResponse, "Delete Response was null"); + assertTrue(deleteResponse.getTook() > 0); + + // delete scroll again (should now be unknown but the 404 caught and ignored) + deleteResponse = service.deleteScroll(scrollResponse.getScrollId()); + assertNotNull(deleteResponse, "Delete Response was null"); + assertEquals(0L, deleteResponse.getTook()); + } + + @Test + void testSearchAfter() throws JsonProcessingException { + final Map<String, Object> queryMap = new MapBuilder() + .put("size", 2) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder().put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()).build()) + .put("sort", Arrays.asList( + new MapBuilder().put("msg", "desc").build() + )) + .build(); + final String query = prettyJson(queryMap); + + // search first page + final SearchResponse response = service.search(query, INDEX, TYPE, null); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(2, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNull(response.getScrollId(), "Unexpected ScrollId"); + assertNotNull(response.getSearchAfter(), "Search_After missing"); + assertNull(response.getPitId(), "Unexpected pitId"); + + final Map termCounts = (Map)response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + assertEquals(5, ((List)termCounts.get("buckets")).size(), "Buckets count is wrong"); + + // search the next page + queryMap.put("search_after", new ObjectMapper().readValue(response.getSearchAfter(), List.class)); Review Comment: Done ########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-client-service/src/test/java/org/apache/nifi/elasticsearch/integration/ElasticSearchClientService_IT.java: ########## @@ -0,0 +1,741 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License") you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.elasticsearch.integration; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.nifi.elasticsearch.DeleteOperationResponse; +import org.apache.nifi.elasticsearch.ElasticSearchClientService; +import org.apache.nifi.elasticsearch.ElasticSearchClientServiceImpl; +import org.apache.nifi.elasticsearch.ElasticsearchException; +import org.apache.nifi.elasticsearch.IndexOperationRequest; +import org.apache.nifi.elasticsearch.IndexOperationResponse; +import org.apache.nifi.elasticsearch.SearchResponse; +import org.apache.nifi.elasticsearch.TestControllerServiceProcessor; +import org.apache.nifi.elasticsearch.UpdateOperationResponse; +import org.apache.nifi.security.util.StandardTlsConfiguration; +import org.apache.nifi.security.util.TemporaryKeyStoreBuilder; +import org.apache.nifi.security.util.TlsConfiguration; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public class ElasticSearchClientService_IT extends AbstractElasticsearch_IT { + private TestRunner runner; + private ElasticSearchClientServiceImpl service; + + static final String INDEX = "messages"; + public static String TYPE; + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static TlsConfiguration generatedTlsConfiguration; + private static TlsConfiguration truststoreTlsConfiguration; + + @BeforeAll + static void beforeAll() throws IOException { + startTestcontainer(); + TYPE = getElasticMajorVersion() == 6 ? "_doc" : ""; + System.out.println( + String.format("%n%n%n%n%n%n%n%n%n%n%n%n%n%n%nTYPE: %s%n%s%s%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n", + TYPE, IMAGE.getRepository(), IMAGE.getVersionPart()) + ); + + generatedTlsConfiguration = new TemporaryKeyStoreBuilder().build(); + truststoreTlsConfiguration = new StandardTlsConfiguration( + null, + null, + null, + generatedTlsConfiguration.getTruststorePath(), + generatedTlsConfiguration.getTruststorePassword(), + generatedTlsConfiguration.getTruststoreType() + ); + + setupTestData(); + } + + @AfterAll + public static void afterAll() throws IOException { + tearDownTestData(); + stopTestcontainer(); + } + + @BeforeEach + void before() throws Exception { + runner = TestRunners.newTestRunner(TestControllerServiceProcessor.class); + service = new ElasticSearchClientServiceImpl(); + runner.addControllerService("Client Service", service); + runner.setProperty(service, ElasticSearchClientService.HTTP_HOSTS, ELASTIC_HOST); + runner.setProperty(service, ElasticSearchClientService.CONNECT_TIMEOUT, "10000"); + runner.setProperty(service, ElasticSearchClientService.SOCKET_TIMEOUT, "60000"); + runner.setProperty(service, ElasticSearchClientService.RETRY_TIMEOUT, "60000"); + runner.setProperty(service, ElasticSearchClientService.SUPPRESS_NULLS, ElasticSearchClientService.ALWAYS_SUPPRESS.getValue()); + runner.setProperty(service, ElasticSearchClientService.USERNAME, "elastic"); + runner.setProperty(service, ElasticSearchClientService.PASSWORD, ELASTIC_USER_PASSWORD); + + try { + runner.enableControllerService(service); + } catch (Exception ex) { + throw ex; + } + + service.refresh(null, null); + } + + @AfterEach + void after() throws Exception { + service.onDisabled(); + } + + private String prettyJson(Object o) throws JsonProcessingException { + return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(o); + } + + private class MapBuilder { + private Map<String, Object> toBuild; + + public MapBuilder() { + toBuild = new HashMap<>(); + } + + public MapBuilder put(String key, Object value) { + toBuild.put(key, value); + return this; + } + + public Map<String, Object> build() { + return toBuild; + } + } + + @Test + void testBasicSearch() throws Exception { + Map<String, Object> temp = new MapBuilder() + .put("size", 10) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder() + .put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()) + .build()) + .build(); + String query = prettyJson(temp); + + SearchResponse response = service.search(query, INDEX, TYPE, null); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(10, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations are missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNull(response.getScrollId(), "Unexpected ScrollId"); + assertNull(response.getSearchAfter(), "Unexpected Search_After"); + assertNull(response.getPitId(), "Unexpected pitId"); + + Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + List<Map<String, Object>> buckets = (List<java.util.Map<String, Object>>) termCounts.get("buckets"); + assertNotNull(buckets, "Buckets branch was empty"); + Map<String, Object> expected = new MapBuilder() + .put("one", 1) + .put("two", 2) + .put("three", 3) + .put("four", 4) + .put("five", 5) + .build(); + + buckets.forEach( aggRes -> { + String key = (String) aggRes.get("key"); + Integer docCount = (Integer) aggRes.get("doc_count"); + assertEquals(expected.get(key), docCount, "${key} did not match."); + }); + } + + @Test + void testBasicSearchRequestParameters() throws Exception { + Map<String, Object> temp = new MapBuilder() + .put("size", 10) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder() + .put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()) + .build()) + .build(); + String query = prettyJson(temp); + + + SearchResponse response = service.search(query, "messages", TYPE, createParameters("preference", "_local")); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(10, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations are missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + + Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + List<Map<String, Object>> buckets = (List<Map<String, Object>>) termCounts.get("buckets"); + assertNotNull(buckets, "Buckets branch was empty"); + Map<String, Object> expected = new MapBuilder() + .put("one", 1) + .put("two", 2) + .put("three", 3) + .put("four", 4) + .put("five", 5) + .build(); + + buckets.forEach( (aggRes) -> { + String key = (String) aggRes.get("key"); + Integer docCount = (Integer) aggRes.get("doc_count"); + assertEquals(expected.get(key), docCount, String.format("%s did not match.", key)); + }); + } + + @Test + void testV5SearchWarnings() throws JsonProcessingException { + int version = getElasticMajorVersion(); + assumeTrue(version == 5, "Requires version 5 (no search API deprecations yet for 8.x)"); + + String query = prettyJson(new MapBuilder() + .put("size", 1) + .put("query", new MapBuilder().put("query_string", new MapBuilder() + .put("query", 1) + .put("all_fields", true) + .build()).build()) + .build()); + final SearchResponse response = service.search(query, INDEX, TYPE, null); + assertTrue(!response.getWarnings().isEmpty(), "Missing warnings"); + } + + @Test + public void testV7SearchWarnings() throws JsonProcessingException { + assumeTrue(getElasticMajorVersion() == 7, + "Requires Elasticsearch 7"); + String query = prettyJson(new MapBuilder() + .put("size", 1) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .build()); + String type = "a-type"; + final SearchResponse response = service.search(query, INDEX, type, null); + assertTrue(!response.getWarnings().isEmpty(), "Missing warnings"); + } + + @Test + void testScroll() throws JsonProcessingException { + final String query = prettyJson(new MapBuilder() + .put("size", 2) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder() + .put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()) + .build()) + .build()); + + // initiate the scroll + final SearchResponse response = service.search(query, INDEX, TYPE, Collections.singletonMap("scroll", "10s")); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(2, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations are missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNotNull(response.getScrollId(), "ScrollId missing"); + assertNull(response.getSearchAfter(), "Unexpected Search_After"); + assertNull(response.getPitId(), "Unexpected pitId"); + + final Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + assertEquals(5, ((List)termCounts.get("buckets")).size(), "Buckets count is wrong"); + + // scroll the next page + Map<String, String> parameters = createParameters("scroll_id", response.getScrollId(), "scroll", "10s"); + final SearchResponse scrollResponse = service.scroll(prettyJson(parameters)); + assertNotNull(scrollResponse, "Scroll Response was null"); + + assertEquals(15, scrollResponse.getNumberOfHits(), "Wrong count"); + assertFalse(scrollResponse.isTimedOut(), "Timed out"); + assertNotNull(scrollResponse.getHits(), "Hits was null"); + assertEquals(2, scrollResponse.getHits().size(), "Wrong number of hits"); + assertNotNull(scrollResponse.getAggregations(), "Aggregations missing"); + assertEquals(0, scrollResponse.getAggregations().size(), "Aggregation count is wrong"); + assertNotNull(scrollResponse.getScrollId(), "ScrollId missing"); + assertNull(scrollResponse.getSearchAfter(), "Unexpected Search_After"); + assertNull(scrollResponse.getPitId(), "Unexpected pitId"); + + assertNotEquals(scrollResponse.getHits(), response.getHits(), () -> "Same results"); + + // delete the scroll + DeleteOperationResponse deleteResponse = service.deleteScroll(scrollResponse.getScrollId()); + assertNotNull(deleteResponse, "Delete Response was null"); + assertTrue(deleteResponse.getTook() > 0); + + // delete scroll again (should now be unknown but the 404 caught and ignored) + deleteResponse = service.deleteScroll(scrollResponse.getScrollId()); + assertNotNull(deleteResponse, "Delete Response was null"); + assertEquals(0L, deleteResponse.getTook()); + } + + @Test + void testSearchAfter() throws JsonProcessingException { + final Map<String, Object> queryMap = new MapBuilder() + .put("size", 2) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder().put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()).build()) + .put("sort", Arrays.asList( + new MapBuilder().put("msg", "desc").build() + )) + .build(); + final String query = prettyJson(queryMap); + + // search first page + final SearchResponse response = service.search(query, INDEX, TYPE, null); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(2, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNull(response.getScrollId(), "Unexpected ScrollId"); + assertNotNull(response.getSearchAfter(), "Search_After missing"); + assertNull(response.getPitId(), "Unexpected pitId"); + + final Map termCounts = (Map)response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + assertEquals(5, ((List)termCounts.get("buckets")).size(), "Buckets count is wrong"); + + // search the next page + queryMap.put("search_after", new ObjectMapper().readValue(response.getSearchAfter(), List.class)); + queryMap.remove("aggs"); + final String secondPage = prettyJson(queryMap); + final SearchResponse secondResponse = service.search(secondPage, INDEX, TYPE, null); + assertNotNull(secondResponse, "Second Response was null"); + + assertEquals(15, secondResponse.getNumberOfHits(), "Wrong count"); + assertFalse(secondResponse.isTimedOut(), "Timed out"); + assertNotNull(secondResponse.getHits(), "Hits was null"); + assertEquals(2, secondResponse.getHits().size(), "Wrong number of hits"); + assertNotNull(secondResponse.getAggregations(), "Aggregations missing"); + assertEquals(0, secondResponse.getAggregations().size(), "Aggregation count is wrong"); + assertNull(secondResponse.getScrollId(), "Unexpected ScrollId"); + assertNotNull(secondResponse.getSearchAfter(), "Unexpected Search_After"); + assertNull(secondResponse.getPitId(), "Unexpected pitId"); + + assertNotEquals(secondResponse.getHits(), response.getHits(), "Same results"); + } + + @Test + void testPointInTime() throws JsonProcessingException { + // Point in Time only available in 7.10+ with XPack enabled + double majorVersion = getElasticMajorVersion(); + double minorVersion = getElasticMinorVersion(); + assumeTrue(majorVersion >= 8 || (majorVersion == 7 && minorVersion >= 10), "Requires version 7.10+"); + + // initialise + final String pitId = service.initialisePointInTime(INDEX, "10s"); + + final Map<String, Object> queryMap = new MapBuilder() + .put("size", 2) + .put("query", new MapBuilder().put("match_all", new HashMap<>()).build()) + .put("aggs", new MapBuilder().put("term_counts", new MapBuilder() + .put("terms", new MapBuilder() + .put("field", "msg") + .put("size", 5) + .build()) + .build()).build()) + .put("sort", Arrays.asList( + new MapBuilder().put("msg", "desc").build() + )) + .put("pit", new MapBuilder() + .put("id", pitId) + .put("keep_alive", "10s") + .build()) + .build(); + final String query = prettyJson(queryMap); + + // search first page + final SearchResponse response = service.search(query, null, TYPE, null); + assertNotNull(response, "Response was null"); + + assertEquals(15, response.getNumberOfHits(), "Wrong count"); + assertFalse(response.isTimedOut(), "Timed out"); + assertNotNull(response.getHits(), "Hits was null"); + assertEquals(2, response.getHits().size(), "Wrong number of hits"); + assertNotNull(response.getAggregations(), "Aggregations missing"); + assertEquals(1, response.getAggregations().size(), "Aggregation count is wrong"); + assertNull(response.getScrollId(), "Unexpected ScrollId"); + assertNotNull(response.getSearchAfter(), "Unexpected Search_After"); + assertNotNull(response.getPitId(), "pitId missing"); + + final Map termCounts = (Map) response.getAggregations().get("term_counts"); + assertNotNull(termCounts, "Term counts was missing"); + assertEquals(5, ((List)termCounts.get("buckets")).size(), "Buckets count is wrong"); + + // search the next page + queryMap.put("search_after", new ObjectMapper().readValue(response.getSearchAfter(), List.class)); Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org