You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ds...@apache.org on 2022/09/12 17:22:13 UTC

[solr] branch branch_9x updated: SOLR-16369: Don't use XPath for parsing elevate.xml (#1002)

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

dsmiley 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 08ac7f61943 SOLR-16369: Don't use XPath for parsing elevate.xml (#1002)
08ac7f61943 is described below

commit 08ac7f619437b580fa0c67e94c131a0e4efca236
Author: Haythem <ha...@yahoo.fr>
AuthorDate: Mon Sep 12 19:16:19 2022 +0200

    SOLR-16369: Don't use XPath for parsing elevate.xml (#1002)
    
    Co-authored-by: David Smiley <ds...@salesforce.com>
---
 solr/CHANGES.txt                                   |  2 ++
 .../handler/component/QueryElevationComponent.java | 37 ++++++++--------------
 2 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8a75eaeec69..c6ccd0327a0 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -162,6 +162,8 @@ Other Changes
 
 * SOLR-16296: XmlConfigFile and QueryElevationComponent now use SafeXMLParsing. (Haythem Khiri)
 
+* SOLR-16369: Avoid XPath in parsing elevate.xml (Haythem Khiri)
+
 * SOLR-16402: Bump google-cloud-bom to 0.178.0 (Kevin Risden)
 
 Build
diff --git a/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java b/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java
index 711dd4d0cb8..48b975a63d4 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java
@@ -49,10 +49,6 @@ import java.util.Queue;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.function.Consumer;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.TokenStream;
 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
@@ -95,6 +91,7 @@ import org.apache.solr.util.plugin.SolrCoreAware;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
@@ -420,28 +417,20 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
           SolrException.ErrorCode.BAD_REQUEST, "Root element must be <elevate>");
     }
     Map<ElevatingQuery, ElevationBuilder> elevationBuilderMap = new LinkedHashMap<>();
-    NodeList nodes = doc.getDocumentElement().getElementsByTagName("query");
-    XPath xpath = XPathFactory.newInstance().newXPath();
-    for (int i = 0; i < nodes.getLength(); i++) {
-      Node node = nodes.item(i);
-      String queryString = DOMUtil.getAttr(node, "text", "missing query 'text'");
-      String matchString = DOMUtil.getAttr(node, "match");
-      ElevatingQuery elevatingQuery =
-          new ElevatingQuery(queryString, isSubsetMatchPolicy(matchString));
-      NodeList children;
-      try {
-        children = (NodeList) xpath.evaluate("doc", node, XPathConstants.NODESET);
-      } catch (XPathExpressionException e) {
-        throw new SolrException(
-            SolrException.ErrorCode.SERVER_ERROR, "query requires '<doc .../>' child");
-      }
-
-      if (children.getLength() == 0) { // weird
+    NodeList queryNodes = doc.getDocumentElement().getElementsByTagName("query");
+    for (int i = 0; i < queryNodes.getLength(); i++) {
+      var queryNode = (Element) queryNodes.item(i);
+      String queryString = DOMUtil.getAttr(queryNode, "text", "missing query 'text'");
+      String matchString = DOMUtil.getAttr(queryNode, "match");
+      var elevatingQuery = new ElevatingQuery(queryString, isSubsetMatchPolicy(matchString));
+
+      NodeList docNodes = queryNode.getElementsByTagName("doc");
+      if (docNodes.getLength() == 0) { // weird
         continue;
       }
-      ElevationBuilder elevationBuilder = new ElevationBuilder();
-      for (int j = 0; j < children.getLength(); j++) {
-        Node child = children.item(j);
+      var elevationBuilder = new ElevationBuilder();
+      for (int j = 0; j < docNodes.getLength(); j++) {
+        Node child = docNodes.item(j);
         String id = DOMUtil.getAttr(child, "id", "missing 'id'");
         String e = DOMUtil.getAttr(child, EXCLUDE, null);
         if (e != null) {