use of org.apache.lucene.queries.function.valuesource.FloatFieldSource 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.FloatFieldSource 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.FloatFieldSource in project lucene-solr by apache.
the class TestValueSources method testFloat.
public void testFloat() throws Exception {
ValueSource vs = new FloatFieldSource("float");
assertHits(new FunctionQuery(vs), new float[] { 5.2f, 9.3f });
assertAllExist(vs);
assertNoneExist(BOGUS_FLOAT_VS);
}
use of org.apache.lucene.queries.function.valuesource.FloatFieldSource in project ddf by codice.
the class GeoNamesQueryLuceneIndex method createQuery.
protected Query createQuery(final String queryString) throws ParseException {
final StandardAnalyzer standardAnalyzer = new StandardAnalyzer();
final QueryParser nameQueryParser = new QueryParser(GeoNamesLuceneConstants.NAME_FIELD, standardAnalyzer);
nameQueryParser.setEnablePositionIncrements(false);
/* For the name, we construct a query searching for exactly the query string (the phrase
query), a query searching for all the terms in the query string (the AND query), and a
query searching for any of the terms in the query string (the OR query). We take the
maximum of the scores generated by these three queries and use that as the score for the
name. */
// Surround with quotes so Lucene looks for the words in the query as a phrase.
// Phrase query gets the biggest boost - 3.2 was obtained after some experimentation.
final Query phraseNameQuery = new BoostQuery(nameQueryParser.parse("\"" + queryString + "\""), 3.2f);
// By default, QueryParser uses OR to separate terms.
// We give OR queries the lowest boost because they're not as good as phrase matches or
// AND matches - 1 (the default boost value) was obtained after some experimentation.
final Query orNameQuery = nameQueryParser.parse(queryString);
nameQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
// We give AND queries the second-biggest boost because they're better than OR matches but
// not as good as phrase matches - 2 was obtained after some experimentation.
final Query andNameQuery = new BoostQuery(nameQueryParser.parse(queryString), 2f);
final List<Query> nameQueryList = Arrays.asList(phraseNameQuery, orNameQuery, andNameQuery);
// This query will score each document by the maximum of the three sub-queries.
final Query nameQuery = new DisjunctionMaxQuery(nameQueryList, 0);
final QueryParser alternateNamesQueryParser = new QueryParser(GeoNamesLuceneConstants.ALTERNATE_NAMES_FIELD, standardAnalyzer);
// For the alternate names, we perform an AND query and an OR query, both of which are
// boosted less than the name query because the alternate names are generally not as
// important.
// The OR query gets a lower boost - 0.5 was obtained after some experimentation.
final Query orAlternateNamesQuery = new BoostQuery(alternateNamesQueryParser.parse(queryString), 0.5f);
alternateNamesQueryParser.setDefaultOperator(QueryParser.AND_OPERATOR);
// The AND query gets a higher boost - 1 (the default boost value) was obtained after some
// experimentation.
final Query andAlternateNamesQuery = alternateNamesQueryParser.parse(queryString);
final List<Query> alternateNamesQueryList = Arrays.asList(orAlternateNamesQuery, andAlternateNamesQuery);
// This query will score each document by the maximum of the two sub-queries.
final Query alternateNamesQuery = new DisjunctionMaxQuery(alternateNamesQueryList, 0);
final List<Query> queryList = Arrays.asList(nameQuery, alternateNamesQuery);
// This query will score each document by the sum of the two sub-queries, since both the
// name and the alternate names are important.
// The boost values ensure that how well the query matches the name has a bigger impact on
// the final score than how well it matches the alternate names.
final DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(queryList, 1.0f);
// This is the boost we calculated at index time, and it is applied in the CustomScoreQuery.
final FunctionQuery boostQuery = new FunctionQuery(new FloatFieldSource(GeoNamesLuceneConstants.BOOST_FIELD));
return new CustomScoreQuery(disjunctionMaxQuery, boostQuery);
}
use of org.apache.lucene.queries.function.valuesource.FloatFieldSource in project lucene-solr by apache.
the class TestValueSources method testRangeMap.
public void testRangeMap() throws Exception {
assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), 5, 6, 1, 0f)), new float[] { 1f, 0f });
assertHits(new FunctionQuery(new RangeMapFloatFunction(new FloatFieldSource("float"), 5, 6, new SumFloatFunction(new ValueSource[] { new ConstValueSource(1f), new ConstValueSource(2f) }), new ConstValueSource(11f))), new float[] { 3f, 11f });
// TODO: what *should* the rules be for exist() ?
// ((source exists && source in range && target exists) OR (source not in range && default exists)) ?
}
Aggregations