forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(autocomplete): fix autocomplete duplicate field
* refactor to base class * prevent duplicate field names
- Loading branch information
1 parent
32b654c
commit eefd99b
Showing
8 changed files
with
147 additions
and
119 deletions.
There are no files selected for viewing
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
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
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
66 changes: 66 additions & 0 deletions
66
...ain/java/com/linkedin/metadata/search/elasticsearch/query/request/BaseRequestHandler.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,66 @@ | ||
package com.linkedin.metadata.search.elasticsearch.query.request; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import io.datahubproject.metadata.context.OperationContext; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import org.opensearch.search.fetch.subphase.highlight.HighlightBuilder; | ||
|
||
public abstract class BaseRequestHandler { | ||
|
||
/** | ||
* Provide the fields which are queried by default | ||
* | ||
* @return collection of field names | ||
*/ | ||
protected abstract Collection<String> getDefaultQueryFieldNames(); | ||
|
||
protected abstract Stream<String> highlightFieldExpansion( | ||
@Nonnull OperationContext opContext, @Nonnull String fieldName); | ||
|
||
@VisibleForTesting | ||
public HighlightBuilder getDefaultHighlights(@Nonnull OperationContext opContext) { | ||
return getHighlights(opContext, null); | ||
} | ||
|
||
@VisibleForTesting | ||
public HighlightBuilder getHighlights( | ||
@Nonnull OperationContext opContext, @Nullable Collection<String> fieldsToHighlight) { | ||
HighlightBuilder highlightBuilder = | ||
new HighlightBuilder() | ||
// Don't set tags to get the original field value | ||
.preTags("") | ||
.postTags("") | ||
.numOfFragments(1); | ||
|
||
final Stream<String> fieldStream; | ||
if (fieldsToHighlight == null || fieldsToHighlight.isEmpty()) { | ||
fieldStream = getDefaultQueryFieldNames().stream(); | ||
} else { | ||
// filter for valid names | ||
fieldStream = | ||
fieldsToHighlight.stream() | ||
.filter(Objects::nonNull) | ||
.filter(fieldName -> !fieldName.isEmpty()); | ||
} | ||
|
||
fieldStream | ||
.flatMap(fieldName -> highlightFieldExpansion(opContext, fieldName)) | ||
.distinct() | ||
.map(HighlightBuilder.Field::new) | ||
.map( | ||
field -> { | ||
if (field.name().endsWith("ngram")) { | ||
return field.requireFieldMatch(false).noMatchSize(200); | ||
} else { | ||
return field; | ||
} | ||
}) | ||
.forEach(highlightBuilder::field); | ||
|
||
return highlightBuilder; | ||
} | ||
} |
Oops, something went wrong.