mirror of https://github.com/apache/lucene.git
SOLR-1569: allow literal strings in functions
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@881319 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
addbda8f67
commit
54d763c162
|
@ -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
|
||||
----------------------
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue