Author: gsingers
Date: Tue Nov 17 15:07:24 2009
New Revision: 881319
URL: http://svn.apache.org/viewvc?rev=881319&view=rev
Log:
SOLR-1569: allow literal strings in functions
Added:
lucene/solr/trunk/src/java/org/apache/solr/search/function/LiteralValueSource.java
(with props)
lucene/solr/trunk/src/test/org/apache/solr/search/FunctionQParserTest.java
(with props)
Modified:
lucene/solr/trunk/CHANGES.txt
lucene/solr/trunk/src/java/org/apache/solr/search/FunctionQParser.java
Modified: lucene/solr/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?rev=881319&r1=881318&r2=881319&view=diff
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Tue Nov 17 15:07:24 2009
@@ -43,6 +43,8 @@
Bug Fixes
----------------------
+1. SOLR-1569: Allow functions to take in literal strings by modifying the
FunctionQParser and adding LiteeralValueSource (gsingers)
+
Other Changes
----------------------
Modified: lucene/solr/trunk/src/java/org/apache/solr/search/FunctionQParser.java
URL:
http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/search/FunctionQParser.java?rev=881319&r1=881318&r2=881319&view=diff
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/search/FunctionQParser.java
(original)
+++ lucene/solr/trunk/src/java/org/apache/solr/search/FunctionQParser.java Tue
Nov 17 15:07:24 2009
@@ -233,8 +233,11 @@
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.
Added:
lucene/solr/trunk/src/java/org/apache/solr/search/function/LiteralValueSource.java
URL:
http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/search/function/LiteralValueSource.java?rev=881319&view=auto
==============================================================================
---
lucene/solr/trunk/src/java/org/apache/solr/search/function/LiteralValueSource.java
(added)
+++
lucene/solr/trunk/src/java/org/apache/solr/search/function/LiteralValueSource.java
Tue Nov 17 15:07:24 2009
@@ -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();
+ }
+}
Propchange:
lucene/solr/trunk/src/java/org/apache/solr/search/function/LiteralValueSource.java
------------------------------------------------------------------------------
svn:eol-style = native
Added:
lucene/solr/trunk/src/test/org/apache/solr/search/FunctionQParserTest.java
URL:
http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/search/FunctionQParserTest.java?rev=881319&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/search/FunctionQParserTest.java
(added)
+++ lucene/solr/trunk/src/test/org/apache/solr/search/FunctionQParserTest.java
Tue Nov 17 15:07:24 2009
@@ -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
+ }
+
+}
Propchange:
lucene/solr/trunk/src/test/org/apache/solr/search/FunctionQParserTest.java
------------------------------------------------------------------------------
svn:eol-style = native