jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tomm...@apache.org
Subject svn commit: r1572546 - in /jackrabbit/oak/trunk/oak-solr-core/src: main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java test/resources/org/apache/jackrabbit/oak/query/native_solr.txt
Date Thu, 27 Feb 2014 13:13:31 GMT
Author: tommaso
Date: Thu Feb 27 13:13:31 2014
New Revision: 1572546

URL: http://svn.apache.org/r1572546
Log:
OAK-1325 - support for request handler aware native solr queries

Modified:
    jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java
    jackrabbit/oak/trunk/oak-solr-core/src/test/resources/org/apache/jackrabbit/oak/query/native_solr.txt

Modified: jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java?rev=1572546&r1=1572545&r2=1572546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java
(original)
+++ jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java
Thu Feb 27 13:13:31 2014
@@ -17,9 +17,7 @@
 package org.apache.jackrabbit.oak.plugins.index.solr.query;
 
 import java.util.Collection;
-
 import javax.annotation.CheckForNull;
-
 import org.apache.jackrabbit.oak.api.PropertyValue;
 import org.apache.jackrabbit.oak.plugins.index.aggregate.NodeAggregator;
 import org.apache.jackrabbit.oak.plugins.index.solr.configuration.OakSolrConfiguration;
@@ -43,14 +41,16 @@ import org.slf4j.LoggerFactory;
  */
 public class SolrQueryIndex implements FulltextQueryIndex {
 
-    public static final String NATIVE_SOLR_QUERY = "native*solr";
-    private final Logger log = LoggerFactory.getLogger(SolrQueryIndex.class);
+    private static final String NATIVE_SOLR_QUERY = "native*solr";
+
     public static final String TYPE = "solr";
 
+    private final Logger log = LoggerFactory.getLogger(SolrQueryIndex.class);
+
     private final String name;
     private final SolrServer solrServer;
     private final OakSolrConfiguration configuration;
-    
+
     private final NodeAggregator aggregator;
 
     public SolrQueryIndex(String name, SolrServer solrServer, OakSolrConfiguration configuration)
{
@@ -101,27 +101,31 @@ public class SolrQueryIndex implements F
 
         StringBuilder queryBuilder = new StringBuilder();
 
-        for (String pt : filter.getPrimaryTypes()) {
-            queryBuilder.append("jcr\\:primaryType").append(':').append(partialEscape(pt));
-        }
-
-        Filter.PathRestriction pathRestriction = filter.getPathRestriction();
-        if (pathRestriction != null) {
-            String path = purgePath(filter);
-            String fieldName = configuration.getFieldForPathRestriction(pathRestriction);
-            if (fieldName != null) {
-                queryBuilder.append(fieldName);
-                queryBuilder.append(':');
-                queryBuilder.append(path);
-                queryBuilder.append(" ");
-            }
-        }
         Collection<Filter.PropertyRestriction> propertyRestrictions = filter.getPropertyRestrictions();
         if (propertyRestrictions != null && !propertyRestrictions.isEmpty()) {
             for (Filter.PropertyRestriction pr : propertyRestrictions) {
                 // native query support
                 if (NATIVE_SOLR_QUERY.equals(pr.propertyName)) {
-                    queryBuilder.append(String.valueOf(pr.first.getValue(pr.first.getType())));
+                    String nativeQueryString = String.valueOf(pr.first.getValue(pr.first.getType()));
+                    if (isHttpRequest(nativeQueryString)) {
+                        // pass through the native HTTP Solr request
+                        String requestHandlerString = nativeQueryString.substring(0, nativeQueryString.indexOf('?'));
+                        if (!"select".equals(requestHandlerString)) {
+                            solrQuery.setRequestHandler(requestHandlerString);
+                        }
+                        String parameterString = nativeQueryString.substring(nativeQueryString.indexOf('?')
+ 1);
+                        for (String param : parameterString.split("&")) {
+                            String[] kv = param.split("=");
+                            if (kv.length != 2) {
+                                throw new RuntimeException("Unparsable native HTTP Solr query");
+                            } else {
+                                solrQuery.setParam(kv[0], kv[1]);
+                            }
+                        }
+                        return solrQuery; // every other restriction is not considered
+                    } else {
+                        queryBuilder.append(nativeQueryString);
+                    }
                 } else {
                     if (pr.propertyName.contains("/")) {
                         // cannot handle child-level property restrictions
@@ -164,6 +168,23 @@ public class SolrQueryIndex implements F
             }
         }
 
+        for (String pt : filter.getPrimaryTypes()) {
+            queryBuilder.append("jcr\\:primaryType").append(':').append(partialEscape(pt));
+        }
+
+
+        Filter.PathRestriction pathRestriction = filter.getPathRestriction();
+        if (pathRestriction != null) {
+            String path = purgePath(filter);
+            String fieldName = configuration.getFieldForPathRestriction(pathRestriction);
+            if (fieldName != null) {
+                queryBuilder.append(fieldName);
+                queryBuilder.append(':');
+                queryBuilder.append(path);
+                queryBuilder.append(" ");
+            }
+        }
+
         Collection<String> fulltextConditions = filter.getFulltextConditions();
         for (String fulltextCondition : fulltextConditions) {
             queryBuilder.append(fulltextCondition).append(" ");
@@ -182,6 +203,10 @@ public class SolrQueryIndex implements F
         return solrQuery;
     }
 
+    private boolean isHttpRequest(String nativeQueryString) {
+        return nativeQueryString.matches("\\w+\\?.*"); // the query string starts with ${handler.selector}?
+    }
+
     private void setDefaults(SolrQuery solrQuery) {
         solrQuery.setParam("q.op", "AND");
 
@@ -289,5 +314,5 @@ public class SolrQueryIndex implements F
     public NodeAggregator getNodeAggregator() {
         return aggregator;
     }
-    
+
 }

Modified: jackrabbit/oak/trunk/oak-solr-core/src/test/resources/org/apache/jackrabbit/oak/query/native_solr.txt
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/test/resources/org/apache/jackrabbit/oak/query/native_solr.txt?rev=1572546&r1=1572545&r2=1572546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-solr-core/src/test/resources/org/apache/jackrabbit/oak/query/native_solr.txt
(original)
+++ jackrabbit/oak/trunk/oak-solr-core/src/test/resources/org/apache/jackrabbit/oak/query/native_solr.txt
Thu Feb 27 13:13:31 2014
@@ -35,6 +35,12 @@ select [jcr:path] from [nt:base]
 /test/a
 /test/b
 
+# same as above but streamlined via direct http call to the request handler
+select [jcr:path] from [nt:base]
+  where native('solr', 'select?q=Hello OR World')
+/test/a
+/test/b
+
 select [jcr:path] from [nt:base] as a
   where native(a, 'solr', 'path_child:\/test _val_:"recip(rord(name),1,2,3)"')
 /test/a



Mime
View raw message