Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-334: nested query type added

Yonik Seeleya73947096351
diff --git a/src/java/org/apache/solr/search/NestedQParserPlugin.java b/src/java/org/apache/solr/search/NestedQParserPlugin.java
new file mode 100755
index 00000000000..cc0fca2825d
--- /dev/null
+++ b/src/java/org/apache/solr/search/NestedQParserPlugin.java
@@ -0,0 +1,70 @@
+/**
+ * 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.solr.search;
+
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.search.Query;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.search.function.BoostedQuery;
+import org.apache.solr.search.function.FunctionQuery;
+import org.apache.solr.search.function.QueryValueSource;
+import org.apache.solr.search.function.ValueSource;
+
+/**
+ * Create a nested query, with the ability of that query to redefine it's type via
+ * local parameters. This is useful in specifying defaults in configuration and
+ * letting clients indirectly reference them.
+ * <br>Example: <code>&lt;!query defType=func v=$q1&gt;</code>
+ * <br> if the q1 parameter is <code>price</code> then the query would be a function query on the price field.
+ * <br> if the q1 parameter is <code>&lt;!lucene&gt;inStock:true</code> then a term query is
+ * created from the lucene syntax string that matches documents with inStock=true.
+ */
+public class NestedQParserPlugin extends QParserPlugin {
+ public static String NAME = "query";
+
+ public void init(NamedList args) {
+ }
+
+ public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
+ return new QParser(qstr, localParams, params, req) {
+ QParser baseParser;
+ ValueSource vs;
+ String b;
+
+ public Query parse() throws ParseException {
+ baseParser = subQuery(localParams.get(QueryParsing.V), null);
+ return baseParser.getQuery();
+ }
+
+ public String[] getDefaultHighlightFields() {
+ return baseParser.getDefaultHighlightFields();
+ }
+
+ public Query getHighlightQuery() throws ParseException {
+ return baseParser.getHighlightQuery();
+ }
+
+ public void addDebugInfo(NamedList<Object> debugInfo) {
+ // encapsulate base debug info in a sub-list?
+ baseParser.addDebugInfo(debugInfo);
+ }
+ };
+ }
+
+}
\ No newline at end of file
diff --git a/src/java/org/apache/solr/search/QParserPlugin.java b/src/java/org/apache/solr/search/QParserPlugin.java
index 9cae5cda2ac..59ee23c9530 100755
--- a/src/java/org/apache/solr/search/QParserPlugin.java
+++ b/src/java/org/apache/solr/search/QParserPlugin.java
@@ -34,6 +34,7 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin {
DisMaxQParserPlugin.NAME, DisMaxQParserPlugin.class,
FieldQParserPlugin.NAME, FieldQParserPlugin.class,
RawQParserPlugin.NAME, RawQParserPlugin.class,
+ NestedQParserPlugin.NAME, NestedQParserPlugin.class,
};
/** return a {@link QParser} */
diff --git a/src/test/org/apache/solr/search/TestQueryTypes.java b/src/test/org/apache/solr/search/TestQueryTypes.java
index 8203ca96163..6745f8a37bd 100755
--- a/src/test/org/apache/solr/search/TestQueryTypes.java
+++ b/src/test/org/apache/solr/search/TestQueryTypes.java
@@ -182,5 +182,15 @@ public class TestQueryTypes extends AbstractSolrTestCase {
,"//result[@numFound='2']"
);
+ assertQ("test nested query",
+ req("q","_query_:\"<!query v=$q1>\"", "q1","<!prefix f=v_t>hel")
+ ,"//result[@numFound='2']"
+ );
+
+ assertQ("test nested nested query",
+ req("q","_query_:\"<!query defType=query v=$q1>\"", "q1","<!v=$q2>","q2","<!prefix f=v_t v=$qqq>","qqq","hel")
+ ,"//result[@numFound='2']"
+ );
+
}
}
\ No newline at end of file