use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.
the class Grouping method addFieldCommand.
/**
* Adds a field command based on the specified field.
* If the field is not compatible with {@link CommandField} it invokes the
* {@link #addFunctionCommand(String, org.apache.solr.request.SolrQueryRequest)} method.
*
* @param field The fieldname to group by.
*/
public void addFieldCommand(String field, SolrQueryRequest request) throws SyntaxError {
// Throws an exception when field doesn't exist. Bad request.
SchemaField schemaField = searcher.getSchema().getField(field);
FieldType fieldType = schemaField.getType();
ValueSource valueSource = fieldType.getValueSource(schemaField, null);
if (!(valueSource instanceof StrFieldSource)) {
addFunctionCommand(field, request);
return;
}
Grouping.CommandField gc = new CommandField();
gc.withinGroupSort = withinGroupSort;
gc.groupBy = field;
gc.key = field;
gc.numGroups = limitDefault;
gc.docsPerGroup = docsPerGroupDefault;
gc.groupOffset = groupOffsetDefault;
gc.offset = cmd.getOffset();
gc.groupSort = groupSort;
gc.format = defaultFormat;
gc.totalCount = defaultTotalCount;
if (main) {
gc.main = true;
gc.format = Grouping.Format.simple;
}
if (gc.format == Grouping.Format.simple) {
// doesn't make sense
gc.groupOffset = 0;
}
commands.add(gc);
}
use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.
the class TestValueSource method getMultiValueSources.
private static MVResult getMultiValueSources(List<ValueSource> sources) {
MVResult mvr = new MVResult();
if (sources.size() % 2 != 0) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal number of sources. There must be an even number of sources");
}
if (sources.size() == 2) {
//check to see if these are MultiValueSource
boolean s1MV = sources.get(0) instanceof MultiValueSource;
boolean s2MV = sources.get(1) instanceof MultiValueSource;
if (s1MV && s2MV) {
mvr.mv1 = (MultiValueSource) sources.get(0);
mvr.mv2 = (MultiValueSource) sources.get(1);
} else if (s1MV || s2MV) {
//if one is a MultiValueSource, than the other one needs to be too.
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal number of sources. There must be an even number of sources");
} else {
mvr.mv1 = new VectorValueSource(Collections.singletonList(sources.get(0)));
mvr.mv2 = new VectorValueSource(Collections.singletonList(sources.get(1)));
}
} else {
int dim = sources.size() / 2;
List<ValueSource> sources1 = new ArrayList<>(dim);
List<ValueSource> sources2 = new ArrayList<>(dim);
//Get dim value sources for the first vector
splitSources(dim, sources, sources1, sources2);
mvr.mv1 = new VectorValueSource(sources1);
mvr.mv2 = new VectorValueSource(sources2);
}
return mvr;
}
use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.
the class FunctionQParser method parseAgg.
/** @lucene.experimental */
public AggValueSource parseAgg(int flags) throws SyntaxError {
String id = sp.getId();
AggValueSource vs = null;
boolean hasParen = false;
if ("agg".equals(id)) {
hasParen = sp.opt("(");
vs = parseAgg(flags | FLAG_IS_AGG);
} else {
// parse as an aggregation...
if (!id.startsWith("agg_")) {
id = "agg_" + id;
}
hasParen = sp.opt("(");
ValueSourceParser argParser = req.getCore().getValueSourceParser(id);
argParser = req.getCore().getValueSourceParser(id);
if (argParser == null) {
throw new SyntaxError("Unknown aggregation " + id + " in (" + sp + ")");
}
ValueSource vv = argParser.parse(this);
if (!(vv instanceof AggValueSource)) {
if (argParser == null) {
throw new SyntaxError("Expected aggregation from " + id + " but got (" + vv + ") in (" + sp + ")");
}
}
vs = (AggValueSource) vv;
}
if (hasParen) {
sp.expect(")");
}
if ((flags & FLAG_CONSUME_DELIMITER) != 0) {
consumeArgumentDelimiter();
}
return vs;
}
use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.
the class FunctionRangeQParserPlugin method createParser.
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
ValueSource vs;
String funcStr;
@Override
public Query parse() throws SyntaxError {
funcStr = localParams.get(QueryParsing.V, null);
QParser subParser = subQuery(funcStr, FunctionQParserPlugin.NAME);
// the range can be based on the relevancy score of embedded queries.
subParser.setIsFilter(false);
Query funcQ = subParser.getQuery();
if (funcQ instanceof FunctionQuery) {
vs = ((FunctionQuery) funcQ).getValueSource();
} else {
vs = new QueryValueSource(funcQ, 0.0f);
}
String l = localParams.get("l");
String u = localParams.get("u");
boolean includeLower = localParams.getBool("incl", true);
boolean includeUpper = localParams.getBool("incu", true);
// TODO: add a score=val option to allow score to be the value
ValueSourceRangeFilter rf = new ValueSourceRangeFilter(vs, l, u, includeLower, includeUpper);
FunctionRangeQuery frq = new FunctionRangeQuery(rf);
return frq;
}
};
}
use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.
the class ExtendedDismaxQParser method getMultiplicativeBoosts.
/**
* Parses all multiplicative boosts
*/
protected List<ValueSource> getMultiplicativeBoosts() throws SyntaxError {
List<ValueSource> boosts = new ArrayList<>();
if (config.hasMultiplicativeBoosts()) {
for (String boostStr : config.multBoosts) {
if (boostStr == null || boostStr.length() == 0)
continue;
Query boost = subQuery(boostStr, FunctionQParserPlugin.NAME).getQuery();
ValueSource vs;
if (boost instanceof FunctionQuery) {
vs = ((FunctionQuery) boost).getValueSource();
} else {
vs = new QueryValueSource(boost, 1.0f);
}
boosts.add(vs);
}
}
return boosts;
}
Aggregations