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
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion solr/CHANGES.txt
Expand Up @@ -41,7 +41,7 @@ Improvements

Optimizations
---------------------
(No changes)
SOLR-16120: Optimise hl.fl expansion. (Christine Poerschke, David Smiley, Mike Drob)

Bug Fixes
---------------------
Expand Down
43 changes: 24 additions & 19 deletions solr/core/src/java/org/apache/solr/highlight/SolrHighlighter.java
Expand Up @@ -20,6 +20,8 @@
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Pattern;
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 +71,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 +88,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
Pattern fieldRegex = Pattern.compile(field.replaceAll("\\*", ".*"));
if (availableFieldNames == null) {
availableFieldNames = availableFieldNamesSupplier.get();
}
for (String availableFieldName : availableFieldNames) {
if (fieldRegex.matcher(availableFieldName).matches()) {
expandedFields.add(availableFieldName);
}
}
} else {
expandedFields.add(field);
}
} else {
expandedFields.add(field);
}
}
return expandedFields.toArray(new String[] {});
}

/**
Expand Down