You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by to...@apache.org on 2014/04/01 12:41:44 UTC

svn commit: r1583600 - in /jackrabbit/oak/trunk/oak-solr-core/src: main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java

Author: tommaso
Date: Tue Apr  1 10:41:44 2014
New Revision: 1583600

URL: http://svn.apache.org/r1583600
Log:
OAK-1636 - support for jcr:score in Solr indexer

Added:
    jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/query/SolrQueryIndex.java

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=1583600&r1=1583599&r2=1583600&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 Tue Apr  1 10:41:44 2014
@@ -18,10 +18,12 @@ package org.apache.jackrabbit.oak.plugin
 
 import java.util.Collection;
 import javax.annotation.CheckForNull;
+
 import org.apache.jackrabbit.JcrConstants;
 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;
+import org.apache.jackrabbit.oak.query.QueryImpl;
 import org.apache.jackrabbit.oak.query.fulltext.FullTextAnd;
 import org.apache.jackrabbit.oak.query.fulltext.FullTextExpression;
 import org.apache.jackrabbit.oak.query.fulltext.FullTextOr;
@@ -295,6 +297,7 @@ public class SolrQueryIndex implements F
 
     private void setDefaults(SolrQuery solrQuery) {
         solrQuery.setParam("q.op", "AND");
+        solrQuery.setParam("fl", "* score");
         String catchAllField = configuration.getCatchAllField();
         if (catchAllField != null && catchAllField.length() > 0) {
             solrQuery.setParam("df", catchAllField);
@@ -387,6 +390,14 @@ public class SolrQueryIndex implements F
 
                     @Override
                     public PropertyValue getValue(String columnName) {
+                        if (QueryImpl.JCR_SCORE.equals(columnName)) {
+                            float score = 0f;
+                            Object scoreObj = doc.get("score");
+                            if (scoreObj != null) {
+                                score = (Float) scoreObj;
+                            }
+                            return PropertyValues.newDouble((double) score);
+                        }
                         Object o = doc.getFieldValue(columnName);
                         return o == null ? null : PropertyValues.newString(o.toString());
                     }

Added: jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java?rev=1583600&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java (added)
+++ jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java Tue Apr  1 10:41:44 2014
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.jcr.query;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
+import javax.jcr.query.QueryResult;
+import javax.jcr.query.RowIterator;
+
+import org.apache.jackrabbit.core.query.AbstractQueryTest;
+
+/**
+ * Tests the fulltext index.
+ */
+public class QueryFulltextTest extends AbstractQueryTest {
+
+    public void testScore() throws Exception {
+        Session session = superuser;
+        QueryManager qm = session.getWorkspace().getQueryManager();
+        Node n1 = testRootNode.addNode("node1");
+        n1.setProperty("text", "hello hello hello");
+        Node n2 = testRootNode.addNode("node2");
+        n2.setProperty("text", "hello");
+        session.save();
+
+        String xpath = "/jcr:root//*[jcr:contains(@text, 'hello')] order by jcr:score()";
+        Query q = qm.createQuery(xpath, "xpath");
+        String result = getResult(q.execute(), "jcr:score");
+        // expect two numbers (any value)
+        result = result.replaceAll("[0-9\\.]+", "n");
+        assertEquals("n, n", result);
+    }
+
+    public void testFulltext() throws Exception {
+        Session session = superuser;
+        QueryManager qm = session.getWorkspace().getQueryManager();
+        Node n1 = testRootNode.addNode("node1");
+        n1.setProperty("text", "hello");
+        Node n2 = testRootNode.addNode("node2");
+        n2.setProperty("text", "hallo");
+        Node n3 = testRootNode.addNode("node3");
+        n3.setProperty("text", "hello hallo");
+        session.save();
+
+        // lowercase "or" mean search for the term "or"
+        String sql2 = "select [jcr:path] as [path] from [nt:base] " +
+                "where contains([text], 'hello or hallo') order by [jcr:path]";
+        Query q = qm.createQuery(sql2, Query.JCR_SQL2);
+        assertEquals("", getResult(q.execute(), "path"));
+
+    }
+
+    public void testFulltextRelativeProperty() throws Exception {
+        Session session = superuser;
+        QueryManager qm = session.getWorkspace().getQueryManager();
+        Node n1 = testRootNode.addNode("node1");
+        n1.setProperty("text", "hello");
+        Node n2 = testRootNode.addNode("node2");
+        n2.setProperty("text", "hallo");
+        Node n3 = testRootNode.addNode("node3");
+        n3.setProperty("text", "hello hallo");
+        session.save();
+
+        Query q;
+
+        String sql2 = "select [jcr:path] as [path] from [nt:base] " + 
+                "where ISCHILDNODE([/testroot])" + 
+                " AND CONTAINS(text, 'hallo')";
+
+        q = qm.createQuery(sql2, Query.JCR_SQL2);
+        assertEquals("/testroot/node2, /testroot/node3", getResult(q.execute(), "path"));
+//
+//        sql2 = "select [jcr:path] as [path] from [nt:base] "
+//                + "where contains([node1/text], 'hello') order by [jcr:path]";
+//        q = qm.createQuery(sql2, Query.JCR_SQL2);
+//        assertEquals("/testroot", getResult(q.execute(), "path"));
+//
+//        sql2 = "select [jcr:path] as [path] from [nt:base] "
+//                + "where contains([node2/text], 'hello OR hallo') order by [jcr:path]";
+//        q = qm.createQuery(sql2, Query.JCR_SQL2);
+//        assertEquals("/testroot", getResult(q.execute(), "path"));
+
+        // TODO OAK-890
+        // sql2 = "select [jcr:path] as [path] from [nt:base] "
+        // + "where contains([node1/text], 'hello') "
+        // + "and contains([node2/text], 'hallo') "
+        // + "order by [jcr:path]";
+        // q = qm.createQuery(sql2, Query.JCR_SQL2);
+        // assertEquals("/testroot", getResult(q.execute(), "path"));
+    }
+
+    static String getResult(QueryResult result, String propertyName) throws RepositoryException {
+        StringBuilder buff = new StringBuilder();
+        RowIterator it = result.getRows();
+        while (it.hasNext()) {
+            if (buff.length() > 0) {
+                buff.append(", ");
+            }
+            buff.append(it.nextRow().getValue(propertyName).getString());
+        }
+        return buff.toString();
+    }
+    
+}

Propchange: jackrabbit/oak/trunk/oak-solr-core/src/test/java/org/apache/jackrabbit/oak/jcr/query/QueryFulltextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native