-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
.../main/java/com/linkedin/metadata/recommendation/candidatesource/RecentlyEditedSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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)))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...edin/gms/factory/recommendation/candidatesource/RecentlyEditedCandidateSourceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.