SOLR-2335: New field(...) function syntax for refering to complex field names (containing whitespace or special characters)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1091516 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2011-04-12 17:40:22 +00:00
parent 904b8a4b77
commit 5e04c0bab9
3 changed files with 45 additions and 3 deletions

View File

@ -121,6 +121,8 @@ New Features
* SOLR-2338: Add support for using <similarity/> in a schema's fieldType, * SOLR-2338: Add support for using <similarity/> in a schema's fieldType,
for customizing scoring on a per-field basis. (hossman, yonik, rmuir) for customizing scoring on a per-field basis. (hossman, yonik, rmuir)
* SOLR-2335: New 'field("...")' function syntax for refering to complex
field names (containing whitespace or special characters) in functions.
Optimizations Optimizations
---------------------- ----------------------

View File

@ -335,6 +335,15 @@ public abstract class ValueSourceParser implements NamedListInitializedPlugin {
return new StringDistanceFunction(str1, str2, dist); return new StringDistanceFunction(str1, str2, dist);
} }
}); });
addParser("field", new ValueSourceParser() {
@Override
public ValueSource parse(FunctionQParser fp) throws ParseException {
String fieldName = fp.parseArg();
SchemaField f = fp.getReq().getSchema().getField(fieldName);
return f.getType().getValueSource(f, fp);
}
});
addParser(new DoubleParser("rad") { addParser(new DoubleParser("rad") {
@Override @Override

View File

@ -71,16 +71,14 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
// replace \0 with the field name and create a parseable string // replace \0 with the field name and create a parseable string
public String func(String field, String template) { public String func(String field, String template) {
StringBuilder sb = new StringBuilder("_val_:\""); StringBuilder sb = new StringBuilder("{!func}");
for (char ch : template.toCharArray()) { for (char ch : template.toCharArray()) {
if (ch=='\0') { if (ch=='\0') {
sb.append(field); sb.append(field);
continue; continue;
} }
if (ch=='"') sb.append('\\');
sb.append(ch); sb.append(ch);
} }
sb.append('"');
return sb.toString(); return sb.toString();
} }
@ -520,5 +518,38 @@ public class TestFunctionQuery extends SolrTestCaseJ4 {
dofunc("atan2(.25,.5)", Math.atan2(.25,.5)); dofunc("atan2(.25,.5)", Math.atan2(.25,.5));
} }
/**
* verify that both the field("...") value source parser as well as
* ExternalFileField work with esoteric field names
*/
@Test
public void testExternalFieldValueSourceParser() {
String field = "CoMpleX \" fieldName _extf";
String fieldAsFunc = "field(\"CoMpleX \\\" fieldName _extf\")";
float[] ids = {100,-4,0,10,25,5,77,23,55,-78,-45,-24,63,78,94,22,34,54321,261,-627};
createIndex(null,ids);
// Unsorted field, largest first
makeExternalFile(field, "54321=543210\n0=-999\n25=250","UTF-8");
// test identity (straight field value)
singleTest(fieldAsFunc, "\0", 54321, 543210, 0,-999, 25,250, 100, 1);
Object orig = FileFloatSource.onlyForTesting;
singleTest(fieldAsFunc, "log(\0)");
// make sure the values were cached
assertTrue(orig == FileFloatSource.onlyForTesting);
singleTest(fieldAsFunc, "sqrt(\0)");
assertTrue(orig == FileFloatSource.onlyForTesting);
makeExternalFile(fieldAsFunc, "0=1","UTF-8");
assertU(adoc("id", "10000")); // will get same reader if no index change
assertU(commit());
singleTest(fieldAsFunc, "sqrt(\0)");
assertTrue(orig != FileFloatSource.onlyForTesting);
purgeFieldCache(FieldCache.DEFAULT); // avoid FC insanity
}
} }