use of org.apache.lucene.queries.function.valuesource.LongFieldSource 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.LongFieldSource 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.LongFieldSource in project lucene-solr by apache.
the class TestTaxonomyFacetSumValueSource method testWithScore.
public void testWithScore() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
FacetsConfig config = new FacetsConfig();
for (int i = 0; i < 4; i++) {
Document doc = new Document();
doc.add(new NumericDocValuesField("price", (i + 1)));
doc.add(new FacetField("a", Integer.toString(i % 2)));
iw.addDocument(config.build(taxoWriter, doc));
}
DirectoryReader r = DirectoryReader.open(iw);
DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
FacetsCollector fc = new FacetsCollector(true);
// score documents by their 'price' field - makes asserting the correct counts for the categories easier
Query q = new FunctionQuery(new LongFieldSource("price"));
FacetsCollector.search(newSearcher(r), q, 10, fc);
Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
assertEquals("dim=a path=[] value=10.0 childCount=2\n 1 (6.0)\n 0 (4.0)\n", facets.getTopChildren(10, "a").toString());
iw.close();
IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
use of org.apache.lucene.queries.function.valuesource.LongFieldSource in project lucene-solr by apache.
the class TestDocValuesFieldSources method test.
@SuppressWarnings("fallthrough")
public void test(DocValuesType type) throws IOException {
Directory d = newDirectory();
IndexWriterConfig iwConfig = newIndexWriterConfig(new MockAnalyzer(random()));
final int nDocs = atLeast(50);
final Field id = new NumericDocValuesField("id", 0);
final Field f;
switch(type) {
case BINARY:
f = new BinaryDocValuesField("dv", new BytesRef());
break;
case SORTED:
f = new SortedDocValuesField("dv", new BytesRef());
break;
case NUMERIC:
f = new NumericDocValuesField("dv", 0);
break;
case SORTED_NUMERIC:
f = new SortedNumericDocValuesField("dv", 0);
break;
default:
throw new AssertionError();
}
Document document = new Document();
document.add(id);
document.add(f);
final Object[] vals = new Object[nDocs];
RandomIndexWriter iw = new RandomIndexWriter(random(), d, iwConfig);
for (int i = 0; i < nDocs; ++i) {
id.setLongValue(i);
switch(type) {
case SORTED:
case BINARY:
do {
vals[i] = TestUtil.randomSimpleString(random(), 20);
} while (((String) vals[i]).isEmpty());
f.setBytesValue(new BytesRef((String) vals[i]));
break;
case NUMERIC:
case SORTED_NUMERIC:
// keep it an int
final int bitsPerValue = RandomNumbers.randomIntBetween(random(), 1, 31);
vals[i] = (long) random().nextInt((int) PackedInts.maxValue(bitsPerValue));
f.setLongValue((Long) vals[i]);
break;
}
iw.addDocument(document);
if (random().nextBoolean() && i % 10 == 9) {
iw.commit();
}
}
iw.close();
DirectoryReader rd = DirectoryReader.open(d);
for (LeafReaderContext leave : rd.leaves()) {
final FunctionValues ids = new LongFieldSource("id").getValues(null, leave);
final ValueSource vs;
switch(type) {
case BINARY:
case SORTED:
vs = new BytesRefFieldSource("dv");
break;
case NUMERIC:
vs = new LongFieldSource("dv");
break;
case SORTED_NUMERIC:
// Since we are not indexing multiple values, MIN and MAX should work the same way
vs = random().nextBoolean() ? new MultiValuedLongFieldSource("dv", Type.MIN) : new MultiValuedLongFieldSource("dv", Type.MAX);
break;
default:
throw new AssertionError();
}
final FunctionValues values = vs.getValues(null, leave);
BytesRefBuilder bytes = new BytesRefBuilder();
for (int i = 0; i < leave.reader().maxDoc(); ++i) {
assertTrue(values.exists(i));
if (vs instanceof BytesRefFieldSource) {
assertTrue(values.objectVal(i) instanceof String);
} else if (vs instanceof LongFieldSource) {
assertTrue(values.objectVal(i) instanceof Long);
assertTrue(values.bytesVal(i, bytes));
} else {
throw new AssertionError();
}
Object expected = vals[ids.intVal(i)];
switch(type) {
case SORTED:
// no exception
values.ordVal(i);
assertTrue(values.numOrd() >= 1);
// fall-through
case BINARY:
assertEquals(expected, values.objectVal(i));
assertEquals(expected, values.strVal(i));
assertEquals(expected, values.objectVal(i));
assertEquals(expected, values.strVal(i));
assertTrue(values.bytesVal(i, bytes));
assertEquals(new BytesRef((String) expected), bytes.get());
break;
case NUMERIC:
case SORTED_NUMERIC:
assertEquals(((Number) expected).longValue(), values.longVal(i));
break;
}
}
}
rd.close();
d.close();
}
use of org.apache.lucene.queries.function.valuesource.LongFieldSource in project lucene-solr by apache.
the class TestValueSources method testLong.
public void testLong() throws Exception {
ValueSource vs = new LongFieldSource("long");
assertHits(new FunctionQuery(vs), new float[] { 4343f, 1954f });
assertAllExist(vs);
assertNoneExist(BOGUS_LONG_VS);
}
Aggregations