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

fix(search): implement queryByDefault annotation for SearchableRef #10603

Merged
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 @@ -115,6 +115,7 @@ private void extractSearchableRefAnnotation(
new SearchableRefAnnotation(
pathName,
annotation.getFieldType(),
annotation.isQueryByDefault(),
annotation.getBoostScore(),
annotation.getDepth(),
annotation.getRefType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class SearchableRefAnnotation {
String fieldName;
// Type of the field. Defines how the field is indexed and matched
SearchableAnnotation.FieldType fieldType;
// Whether we should match the field for the default search query
boolean queryByDefault;
// Boost multiplier to the match score. Matches on fields with higher boost score ranks higher
double boostScore;
// defines what depth should be explored of reference object
Expand Down Expand Up @@ -72,20 +74,35 @@ public static SearchableRefAnnotation fromPegasusAnnotationObject(
+ "Mandatory input field refType defining the Entity Type is not provided",
ANNOTATION_NAME, context));
}
final Optional<Boolean> queryByDefault =
AnnotationUtils.getField(map, "queryByDefault", Boolean.class);
final Optional<Integer> depth = AnnotationUtils.getField(map, "depth", Integer.class);
final Optional<Double> boostScore = AnnotationUtils.getField(map, "boostScore", Double.class);
final List<String> fieldNameAliases = getFieldNameAliases(map);
final SearchableAnnotation.FieldType resolvedFieldType =
getFieldType(fieldType, schemaDataType);

return new SearchableRefAnnotation(
fieldName.orElse(schemaFieldName),
resolvedFieldType,
getQueryByDefault(queryByDefault, resolvedFieldType),
boostScore.orElse(1.0),
depth.orElse(2),
refType.get(),
fieldNameAliases);
}

private static Boolean getQueryByDefault(
Optional<Boolean> maybeQueryByDefault, SearchableAnnotation.FieldType fieldType) {
if (!maybeQueryByDefault.isPresent()) {
if (DEFAULT_QUERY_FIELD_TYPES.contains(fieldType)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
return maybeQueryByDefault.get();
}

private static SearchableAnnotation.FieldType getFieldType(
Optional<String> maybeFieldType, DataSchema.Type schemaDataType) {
if (!maybeFieldType.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ public Set<SearchFieldConfig> getStandardFields(
return fields;
}

/**
* Return query by default fields
*
* @param entityRegistry entity registry with search annotations
* @param entitySpec the entity spect
* @return set of queryByDefault field configurations
*/
@VisibleForTesting
public Set<SearchFieldConfig> getFieldsFromEntitySpec(
@Nonnull EntityRegistry entityRegistry, EntitySpec entitySpec) {
Expand Down Expand Up @@ -212,14 +219,20 @@ public Set<SearchFieldConfig> getFieldsFromEntitySpec(

List<SearchableRefFieldSpec> searchableRefFieldSpecs = entitySpec.getSearchableRefFieldSpecs();
for (SearchableRefFieldSpec refFieldSpec : searchableRefFieldSpecs) {
if (!refFieldSpec.getSearchableRefAnnotation().isQueryByDefault()) {
continue;
}

int depth = refFieldSpec.getSearchableRefAnnotation().getDepth();
Set<SearchFieldConfig> searchFieldConfig =
SearchFieldConfig.detectSubFieldType(refFieldSpec, depth, entityRegistry);
fields.addAll(searchFieldConfig);
Set<SearchFieldConfig> searchFieldConfigs =
SearchFieldConfig.detectSubFieldType(refFieldSpec, depth, entityRegistry).stream()
.filter(SearchFieldConfig::isQueryByDefault)
.collect(Collectors.toSet());
fields.addAll(searchFieldConfigs);

Map<String, SearchableAnnotation.FieldType> fieldTypeMap =
getAllFieldTypeFromSearchableRef(refFieldSpec, depth, entityRegistry, "");
for (SearchFieldConfig fieldConfig : searchFieldConfig) {
for (SearchFieldConfig fieldConfig : searchFieldConfigs) {
if (fieldConfig.hasDelimitedSubfield()) {
fields.add(
SearchFieldConfig.detectSubFieldType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,16 @@ public void testGetStandardFields() {
.map(SearchFieldConfig::boost),
Optional.of(2.0F));
}

@Test
public void testStandardFieldsQueryByDefault() {
assertTrue(
TEST_BUILDER
.getStandardFields(
opContext.getEntityRegistry(),
opContext.getEntityRegistry().getEntitySpecs().values())
.stream()
.allMatch(SearchFieldConfig::isQueryByDefault),
"Expect all search fields to be queryByDefault.");
}
}
Loading