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

SOLR-16120: optimise hl.fl expansion #767

Merged
merged 6 commits into from Mar 30, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 23 additions & 19 deletions solr/core/src/java/org/apache/solr/highlight/SolrHighlighter.java
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.HighlightParams;
Expand Down Expand Up @@ -69,14 +70,9 @@ public String[] getHighlightFields(
fields = defaultFields;
}
} else {
Set<String> expandedFields = new LinkedHashSet<String>();
Collection<String> storedHighlightFieldNames =
request.getSearcher().getDocFetcher().getStoredHighlightFieldNames();
for (String field : fields) {
expandWildcardsInHighlightFields(
expandedFields, storedHighlightFieldNames, SolrPluginUtils.split(field));
}
fields = expandedFields.toArray(new String[] {});
fields =
expandWildcardsInFields(
() -> request.getSearcher().getDocFetcher().getStoredHighlightFieldNames(), fields);
}

// Trim them now in case they haven't been yet. Not needed for all code-paths above but do it
Expand All @@ -91,21 +87,29 @@ protected boolean emptyArray(String[] arr) {
return (arr == null || arr.length == 0 || arr[0] == null || arr[0].trim().length() == 0);
}

private static void expandWildcardsInHighlightFields(
Set<String> expandedFields, Collection<String> storedHighlightFieldNames, String... fields) {
for (String field : fields) {
if (field.contains("*")) {
// create a Java regular expression from the wildcard string
String fieldRegex = field.replaceAll("\\*", ".*");
for (String storedFieldName : storedHighlightFieldNames) {
if (storedFieldName.matches(fieldRegex)) {
expandedFields.add(storedFieldName);
private static String[] expandWildcardsInFields(
Supplier<Collection<String>> availableFieldNamesSupplier, String... inFields) {
Set<String> expandedFields = new LinkedHashSet<String>();
Collection<String> availableFieldNames = null;
for (String inField : inFields) {
for (String field : SolrPluginUtils.split(inField)) {
if (field.contains("*")) {
// create a Java regular expression from the wildcard string
String fieldRegex = field.replaceAll("\\*", ".*");
dsmiley marked this conversation as resolved.
Show resolved Hide resolved
if (availableFieldNames == null) {
availableFieldNames = availableFieldNamesSupplier.get();
}
for (String availableFieldName : availableFieldNames) {
if (availableFieldName.matches(fieldRegex)) {
expandedFields.add(availableFieldName);
}
}
} else {
expandedFields.add(field);
}
} else {
expandedFields.add(field);
}
}
return expandedFields.toArray(new String[] {});
}

/**
Expand Down