You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by cp...@apache.org on 2022/03/30 09:44:30 UTC

[solr] branch branch_9x updated: SOLR-16120: optimise hl.fl expansion (#767)

This is an automated email from the ASF dual-hosted git repository.

cpoerschke pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 5c188b5  SOLR-16120: optimise hl.fl expansion (#767)
5c188b5 is described below

commit 5c188b57efb732bc72ce0b99d1798ed7a8116c0c
Author: Christine Poerschke <cp...@apache.org>
AuthorDate: Wed Mar 30 10:30:26 2022 +0100

    SOLR-16120: optimise hl.fl expansion (#767)
    
    (cherry picked from commit 29bd686ee0c3c13194805f43cb58038c2c9c5fc7)
---
 solr/CHANGES.txt                                   |  2 +-
 .../org/apache/solr/highlight/SolrHighlighter.java | 43 ++++++++++++----------
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8a8c610..8339492 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -20,7 +20,7 @@ Improvements
 
 Optimizations
 ---------------------
-(No changes)
+SOLR-16120: Optimise hl.fl expansion. (Christine Poerschke, David Smiley, Mike Drob)
 
 Bug Fixes
 ---------------------
diff --git a/solr/core/src/java/org/apache/solr/highlight/SolrHighlighter.java b/solr/core/src/java/org/apache/solr/highlight/SolrHighlighter.java
index 3e67fe3..ff5ca98 100644
--- a/solr/core/src/java/org/apache/solr/highlight/SolrHighlighter.java
+++ b/solr/core/src/java/org/apache/solr/highlight/SolrHighlighter.java
@@ -20,6 +20,8 @@ import java.io.IOException;
 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;
@@ -69,14 +71,9 @@ public abstract class SolrHighlighter {
         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
@@ -91,21 +88,29 @@ public abstract class SolrHighlighter {
     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[] {});
   }
 
   /**