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(recommendations): add last edited entities #6329

Merged
merged 3 commits into from
Nov 15, 2022
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
@@ -0,0 +1,135 @@
package com.linkedin.metadata.recommendation.candidatesource;

import com.codahale.metrics.Timer;
import com.datahub.util.exception.ESQueryException;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.metadata.datahubusage.DataHubUsageEventConstants;
import com.linkedin.metadata.datahubusage.DataHubUsageEventType;
import com.linkedin.metadata.entity.EntityService;
import com.linkedin.metadata.entity.EntityUtils;
import com.linkedin.metadata.recommendation.EntityProfileParams;
import com.linkedin.metadata.recommendation.RecommendationContent;
import com.linkedin.metadata.recommendation.RecommendationParams;
import com.linkedin.metadata.recommendation.RecommendationRenderType;
import com.linkedin.metadata.recommendation.RecommendationRequestContext;
import com.linkedin.metadata.recommendation.ScenarioType;
import com.linkedin.metadata.search.utils.ESUtils;
import com.linkedin.metadata.utils.elasticsearch.IndexConvention;
import com.linkedin.metadata.utils.metrics.MetricUtils;
import io.opentelemetry.extension.annotations.WithSpan;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedTerms;
import org.elasticsearch.search.builder.SearchSourceBuilder;


@Slf4j
@RequiredArgsConstructor
public class RecentlyEditedSource implements RecommendationSource {
private final RestHighLevelClient _searchClient;
private final IndexConvention _indexConvention;
private final EntityService _entityService;

private static final String DATAHUB_USAGE_INDEX = "datahub_usage_event";
private static final String ENTITY_AGG_NAME = "entity";
private static final int MAX_CONTENT = 5;

@Override
public String getTitle() {
return "Recently Edited";
}

@Override
public String getModuleId() {
return "RecentlyEditedEntities";
}

@Override
public RecommendationRenderType getRenderType() {
return RecommendationRenderType.ENTITY_NAME_LIST;
}

@Override
public boolean isEligible(@Nonnull Urn userUrn, @Nonnull RecommendationRequestContext requestContext) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great @CorentinDuhamel !

Would you mind adding a unit test for this class. I understand that existing sources may or may not have these, but I think going forward we'll be requiring (and backfilling) these.

Cheers
John

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's was not in my plan. I have no idea how to do it but if it's done for the "recently viewed" module, I can take inspiration from it.

boolean analyticsEnabled = false;
try {
analyticsEnabled = _searchClient.indices()
.exists(new GetIndexRequest(_indexConvention.getIndexName(DATAHUB_USAGE_INDEX)), RequestOptions.DEFAULT);
} catch (IOException e) {
log.error("Failed to check whether DataHub usage index exists");
}
return requestContext.getScenario() == ScenarioType.HOME && analyticsEnabled;
}

@Override
@WithSpan
public List<RecommendationContent> getRecommendations(@Nonnull Urn userUrn,
@Nonnull RecommendationRequestContext requestContext) {
SearchRequest searchRequest = buildSearchRequest(userUrn);
try (Timer.Context ignored = MetricUtils.timer(this.getClass(), "getRecentlyEdited").time()) {
final SearchResponse searchResponse = _searchClient.search(searchRequest, RequestOptions.DEFAULT);
// extract results
ParsedTerms parsedTerms = searchResponse.getAggregations().get(ENTITY_AGG_NAME);
return parsedTerms.getBuckets()
.stream()
.map(bucket -> buildContent(bucket.getKeyAsString()))
.filter(Optional::isPresent)
.map(Optional::get).limit(MAX_CONTENT)
.collect(Collectors.toList());
} catch (Exception e) {
log.error("Search query to get most recently edited entities failed", e);
throw new ESQueryException("Search query failed:", e);
}
}

private SearchRequest buildSearchRequest(@Nonnull Urn userUrn) {
SearchRequest request = new SearchRequest();
SearchSourceBuilder source = new SearchSourceBuilder();
BoolQueryBuilder query = QueryBuilders.boolQuery();
// Filter for the entity action events
query.must(
QueryBuilders.termQuery(DataHubUsageEventConstants.TYPE, DataHubUsageEventType.ENTITY_ACTION_EVENT.getType()));
source.query(query);

// Find the entity with the largest last viewed timestamp
String lastViewed = "last_viewed";
AggregationBuilder aggregation = AggregationBuilders.terms(ENTITY_AGG_NAME)
.field(DataHubUsageEventConstants.ENTITY_URN + ESUtils.KEYWORD_SUFFIX)
.size(MAX_CONTENT)
.order(BucketOrder.aggregation(lastViewed, false))
.subAggregation(AggregationBuilders.max(lastViewed).field(DataHubUsageEventConstants.TIMESTAMP));
source.aggregation(aggregation);
source.size(0);

request.source(source);
request.indices(_indexConvention.getIndexName(DATAHUB_USAGE_INDEX));
return request;
}

private Optional<RecommendationContent> buildContent(@Nonnull String entityUrn) {
Urn entity = UrnUtils.getUrn(entityUrn);
if (EntityUtils.checkIfRemoved(_entityService, entity)) {
return Optional.empty();
}

return Optional.of(new RecommendationContent().setEntity(entity)
.setValue(entityUrn)
.setParams(new RecommendationParams().setEntityProfileParams(new EntityProfileParams().setUrn(entity))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.google.common.collect.ImmutableList;
import com.linkedin.gms.factory.recommendation.candidatesource.DomainsCandidateSourceFactory;
import com.linkedin.gms.factory.recommendation.candidatesource.MostPopularCandidateSourceFactory;
import com.linkedin.gms.factory.recommendation.candidatesource.RecentlyViewedCandidateSourceFactory;
import com.linkedin.gms.factory.recommendation.candidatesource.RecentlyEditedCandidateSourceFactory;
import com.linkedin.gms.factory.recommendation.candidatesource.TopPlatformsCandidateSourceFactory;
import com.linkedin.gms.factory.recommendation.candidatesource.TopTagsCandidateSourceFactory;
import com.linkedin.gms.factory.recommendation.candidatesource.TopTermsCandidateSourceFactory;
Expand All @@ -12,6 +12,7 @@
import com.linkedin.metadata.recommendation.candidatesource.MostPopularSource;
import com.linkedin.metadata.recommendation.candidatesource.RecentlySearchedSource;
import com.linkedin.metadata.recommendation.candidatesource.RecentlyViewedSource;
import com.linkedin.metadata.recommendation.candidatesource.RecentlyEditedSource;
import com.linkedin.metadata.recommendation.candidatesource.RecommendationSource;
import com.linkedin.metadata.recommendation.candidatesource.TopPlatformsSource;
import com.linkedin.metadata.recommendation.candidatesource.TopTagsSource;
Expand All @@ -27,7 +28,7 @@


@Configuration
@Import({TopPlatformsCandidateSourceFactory.class, RecentlyViewedCandidateSourceFactory.class,
@Import({TopPlatformsCandidateSourceFactory.class, RecentlyEditedCandidateSourceFactory.class,
MostPopularCandidateSourceFactory.class, TopTagsCandidateSourceFactory.class, TopTermsCandidateSourceFactory.class, DomainsCandidateSourceFactory.class})
public class RecommendationServiceFactory {

Expand All @@ -39,6 +40,10 @@ public class RecommendationServiceFactory {
@Qualifier("recentlyViewedCandidateSource")
private RecentlyViewedSource recentlyViewedCandidateSource;

@Autowired
@Qualifier("recentlyEditedCandidateSource")
private RecentlyEditedSource recentlyEditedCandidateSource;

@Autowired
@Qualifier("mostPopularCandidateSource")
private MostPopularSource _mostPopularCandidateSource;
Expand Down Expand Up @@ -67,7 +72,7 @@ protected RecommendationsService getInstance() {
final List<RecommendationSource> candidateSources = ImmutableList.of(
topPlatformsCandidateSource,
domainsCandidateSource,
recentlyViewedCandidateSource, _mostPopularCandidateSource,
recentlyViewedCandidateSource, recentlyEditedCandidateSource, _mostPopularCandidateSource,
topTagsCandidateSource, topTermsCandidateSource, recentlySearchedCandidateSource);
return new RecommendationsService(candidateSources, new SimpleRecommendationRanker());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.linkedin.gms.factory.recommendation.candidatesource;

import com.linkedin.gms.factory.common.IndexConventionFactory;
import com.linkedin.gms.factory.common.RestHighLevelClientFactory;
import com.linkedin.gms.factory.entity.EntityServiceFactory;
import com.linkedin.metadata.entity.EntityService;
import com.linkedin.metadata.recommendation.candidatesource.RecentlyEditedSource;
import com.linkedin.metadata.utils.elasticsearch.IndexConvention;
import javax.annotation.Nonnull;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;


@Configuration
@Import({RestHighLevelClientFactory.class, IndexConventionFactory.class, EntityServiceFactory.class})
public class RecentlyEditedCandidateSourceFactory {
@Autowired
@Qualifier("elasticSearchRestHighLevelClient")
private RestHighLevelClient searchClient;

@Autowired
@Qualifier(IndexConventionFactory.INDEX_CONVENTION_BEAN)
private IndexConvention indexConvention;

@Autowired
@Qualifier("entityService")
private EntityService entityService;

@Bean(name = "recentlyEditedCandidateSource")
@Nonnull
protected RecentlyEditedSource getInstance() {
return new RecentlyEditedSource(searchClient, indexConvention, entityService);
}
}