-
Notifications
You must be signed in to change notification settings - Fork 3k
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 (#12558)
- Loading branch information
1 parent
65376ee
commit 52f71dd
Showing
8 changed files
with
173 additions
and
132 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
69 changes: 69 additions & 0 deletions
69
...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,69 @@ | ||
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 Collection<String> getValidQueryFieldNames(); | ||
|
||
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()) | ||
.filter(getValidQueryFieldNames()::contains); | ||
} | ||
|
||
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.