use of org.apache.lucene.queries.function.valuesource.IntFieldSource 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.IntFieldSource in project lucene-solr by apache.
the class TestValueSources method testScale.
public void testScale() throws Exception {
ValueSource vs = new ScaleFloatFunction(new IntFieldSource("int"), 0, 1);
assertHits(new FunctionQuery(vs), new float[] { 0.0f, 1.0f });
assertAllExist(vs);
vs = new ScaleFloatFunction(BOGUS_INT_VS, 0, 1);
assertNoneExist(vs);
}
use of org.apache.lucene.queries.function.valuesource.IntFieldSource 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.IntFieldSource in project lucene-solr by apache.
the class TestValueSources method testInt.
public void testInt() throws Exception {
ValueSource vs = new IntFieldSource("int");
assertHits(new FunctionQuery(vs), new float[] { 35f, 54f });
assertAllExist(vs);
assertNoneExist(BOGUS_INT_VS);
}
use of org.apache.lucene.queries.function.valuesource.IntFieldSource in project lucene-solr by apache.
the class TestFunctionQuerySort method testSearchAfterWhenSortingByFunctionValues.
public void testSearchAfterWhenSortingByFunctionValues() throws IOException {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig(null);
// depends on docid order
iwc.setMergePolicy(newLogMergePolicy());
RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
Document doc = new Document();
Field field = new StoredField("value", 0);
Field dvField = new NumericDocValuesField("value", 0);
doc.add(field);
doc.add(dvField);
// Save docs unsorted (decreasing value n, n-1, ...)
final int NUM_VALS = 5;
for (int val = NUM_VALS; val > 0; val--) {
field.setIntValue(val);
dvField.setLongValue(val);
writer.addDocument(doc);
}
// Open index
IndexReader reader = writer.getReader();
writer.close();
IndexSearcher searcher = newSearcher(reader);
// Trivial ValueSource function that bypasses single field ValueSource sort optimization
ValueSource src = new SumFloatFunction(new ValueSource[] { new IntFieldSource("value"), new DoubleConstValueSource(1.0D) });
// ...and make it a sort criterion
SortField sf = src.getSortField(false).rewrite(searcher);
Sort orderBy = new Sort(sf);
// Get hits sorted by our FunctionValues (ascending values)
Query q = new MatchAllDocsQuery();
TopDocs hits = searcher.search(q, reader.maxDoc(), orderBy);
assertEquals(NUM_VALS, hits.scoreDocs.length);
// Verify that sorting works in general
int i = 0;
for (ScoreDoc hit : hits.scoreDocs) {
int valueFromDoc = Integer.parseInt(reader.document(hit.doc).get("value"));
assertEquals(++i, valueFromDoc);
}
// Now get hits after hit #2 using IS.searchAfter()
int afterIdx = 1;
FieldDoc afterHit = (FieldDoc) hits.scoreDocs[afterIdx];
hits = searcher.searchAfter(afterHit, q, reader.maxDoc(), orderBy);
// Expected # of hits: NUM_VALS - 2
assertEquals(NUM_VALS - (afterIdx + 1), hits.scoreDocs.length);
// Verify that hits are actually "after"
int afterValue = ((Double) afterHit.fields[0]).intValue();
for (ScoreDoc hit : hits.scoreDocs) {
int val = Integer.parseInt(reader.document(hit.doc).get("value"));
assertTrue(afterValue <= val);
assertFalse(hit.doc == afterHit.doc);
}
reader.close();
dir.close();
}
Aggregations