Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-1569: allow literal strings in functions

Grant Ingersoll54d763c16296
diff --git a/CHANGES.txt b/CHANGES.txt
index c233324ed2a..f72a0bfefee 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -43,6 +43,8 @@ Optimizations
Bug Fixes
----------------------
+1. SOLR-1569: Allow functions to take in literal strings by modifying the FunctionQParser and adding LiteeralValueSource (gsingers)
+
Other Changes
----------------------
diff --git a/src/java/org/apache/solr/search/FunctionQParser.java b/src/java/org/apache/solr/search/FunctionQParser.java
index e7cf228691d..ac664cd27e6 100755
--- a/src/java/org/apache/solr/search/FunctionQParser.java
+++ b/src/java/org/apache/solr/search/FunctionQParser.java
@@ -233,8 +233,11 @@ public class FunctionQParser extends QParser {
int ch = sp.peek();
if (ch>='0' && ch<='9' || ch=='.' || ch=='+' || ch=='-') {
valueSource = new ConstValueSource(sp.getFloat());
+ } else if (ch == '"' || ch == '\''){
+ valueSource = new LiteralValueSource(sp.getQuotedString());
}
else {
+
String id = sp.getId();
if (sp.opt("(")) {
// a function... look it up.
diff --git a/src/java/org/apache/solr/search/function/LiteralValueSource.java b/src/java/org/apache/solr/search/function/LiteralValueSource.java
new file mode 100644
index 00000000000..a00cd0d70bd
--- /dev/null
+++ b/src/java/org/apache/solr/search/function/LiteralValueSource.java
@@ -0,0 +1,76 @@
+package org.apache.solr.search.function;
+/**
+ * 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.
+ */
+
+import org.apache.lucene.index.IndexReader;
+
+import java.util.Map;
+import java.io.IOException;
+
+
+/**
+ * Pass a the field value through as a String, no matter the type
+ *
+ **/
+public class LiteralValueSource extends ValueSource {
+ protected String string;
+ public LiteralValueSource(String string) {
+ this.string = string;
+ }
+
+ protected String name() {
+ return "literal";
+ }
+
+ @Override
+ public DocValues getValues(Map context, IndexReader reader) throws IOException {
+
+ return new DocValues() {
+ @Override
+ public String strVal(int doc) {
+ return string;
+ }
+
+ @Override
+ public String toString(int doc) {
+ return string;
+ }
+ };
+ }
+
+ public String description() {
+ return "literal(" + string + ")";
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof LiteralValueSource)) return false;
+
+ LiteralValueSource that = (LiteralValueSource) o;
+
+ if (!string.equals(that.string)) return false;
+
+ return true;
+ }
+
+ public static final int hash = LiteralValueSource.class.hashCode();
+ @Override
+ public int hashCode() {
+ return hash + string.hashCode();
+ }
+}
diff --git a/src/test/org/apache/solr/search/FunctionQParserTest.java b/src/test/org/apache/solr/search/FunctionQParserTest.java
new file mode 100644
index 00000000000..f4ed71dce66
--- /dev/null
+++ b/src/test/org/apache/solr/search/FunctionQParserTest.java
@@ -0,0 +1,56 @@
+package org.apache.solr.search;
+
+import org.apache.lucene.search.Query;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.request.LocalSolrQueryRequest;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.search.function.FunctionQuery;
+import org.apache.solr.search.function.LiteralValueSource;
+import org.apache.solr.search.function.ConstValueSource;
+import org.apache.solr.search.function.DocValues;
+import org.apache.solr.util.AbstractSolrTestCase;
+
+import java.util.HashMap;
+
+
+/**
+ *
+ *
+ **/
+public class FunctionQParserTest extends AbstractSolrTestCase {
+ public String getSchemaFile() {
+ return "schema11.xml";
+ }
+
+ public String getSolrConfigFile() {
+ return "solrconfig-functionquery.xml";
+ }
+
+ public String getCoreName() {
+ return "basic";
+ }
+
+
+ public void testFunctionQParser() throws Exception {
+ ModifiableSolrParams local = new ModifiableSolrParams();
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ SolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), "_val_:'foo'", "", 0, 10, new HashMap());
+ FunctionQParser parser;
+ Query query;
+ FunctionQuery fq;
+ parser = new FunctionQParser("'foo'", local, params, req);
+ query = parser.parse();
+ assertTrue("query is not a FunctionQuery", query instanceof FunctionQuery);
+ fq = (FunctionQuery) query;
+ assertTrue("ValueSource is not a LiteralValueSource", fq.getValueSource() instanceof LiteralValueSource);
+
+ parser = new FunctionQParser("1.5", local, params, req);
+ query = parser.parse();
+ assertTrue("query is not a FunctionQuery", query instanceof FunctionQuery);
+ fq = (FunctionQuery) query;
+ assertTrue("ValueSource is not a LiteralValueSource", fq.getValueSource() instanceof ConstValueSource);
+
+ //TODO: Add more tests here to test the parser
+ }
+
+}