use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class UninvertDocValuesMergePolicyTest method implUpdateSchemaField.
private static void implUpdateSchemaField(TestHarness h, String fieldName, IntUnaryOperator propertiesModifier) {
try (SolrCore core = h.getCoreInc()) {
// Add docvalues to the field type
IndexSchema schema = core.getLatestSchema();
SchemaField oldSchemaField = schema.getField(fieldName);
SchemaField newSchemaField = new SchemaField(fieldName, oldSchemaField.getType(), propertiesModifier.applyAsInt(oldSchemaField.getProperties()), oldSchemaField.getDefaultValue());
schema.getFields().put(fieldName, newSchemaField);
}
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class TestIndexSearcher method getStringVal.
private String getStringVal(SolrQueryRequest sqr, String field, int doc) throws IOException {
SchemaField sf = sqr.getSchema().getField(field);
ValueSource vs = sf.getType().getValueSource(sf, null);
Map context = ValueSource.newContext(sqr.getSearcher());
vs.createWeight(context, sqr.getSearcher());
IndexReaderContext topReaderContext = sqr.getSearcher().getTopReaderContext();
List<LeafReaderContext> leaves = topReaderContext.leaves();
int idx = ReaderUtil.subIndex(doc, leaves);
LeafReaderContext leaf = leaves.get(idx);
FunctionValues vals = vs.getValues(context, leaf);
return vals.strVal(doc - leaf.docBase);
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class SortSpecParsingTest method testSort.
@Test
public void testSort() throws Exception {
Sort sort;
SortSpec spec;
SolrQueryRequest req = req();
sort = doParseSortSpec("score desc", req).getSort();
//only 1 thing in the list, no Sort specified
assertNull("sort", sort);
spec = doParseSortSpec("score desc", req);
assertNotNull("spec", spec);
assertNull(spec.getSort());
assertNotNull(spec.getSchemaFields());
assertEquals(0, spec.getSchemaFields().size());
// SOLR-4458 - using different case variations of asc and desc
sort = doParseSortSpec("score aSc", req).getSort();
SortField[] flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.SCORE);
assertTrue(flds[0].getReverse());
spec = doParseSortSpec("score aSc", req);
flds = spec.getSort().getSort();
assertEquals(1, flds.length);
assertEquals(flds[0].getType(), SortField.Type.SCORE);
assertTrue(flds[0].getReverse());
assertEquals(1, spec.getSchemaFields().size());
assertNull(spec.getSchemaFields().get(0));
sort = doParseSortSpec("weight dEsC", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
assertEquals(flds[0].getReverse(), true);
spec = doParseSortSpec("weight dEsC", req);
flds = spec.getSort().getSort();
assertEquals(1, flds.length);
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
assertEquals(flds[0].getReverse(), true);
assertEquals(1, spec.getSchemaFields().size());
assertNotNull(spec.getSchemaFields().get(0));
assertEquals("weight", spec.getSchemaFields().get(0).getName());
sort = doParseSortSpec("weight desc,bday ASC", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
assertEquals(flds[0].getReverse(), true);
assertEquals(flds[1].getType(), SortField.Type.LONG);
assertEquals(flds[1].getField(), "bday");
assertEquals(flds[1].getReverse(), false);
//order aliases
sort = doParseSortSpec("weight top,bday asc", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
assertEquals(flds[0].getReverse(), true);
assertEquals(flds[1].getType(), SortField.Type.LONG);
assertEquals(flds[1].getField(), "bday");
assertEquals(flds[1].getReverse(), false);
sort = doParseSortSpec("weight top,bday bottom", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
assertEquals(flds[0].getReverse(), true);
assertEquals(flds[1].getType(), SortField.Type.LONG);
assertEquals(flds[1].getField(), "bday");
assertEquals(flds[1].getReverse(), false);
//test weird spacing
sort = doParseSortSpec("weight DESC, bday asc", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
assertEquals(flds[1].getField(), "bday");
assertEquals(flds[1].getType(), SortField.Type.LONG);
//handles trailing commas
sort = doParseSortSpec("weight desc,", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
//test functions
sort = SortSpecParsing.parseSortSpec("pow(weight, 2) desc", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.REWRITEABLE);
//Not thrilled about the fragility of string matching here, but...
//the value sources get wrapped, so the out field is different than the input
assertEquals(flds[0].getField(), "pow(float(weight),const(2))");
//test functions (more deep)
sort = SortSpecParsing.parseSortSpec("sum(product(r_f1,sum(d_f1,t_f1,1.0)),a_f1) asc", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.REWRITEABLE);
assertEquals(flds[0].getField(), "sum(product(float(r_f1),sum(float(d_f1),float(t_f1),const(1.0))),float(a_f1))");
sort = SortSpecParsing.parseSortSpec("pow(weight, 2.0) desc", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.REWRITEABLE);
//Not thrilled about the fragility of string matching here, but...
//the value sources get wrapped, so the out field is different than the input
assertEquals(flds[0].getField(), "pow(float(weight),const(2.0))");
spec = SortSpecParsing.parseSortSpec("pow(weight, 2.0) desc, weight desc, bday asc", req);
flds = spec.getSort().getSort();
List<SchemaField> schemaFlds = spec.getSchemaFields();
assertEquals(3, flds.length);
assertEquals(3, schemaFlds.size());
assertEquals(flds[0].getType(), SortField.Type.REWRITEABLE);
//Not thrilled about the fragility of string matching here, but...
//the value sources get wrapped, so the out field is different than the input
assertEquals(flds[0].getField(), "pow(float(weight),const(2.0))");
assertNull(schemaFlds.get(0));
assertEquals(flds[1].getType(), SortField.Type.FLOAT);
assertEquals(flds[1].getField(), "weight");
assertNotNull(schemaFlds.get(1));
assertEquals("weight", schemaFlds.get(1).getName());
assertEquals(flds[2].getField(), "bday");
assertEquals(flds[2].getType(), SortField.Type.LONG);
assertNotNull(schemaFlds.get(2));
assertEquals("bday", schemaFlds.get(2).getName());
//handles trailing commas
sort = doParseSortSpec("weight desc,", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.FLOAT);
assertEquals(flds[0].getField(), "weight");
//Test literals in functions
sort = SortSpecParsing.parseSortSpec("strdist(foo_s1, \"junk\", jw) desc", req).getSort();
flds = sort.getSort();
assertEquals(flds[0].getType(), SortField.Type.REWRITEABLE);
//the value sources get wrapped, so the out field is different than the input
assertEquals(flds[0].getField(), "strdist(str(foo_s1),literal(junk), dist=org.apache.lucene.search.spell.JaroWinklerDistance)");
sort = doParseSortSpec("", req).getSort();
assertNull(sort);
spec = doParseSortSpec("", req);
assertNotNull(spec);
assertNull(spec.getSort());
req.close();
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class FieldQParserPlugin method createParser.
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
@Override
public Query parse() {
String field = localParams.get(QueryParsing.F);
String queryText = localParams.get(QueryParsing.V);
SchemaField sf = req.getSchema().getField(field);
FieldType ft = sf.getType();
return ft.getFieldQuery(this, sf, queryText);
}
};
}
use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.
the class FunctionQParser method parseValueSource.
protected ValueSource parseValueSource(int flags) throws SyntaxError {
ValueSource valueSource;
int ch = sp.peek();
if (ch >= '0' && ch <= '9' || ch == '.' || ch == '+' || ch == '-') {
Number num = sp.getNumber();
if (num instanceof Long) {
valueSource = new LongConstValueSource(num.longValue());
} else if (num instanceof Double) {
valueSource = new DoubleConstValueSource(num.doubleValue());
} else {
// shouldn't happen
valueSource = new ConstValueSource(num.floatValue());
}
} else if (ch == '"' || ch == '\'') {
valueSource = new LiteralValueSource(sp.getQuotedString());
} else if (ch == '$') {
sp.pos++;
String param = sp.getId();
String val = getParam(param);
if (val == null) {
throw new SyntaxError("Missing param " + param + " while parsing function '" + sp.val + "'");
}
QParser subParser = subQuery(val, "func");
if (subParser instanceof FunctionQParser) {
((FunctionQParser) subParser).setParseMultipleSources(true);
}
Query subQuery = subParser.getQuery();
if (subQuery instanceof FunctionQuery) {
valueSource = ((FunctionQuery) subQuery).getValueSource();
} else {
valueSource = new QueryValueSource(subQuery, 0.0f);
}
/***
// dereference *simple* argument (i.e., can't currently be a function)
// In the future we could support full function dereferencing via a stack of ValueSource (or StringParser) objects
ch = val.length()==0 ? '\0' : val.charAt(0);
if (ch>='0' && ch<='9' || ch=='.' || ch=='+' || ch=='-') {
StrParser sp = new StrParser(val);
Number num = sp.getNumber();
if (num instanceof Long) {
valueSource = new LongConstValueSource(num.longValue());
} else if (num instanceof Double) {
valueSource = new DoubleConstValueSource(num.doubleValue());
} else {
// shouldn't happen
valueSource = new ConstValueSource(num.floatValue());
}
} else if (ch == '"' || ch == '\'') {
StrParser sp = new StrParser(val);
val = sp.getQuotedString();
valueSource = new LiteralValueSource(val);
} else {
if (val.length()==0) {
valueSource = new LiteralValueSource(val);
} else {
String id = val;
SchemaField f = req.getSchema().getField(id);
valueSource = f.getType().getValueSource(f, this);
}
}
***/
} else {
String id = sp.getId();
if (sp.opt("(")) {
// a function... look it up.
ValueSourceParser argParser = req.getCore().getValueSourceParser(id);
if (argParser == null) {
throw new SyntaxError("Unknown function " + id + " in FunctionQuery(" + sp + ")");
}
valueSource = argParser.parse(this);
sp.expect(")");
} else {
if ("true".equals(id)) {
valueSource = new BoolConstValueSource(true);
} else if ("false".equals(id)) {
valueSource = new BoolConstValueSource(false);
} else {
SchemaField f = req.getSchema().getField(id);
valueSource = f.getType().getValueSource(f, this);
}
}
}
if ((flags & FLAG_CONSUME_DELIMITER) != 0) {
consumeArgumentDelimiter();
}
return valueSource;
}
Aggregations