From af5a3585704e46b74175489a89d893143a671328 Mon Sep 17 00:00:00 2001 From: David Leifker Date: Thu, 3 Oct 2024 07:23:26 -0500 Subject: [PATCH] test only logging --- .../elasticsearch/query/ESSearchDAO.java | 43 +++++++++++++++++++ .../fixtures/SampleDataFixtureTestBase.java | 30 +++++++++++-- .../SampleDataFixtureConfiguration.java | 3 +- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/metadata-io/src/main/java/com/linkedin/metadata/search/elasticsearch/query/ESSearchDAO.java b/metadata-io/src/main/java/com/linkedin/metadata/search/elasticsearch/query/ESSearchDAO.java index cec73de7041263..be74c2ef422775 100644 --- a/metadata-io/src/main/java/com/linkedin/metadata/search/elasticsearch/query/ESSearchDAO.java +++ b/metadata-io/src/main/java/com/linkedin/metadata/search/elasticsearch/query/ESSearchDAO.java @@ -6,7 +6,9 @@ import com.codahale.metrics.Timer; import com.datahub.util.exception.ESQueryException; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; import com.linkedin.data.template.LongMap; import com.linkedin.metadata.config.search.SearchConfiguration; @@ -78,6 +80,24 @@ public class ESSearchDAO { @Nonnull private final SearchConfiguration searchConfiguration; @Nullable private final CustomSearchConfiguration customSearchConfiguration; @Nonnull private final QueryFilterRewriteChain queryFilterRewriteChain; + private final boolean testLoggingEnabled; + + public ESSearchDAO( + RestHighLevelClient client, + boolean pointInTimeCreationEnabled, + String elasticSearchImplementation, + @Nonnull SearchConfiguration searchConfiguration, + @Nullable CustomSearchConfiguration customSearchConfiguration, + @Nonnull QueryFilterRewriteChain queryFilterRewriteChain) { + this( + client, + pointInTimeCreationEnabled, + elasticSearchImplementation, + searchConfiguration, + customSearchConfiguration, + queryFilterRewriteChain, + false); + } public long docCount(@Nonnull OperationContext opContext, @Nonnull String entityName) { return docCount(opContext, entityName, null); @@ -279,6 +299,11 @@ public SearchResult search( searchRequest.indices( entityNames.stream().map(indexConvention::getEntityIndexName).toArray(String[]::new)); searchRequestTimer.stop(); + + if (testLoggingEnabled) { + testLog(opContext.getObjectMapper(), searchRequest); + } + // Step 2: execute the query and extract results, validated against document model as well return executeAndExtract(opContext, entitySpecs, searchRequest, transformedFilters, from, size); } @@ -478,6 +503,11 @@ public ScrollResult scroll( } scrollRequestTimer.stop(); + + if (testLoggingEnabled) { + testLog(opContext.getObjectMapper(), searchRequest); + } + return executeAndExtract( opContext, entitySpecs, searchRequest, transformedFilters, keepAlive, size); } @@ -605,4 +635,17 @@ public ExplainResponse explain( throw new IllegalStateException("Failed to explain query:", e); } } + + private static void testLog(ObjectMapper mapper, SearchRequest searchRequest) { + try { + final String[] indices = searchRequest.indices(); + log.warn(String.format("SearchRequest: %s", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(indices))); + log.warn( + String.format( + "SearchRequest: %s", + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(searchRequest.source()))); + } catch (JsonProcessingException e) { + log.warn("Error writing test log"); + } + } } diff --git a/metadata-io/src/test/java/com/linkedin/metadata/search/fixtures/SampleDataFixtureTestBase.java b/metadata-io/src/test/java/com/linkedin/metadata/search/fixtures/SampleDataFixtureTestBase.java index 939c13c80580e0..eb849019b23622 100644 --- a/metadata-io/src/test/java/com/linkedin/metadata/search/fixtures/SampleDataFixtureTestBase.java +++ b/metadata-io/src/test/java/com/linkedin/metadata/search/fixtures/SampleDataFixtureTestBase.java @@ -1715,7 +1715,15 @@ public void testOr() { assertTrue( result.getEntities().stream().noneMatch(e -> e.getMatchedFields().isEmpty()), String.format("%s - Expected search results to include matched fields", query)); - assertEquals(result.getEntities().size(), 2); + assertEquals( + result.getEntities().size(), + 2, + String.format( + "Query: `%s` Results: %s", + query, + result.getEntities().stream() + .map(SearchEntity::getEntity) + .collect(Collectors.toList()))); } @Test @@ -1738,7 +1746,15 @@ public void testNegate() { assertTrue( result.getEntities().stream().noneMatch(e -> e.getMatchedFields().isEmpty()), String.format("%s - Expected search results to include matched fields", query)); - assertEquals(result.getEntities().size(), 2); + assertEquals( + result.getEntities().size(), + 2, + String.format( + "Query: `%s` Results: %s", + query, + result.getEntities().stream() + .map(SearchEntity::getEntity) + .collect(Collectors.toList()))); } @Test @@ -1908,7 +1924,15 @@ public void testPrefixVsExact() { result.getEntities().stream().noneMatch(e -> e.getMatchedFields().isEmpty()), String.format("%s - Expected search results to include matched fields", query)); - assertEquals(result.getEntities().size(), 2); + assertEquals( + result.getEntities().size(), + 2, + String.format( + "Query: `%s` Results: %s", + query, + result.getEntities().stream() + .map(SearchEntity::getEntity) + .collect(Collectors.toList()))); assertEquals( result.getEntities().get(0).getEntity().toString(), "urn:li:dataset:(urn:li:dataPlatform:dbt,cypress_project.jaffle_shop.customers,PROD)", diff --git a/metadata-io/src/test/java/io/datahubproject/test/fixtures/search/SampleDataFixtureConfiguration.java b/metadata-io/src/test/java/io/datahubproject/test/fixtures/search/SampleDataFixtureConfiguration.java index df278bd305fd5f..e47cdf80281c9a 100644 --- a/metadata-io/src/test/java/io/datahubproject/test/fixtures/search/SampleDataFixtureConfiguration.java +++ b/metadata-io/src/test/java/io/datahubproject/test/fixtures/search/SampleDataFixtureConfiguration.java @@ -196,7 +196,8 @@ protected ElasticSearchService entitySearchServiceHelper(EntityIndexBuilders ind ELASTICSEARCH_IMPLEMENTATION_ELASTICSEARCH, _searchConfiguration, _customSearchConfiguration, - queryFilterRewriteChain); + queryFilterRewriteChain, + true); ESBrowseDAO browseDAO = new ESBrowseDAO( _searchClient,