Skip to content

Commit

Permalink
test only logging
Browse files Browse the repository at this point in the history
  • Loading branch information
david-leifker committed Oct 3, 2024
1 parent 61fe36e commit af5a358
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -478,6 +503,11 @@ public ScrollResult scroll(
}

scrollRequestTimer.stop();

if (testLoggingEnabled) {
testLog(opContext.getObjectMapper(), searchRequest);
}

return executeAndExtract(
opContext, entitySpecs, searchRequest, transformedFilters, keepAlive, size);
}
Expand Down Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ protected ElasticSearchService entitySearchServiceHelper(EntityIndexBuilders ind
ELASTICSEARCH_IMPLEMENTATION_ELASTICSEARCH,
_searchConfiguration,
_customSearchConfiguration,
queryFilterRewriteChain);
queryFilterRewriteChain,
true);
ESBrowseDAO browseDAO =
new ESBrowseDAO(
_searchClient,
Expand Down

0 comments on commit af5a358

Please sign in to comment.