use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.
the class StatsCollectorSupplierFactory method buildFieldSource.
/**
* Builds a value source for a given field, making sure that the field fits a given source type.
* @param schema the schema
* @param expressionString The name of the field to build a Field Source from.
* @param sourceType FIELD_TYPE for any type of field, NUMBER_TYPE for numeric fields,
* DATE_TYPE for date fields and STRING_TYPE for string fields.
* @return a value source
*/
private static ValueSource buildFieldSource(IndexSchema schema, String expressionString, int sourceType) {
SchemaField sf;
try {
sf = schema.getField(expressionString);
} catch (SolrException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The field " + expressionString + " does not exist.", e);
}
FieldType type = sf.getType();
if (type instanceof TrieIntField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new IntFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieLongField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new LongFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieFloatField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new FloatFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieDoubleField) {
if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new DoubleFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof TrieDateField) {
if (sourceType != DATE_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new DateFieldSource(expressionString) {
public String description() {
return field;
}
};
} else if (type instanceof StrField) {
if (sourceType != STRING_TYPE && sourceType != FIELD_TYPE) {
return null;
}
return new BytesRefFieldSource(expressionString) {
public String description() {
return field;
}
};
}
throw new SolrException(ErrorCode.BAD_REQUEST, type.toString() + " is not a supported field type in Solr Analytics.");
}
use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.
the class TestFunctionQuerySort method testOptimizedFieldSourceFunctionSorting.
public void testOptimizedFieldSourceFunctionSorting() throws IOException {
// index contents don't matter for this test.
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig(null);
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
IndexReader reader = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(reader);
final boolean reverse = random().nextBoolean();
ValueSource vs;
SortField sf, vssf;
vs = new IntFieldSource("int_field");
sf = new SortField("int_field", Type.INT, reverse);
vssf = vs.getSortField(reverse);
assertEquals(sf, vssf);
sf = sf.rewrite(searcher);
vssf = vssf.rewrite(searcher);
assertEquals(sf, vssf);
vs = new FloatFieldSource("float_field");
sf = new SortField("float_field", Type.FLOAT, reverse);
vssf = vs.getSortField(reverse);
assertEquals(sf, vssf);
sf = sf.rewrite(searcher);
vssf = vssf.rewrite(searcher);
assertEquals(sf, vssf);
vs = new DoubleFieldSource("double_field");
sf = new SortField("double_field", Type.DOUBLE, reverse);
vssf = vs.getSortField(reverse);
assertEquals(sf, vssf);
sf = sf.rewrite(searcher);
vssf = vssf.rewrite(searcher);
assertEquals(sf, vssf);
vs = new LongFieldSource("long_field");
sf = new SortField("long_field", Type.LONG, reverse);
vssf = vs.getSortField(reverse);
assertEquals(sf, vssf);
sf = sf.rewrite(searcher);
vssf = vssf.rewrite(searcher);
assertEquals(sf, vssf);
reader.close();
dir.close();
}
use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.
the class TestValueSources method testDouble.
public void testDouble() throws Exception {
ValueSource vs = new DoubleFieldSource("double");
assertHits(new FunctionQuery(vs), new float[] { 3.63f, 5.65f });
assertAllExist(vs);
assertNoneExist(BOGUS_DOUBLE_VS);
}
use of org.apache.lucene.queries.function.valuesource.DoubleFieldSource in project lucene-solr by apache.
the class StatsCollectorSupplierFactory method buildFilterSource.
/**
* Builds a default is missing source that wraps a given source. A missing value is required for all
* non-field value sources.
* @param schema the schema
* @param expressionString The name of the field to build a Field Source from.
* @param sourceType FIELD_TYPE for any type of field, NUMBER_TYPE for numeric fields,
* DATE_TYPE for date fields and STRING_TYPE for string fields.
* @return a value source
*/
@SuppressWarnings("deprecation")
private static ValueSource buildFilterSource(IndexSchema schema, String expressionString, int sourceType) {
String[] arguments = ExpressionFactory.getArguments(expressionString);
if (arguments.length != 2) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid arguments were given for \"" + AnalyticsParams.FILTER + "\".");
}
ValueSource delegateSource = buildSourceTree(schema, arguments[0], sourceType);
if (delegateSource == null) {
return null;
}
Object defaultObject;
ValueSource src = delegateSource;
if (delegateSource instanceof FilterFieldSource) {
src = ((FilterFieldSource) delegateSource).getRootSource();
}
if (src instanceof IntFieldSource) {
try {
defaultObject = new Integer(arguments[1]);
} catch (NumberFormatException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into an integer.", e);
}
} else if (src instanceof DateFieldSource || src instanceof MultiDateFunction) {
defaultObject = DateMathParser.parseMath(null, arguments[1]);
} else if (src instanceof LongFieldSource) {
try {
defaultObject = new Long(arguments[1]);
} catch (NumberFormatException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into a long.", e);
}
} else if (src instanceof FloatFieldSource) {
try {
defaultObject = new Float(arguments[1]);
} catch (NumberFormatException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into a float.", e);
}
} else if (src instanceof DoubleFieldSource || src instanceof SingleDoubleFunction || src instanceof DualDoubleFunction || src instanceof MultiDoubleFunction) {
try {
defaultObject = new Double(arguments[1]);
} catch (NumberFormatException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "The filter value " + arguments[1] + " cannot be converted into a double.", e);
}
} else {
defaultObject = arguments[1];
}
return new FilterFieldSource(delegateSource, defaultObject);
}
Aggregations