use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.
the class Grouping method addFunctionCommand.
public void addFunctionCommand(String groupByStr, SolrQueryRequest request) throws SyntaxError {
QParser parser = QParser.getParser(groupByStr, "func", request);
Query q = parser.getQuery();
final Grouping.Command gc;
if (q instanceof FunctionQuery) {
ValueSource valueSource = ((FunctionQuery) q).getValueSource();
if (valueSource instanceof StrFieldSource) {
String field = ((StrFieldSource) valueSource).getField();
CommandField commandField = new CommandField();
commandField.groupBy = field;
gc = commandField;
} else {
CommandFunc commandFunc = new CommandFunc();
commandFunc.groupBy = valueSource;
gc = commandFunc;
}
} else {
CommandFunc commandFunc = new CommandFunc();
commandFunc.groupBy = new QueryValueSource(q, 0.0f);
gc = commandFunc;
}
gc.withinGroupSort = withinGroupSort;
gc.key = groupByStr;
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.FunctionQuery in project lucene-solr by apache.
the class SortSpecParsing method parseSortSpecImpl.
private static SortSpec parseSortSpecImpl(String sortSpec, IndexSchema schema, SolrQueryRequest optionalReq) {
if (sortSpec == null || sortSpec.length() == 0)
return newEmptySortSpec();
List<SortField> sorts = new ArrayList<>(4);
List<SchemaField> fields = new ArrayList<>(4);
try {
StrParser sp = new StrParser(sortSpec);
while (sp.pos < sp.end) {
sp.eatws();
final int start = sp.pos;
// short circuit test for a really simple field name
String field = sp.getId(null);
Exception qParserException = null;
if ((field == null || !Character.isWhitespace(sp.peekChar())) && (optionalReq != null)) {
// let's try it as a function instead
field = null;
String funcStr = sp.val.substring(start);
QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, optionalReq);
Query q = null;
try {
if (parser instanceof FunctionQParser) {
FunctionQParser fparser = (FunctionQParser) parser;
fparser.setParseMultipleSources(false);
fparser.setParseToEnd(false);
q = fparser.getQuery();
if (fparser.localParams != null) {
if (fparser.valFollowedParams) {
// need to find the end of the function query via the string parser
int leftOver = fparser.sp.end - fparser.sp.pos;
// reset our parser to the same amount of leftover
sp.pos = sp.end - leftOver;
} else {
// the value was via the "v" param in localParams, so we need to find
// the end of the local params themselves to pick up where we left off
sp.pos = start + fparser.localParamsEnd;
}
} else {
// need to find the end of the function query via the string parser
int leftOver = fparser.sp.end - fparser.sp.pos;
// reset our parser to the same amount of leftover
sp.pos = sp.end - leftOver;
}
} else {
// A QParser that's not for function queries.
// It must have been specified via local params.
q = parser.getQuery();
assert parser.getLocalParams() != null;
sp.pos = start + parser.localParamsEnd;
}
Boolean top = sp.getSortDirection();
if (null != top) {
// we have a Query and a valid direction
if (q instanceof FunctionQuery) {
sorts.add(((FunctionQuery) q).getValueSource().getSortField(top));
} else {
sorts.add((new QueryValueSource(q, 0.0f)).getSortField(top));
}
fields.add(null);
continue;
}
} catch (Exception e) {
// hang onto this in case the string isn't a full field name either
qParserException = e;
}
}
if (field == null) {
// try again, simple rules for a field name with no whitespace
sp.pos = start;
field = sp.getSimpleString();
}
Boolean top = sp.getSortDirection();
if (null == top) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't determine a Sort Order (asc or desc) in sort spec " + sp);
}
if (SCORE.equals(field)) {
if (top) {
sorts.add(SortField.FIELD_SCORE);
} else {
sorts.add(new SortField(null, SortField.Type.SCORE, true));
}
fields.add(null);
} else if (DOCID.equals(field)) {
sorts.add(new SortField(null, SortField.Type.DOC, top));
fields.add(null);
} else {
// try to find the field
SchemaField sf = schema.getFieldOrNull(field);
if (null == sf) {
if (null != qParserException) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param could not be parsed as a query, and is not a " + "field that exists in the index: " + field, qParserException);
}
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param field can't be found: " + field);
}
sorts.add(sf.getSortField(top));
fields.add(sf);
}
}
} catch (SyntaxError e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error in sort: " + sortSpec, e);
}
// normalize a sort on score desc to null
if (sorts.size() == 1 && sorts.get(0) == SortField.FIELD_SCORE) {
return newEmptySortSpec();
}
Sort s = new Sort(sorts.toArray(new SortField[sorts.size()]));
return new SortSpec(s, fields);
}
use of org.apache.lucene.queries.function.FunctionQuery 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.FunctionQuery 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;
}
use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.
the class AbstractSpatialFieldType method getQueryFromSpatialArgs.
protected Query getQueryFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs) {
T strategy = getStrategy(field.getName());
SolrParams localParams = parser.getLocalParams();
//See SOLR-2883 needScore
String scoreParam = (localParams == null ? null : localParams.get(SCORE_PARAM));
//We get the valueSource for the score then the filter and combine them.
ValueSource valueSource = getValueSourceFromSpatialArgs(parser, field, spatialArgs, scoreParam, strategy);
if (valueSource == null) {
//assumed constant scoring
return strategy.makeQuery(spatialArgs);
}
FunctionQuery functionQuery = new FunctionQuery(valueSource);
if (localParams != null && !localParams.getBool(FILTER_PARAM, true))
return functionQuery;
Query filterQuery = strategy.makeQuery(spatialArgs);
return new BooleanQuery.Builder().add(functionQuery, //matches everything and provides score
Occur.MUST).add(filterQuery, //filters (score isn't used)
Occur.FILTER).build();
}
Aggregations