Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql): implement sort and facet for scroll #12746

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private List<AnalyticsChart> getCharts(MetadataAnalyticsInput input, OperationCo

SearchResult searchResult =
_entityClient.searchAcrossEntities(
opContext, entities, query, filter, 0, 0, Collections.emptyList(), null);
opContext, entities, query, filter, 0, 0, Collections.emptyList());

List<AggregationMetadata> aggregationMetadataList =
searchResult.getMetadata().getAggregations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ public CompletableFuture<SearchResults> get(final DataFetchingEnvironment enviro
new CriterionArray(ImmutableList.of(filterCriterion))))),
start,
count,
Collections.emptyList(),
null));
Collections.emptyList()));

} catch (Exception e) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ public CompletableFuture<SearchResults> get(DataFetchingEnvironment environment)
finalFilter,
start,
count,
null,
null));
results
.getSearchResults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public CompletableFuture<SearchResults> get(final DataFetchingEnvironment enviro
new ConjunctiveCriterion().setAnd(criteria))),
start,
count,
Collections.emptyList(),
null));
Collections.emptyList()));

} catch (Exception e) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ private SearchResult getSearchResults(
: null,
0,
0,
Collections.emptyList(),
null);
Collections.emptyList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.linkedin.entity.client.EntityClient;
import com.linkedin.metadata.query.SearchFlags;
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.query.filter.SortCriterion;
import com.linkedin.metadata.service.ViewService;
import com.linkedin.view.DataHubViewInfo;
import graphql.schema.DataFetcher;
Expand Down Expand Up @@ -80,6 +81,7 @@ public CompletableFuture<ScrollResults> get(DataFetchingEnvironment environment)
} else {
searchFlags = null;
}
List<SortCriterion> sortCriteria = SearchUtils.getSortCriteria(input.getSortInput());

try {
log.debug(
Expand Down Expand Up @@ -108,6 +110,7 @@ public CompletableFuture<ScrollResults> get(DataFetchingEnvironment environment)
: baseFilter,
scrollId,
keepAlive,
sortCriteria,
count));
} catch (Exception e) {
log.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ private List<String> getStructuredPropertyFacets(final QueryContext context) {
createStructuredPropertyFilter(),
0,
100,
Collections.emptyList(),
null);
Collections.emptyList());
return result.getEntities().stream()
.map(entity -> String.format("structuredProperties.%s", entity.getEntity().getId()))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ public static CompletableFuture<ScrollResults> scrollAcrossEntities(
Integer inputCount,
String scrollId,
String inputKeepAlive,
List<SortCriterion> sortCriteria,
List<String> facets,
String className) {

final List<EntityType> entityTypes =
Expand Down Expand Up @@ -431,7 +433,15 @@ public static CompletableFuture<ScrollResults> scrollAcrossEntities(
try {
final ScrollResult scrollResult =
_entityClient.scrollAcrossEntities(
context, finalEntityNames, query, finalFilters, scrollId, keepAlive, count);
context,
finalEntityNames,
query,
finalFilters,
scrollId,
keepAlive,
sortCriteria,
count,
facets);
return UrnScrollResultsMapper.map(inputContext, scrollResult);
} catch (Exception e) {
log.warn(
Expand Down Expand Up @@ -518,14 +528,7 @@ public static CompletableFuture<SearchResults> searchAcrossEntities(
try {
final SearchResult searchResult =
_entityClient.searchAcrossEntities(
context,
finalEntityNames,
query,
finalFilters,
start,
count,
sortCriteria,
null);
context, finalEntityNames, query, finalFilters, start, count, sortCriteria);
return UrnSearchResultsMapper.map(inputContext, searchResult);
} catch (Exception e) {
log.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.linkedin.metadata.utils.CriterionUtils;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -61,6 +62,8 @@ public CompletableFuture<ScrollResults> get(DataFetchingEnvironment environment)
input.getCount(),
input.getScrollId(),
input.getKeepAlive(),
List.of(),
List.of(),
this.getClass().getSimpleName());
}
}
5 changes: 5 additions & 0 deletions datahub-graphql-core/src/main/resources/search.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ input ScrollAcrossEntitiesInput {
Flags controlling search options
"""
searchFlags: SearchFlags

"""
Optional - Information on how to sort this search result
"""
sortInput: SearchSortInput
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public void testGetSuccess() throws Exception {
new CriterionArray(ImmutableList.of(filterCriterion)))))),
Mockito.eq(0),
Mockito.eq(20),
Mockito.eq(Collections.emptyList()),
Mockito.eq(null)))
Mockito.eq(Collections.emptyList())))
.thenReturn(
new SearchResult()
.setFrom(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public void testGetSuccess() throws Exception {
new CriterionArray(ImmutableList.of(filterCriterion)))))),
Mockito.eq(0),
Mockito.eq(20),
Mockito.eq(Collections.emptyList()),
Mockito.eq(null)))
Mockito.eq(Collections.emptyList())))
.thenReturn(
new SearchResult()
.setFrom(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ private static EntityClient initMockEntityClient(
Mockito.eq(filter),
Mockito.eq(start),
Mockito.eq(limit),
Mockito.eq(Collections.emptyList()),
Mockito.eq(null)))
Mockito.eq(Collections.emptyList())))
.thenReturn(result);
return client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.linkedin.metadata.Constants.*;
import static com.linkedin.metadata.utils.CriterionUtils.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

Expand Down Expand Up @@ -39,6 +40,7 @@
import com.linkedin.view.DataHubViewInfo;
import com.linkedin.view.DataHubViewType;
import graphql.schema.DataFetchingEnvironment;
import io.datahubproject.metadata.context.OperationContext;
import java.util.List;
import java.util.concurrent.CompletionException;
import org.mockito.Mockito;
Expand Down Expand Up @@ -144,8 +146,7 @@ public void testGetSuccessBasic() throws Exception {
List.of(
new com.linkedin.metadata.query.filter.SortCriterion()
.setField(VERSION_SORT_ID_FIELD_NAME)
.setOrder(com.linkedin.metadata.query.filter.SortOrder.DESCENDING))),
any());
.setOrder(com.linkedin.metadata.query.filter.SortOrder.DESCENDING))));
}

@Test
Expand Down Expand Up @@ -240,8 +241,7 @@ public void testGetSuccessComplex() throws Exception {
.setOrder(com.linkedin.metadata.query.filter.SortOrder.ASCENDING),
new com.linkedin.metadata.query.filter.SortCriterion()
.setField(VERSION_SORT_ID_FIELD_NAME)
.setOrder(com.linkedin.metadata.query.filter.SortOrder.DESCENDING))),
any());
.setOrder(com.linkedin.metadata.query.filter.SortOrder.DESCENDING))));
}

@Test
Expand All @@ -251,7 +251,7 @@ public void testThrowsError() throws Exception {

Mockito.when(
mockEntityClient.searchAcrossEntities(
any(), any(), any(), any(), Mockito.anyInt(), Mockito.anyInt(), any(), any()))
any(), any(), any(), any(), Mockito.anyInt(), Mockito.anyInt(), any()))
.thenThrow(new RemoteInvocationException());

VersionsSearchResolver resolver = new VersionsSearchResolver(mockEntityClient, mockViewService);
Expand All @@ -271,23 +271,36 @@ public void testThrowsError() throws Exception {
private EntityClient initMockEntityClient() throws Exception {
EntityClient client = Mockito.mock(EntityClient.class);

SearchResult result =
new SearchResult()
.setEntities(new SearchEntityArray())
.setNumEntities(0)
.setFrom(0)
.setPageSize(0)
.setMetadata(new SearchResultMetadata());

Mockito.when(
client.searchAcrossEntities(
any(),
any(OperationContext.class),
any(),
Mockito.anyString(),
any(),
any(Filter.class),
Mockito.anyInt(),
Mockito.anyInt(),
anyList(),
anyList()))
.thenReturn(result);

Mockito.when(
client.searchAcrossEntities(
any(OperationContext.class),
any(),
Mockito.eq(null)))
.thenReturn(
new SearchResult()
.setEntities(new SearchEntityArray())
.setNumEntities(0)
.setFrom(0)
.setPageSize(0)
.setMetadata(new SearchResultMetadata()));
anyString(),
any(Filter.class),
anyInt(),
anyInt(),
anyList()))
.thenReturn(result);

return client;
}
Expand Down
44 changes: 40 additions & 4 deletions docs/api/graphql/graphql-best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ This technique makes maintaining your GraphQL queries much more doable. For exam

`search*` APIs such as [`searchAcrossEntities`](https://datahubproject.io/docs/GraphQL/queries/#searchacrossentities) are designed for minimal pagination (< ~50). They do not perform well for deep pagination requests. Use the equivalent `scroll*` APIs such as [`scrollAcrossEntities`](https://datahubproject.io/docs/GraphQL/queries/#scrollacrossentities) when expecting the need to paginate deeply into the result set.

Note: that it is impossible to use `search*` for paginating beyond 10k results.
:::note
It is impossible to use `search*` for paginating beyond 10k results.
:::

:::caution
In order to `scroll*` through the entire result set it is required to use a stable sort order. This means using `_score` as
the first sort order cannot be used. Use the `urn` field as the sort order instead.
:::

#### Examples

Expand All @@ -54,7 +61,15 @@ Page 1 Request:
orFilters: [
{ and: [{ field: "name", condition: CONTAIN, values: ["pet"] }] },
{ and: [{ field: "title", condition: CONTAIN, values: ["pet"] }] }
]
],
sortInput: {
sortCriteria: [
{
field: "urn",
sortOrder: ASCENDING
}
]
}
}
) {
nextScrollId
Expand Down Expand Up @@ -110,7 +125,15 @@ Page 2 Request:
orFilters: [
{ and: [{ field: "name", condition: CONTAIN, values: ["pet"] }] },
{ and: [{ field: "title", condition: CONTAIN, values: ["pet"] }] }
]
],
sortInput: {
sortCriteria: [
{
field: "urn",
sortOrder: ASCENDING
}
]
}
}
) {
nextScrollId
Expand Down Expand Up @@ -282,7 +305,20 @@ Example for skipping highlighting and aggregates, typically used for scrolling s
```graphql
{
scrollAcrossEntities(
input: {types: [DATASET], count: 2, query: "pet", searchFlags: {skipAggregates: true, skipHighlighting: true}}
input: {
types: [DATASET],
count: 2,
query: "pet",
searchFlags: {skipAggregates: true, skipHighlighting: true},
sortInput: {
sortCriteria: [
{
field: "urn",
sortOrder: ASCENDING
}
]
},
}
) {
searchResults {
entity {
Expand Down
Loading
Loading