use of org.apache.solr.analytics.request.QueryFacetRequest in project lucene-solr by apache.
the class FacetingAccumulator method processQueryFacets.
/**
* Initiates the collecting of query facets
* @param filter the base filter to work against
* @throws IOException if searching failed
*/
public void processQueryFacets(final Filter filter) throws IOException {
for (QueryFacetRequest qfr : queryFacets) {
for (String query : qfr.getQueries()) {
if (query.contains(AnalyticsParams.RESULT) && !query.contains(AnalyticsParams.QUERY_RESULT)) {
try {
String[] pivotStr = ExpressionFactory.getArguments(query.substring(query.indexOf('(') + 1, query.lastIndexOf(')')).trim());
if (pivotStr.length == 1) {
query = getResult(pivotStr[0]);
} else if (pivotStr.length == 3) {
query = getResult(pivotStr[0], pivotStr[1], pivotStr[2]);
} else {
throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " has an invalid amount of arguments.");
}
} catch (IndexOutOfBoundsException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " is invalid. Lacks parentheses.", e);
}
} else if (query.contains(AnalyticsParams.QUERY_RESULT)) {
try {
String[] pivotStr = ExpressionFactory.getArguments(query.substring(query.indexOf('(') + 1, query.lastIndexOf(')')).trim());
if (pivotStr.length == 3) {
query = getQueryResult(qfr.getName(), pivotStr[0], pivotStr[1], pivotStr[2]);
} else {
throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " has an invalid amount of arguments.");
}
} catch (IndexOutOfBoundsException e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Result request " + query + " is invalid. Lacks parentheses.", e);
}
}
QueryFacetAccumulator qAcc = new QueryFacetAccumulator(this, qfr.getName(), query);
final Query q;
try {
q = QParser.getParser(query, queryRequest).getQuery();
} catch (SyntaxError e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid query '" + query + "'", e);
}
// The searcher sends docIds to the QueryFacetAccumulator which forwards
// them to <code>collectQuery()</code> in this class for collection.
Query filtered = new BooleanQuery.Builder().add(q, Occur.MUST).add(filter, Occur.FILTER).build();
searcher.search(filtered, qAcc);
computeQueryFacet(qfr.getName());
queryCount++;
}
}
}
use of org.apache.solr.analytics.request.QueryFacetRequest in project lucene-solr by apache.
the class FacetingAccumulator method export.
@Override
@SuppressWarnings("unchecked")
public NamedList<?> export() {
final NamedList<Object> base = (NamedList<Object>) super.export();
NamedList<NamedList<?>> facetList = new NamedList<>();
// Add the field facet buckets to the output
base.add("fieldFacets", facetList);
for (FieldFacetRequest freq : request.getFieldFacets()) {
final String name = freq.getName();
if (hiddenFieldFacets.contains(name)) {
continue;
}
final Map<String, Expression[]> buckets = fieldFacetExpressions.get(name);
final NamedList<Object> bucketBase = new NamedList<>();
Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
final FieldFacetRequest fr = (FieldFacetRequest) freq;
final FacetSortSpecification sort = fr.getSort();
final int limit = fr.getLimit();
final int offset = fr.getOffset();
final boolean showMissing = fr.showsMissing();
if (!showMissing) {
buckets.remove(MISSING_VALUE);
}
// Sorting the buckets if a sort specification is provided
if (sort != null && buckets.values().iterator().hasNext()) {
int sortPlace = Arrays.binarySearch(expressionNames, sort.getStatistic());
final Expression first = buckets.values().iterator().next()[sortPlace];
final Comparator<Expression> comp = (Comparator<Expression>) first.comparator(sort.getDirection());
final List<Entry<String, Expression[]>> sorted = new ArrayList<>(buckets.size());
Iterables.addAll(sorted, iter);
Collections.sort(sorted, new EntryComparator(comp, sortPlace));
iter = sorted;
}
// apply the limit
if (limit > AnalyticsContentHandler.DEFAULT_FACET_LIMIT) {
if (offset > 0) {
iter = Iterables.skip(iter, offset);
}
iter = Iterables.limit(iter, limit);
}
// Export each expression in the bucket.
for (Entry<String, Expression[]> bucket : iter) {
bucketBase.add(bucket.getKey(), export(bucket.getValue()));
}
facetList.add(name, bucketBase);
}
// Add the range facet buckets to the output
facetList = new NamedList<>();
base.add("rangeFacets", facetList);
for (RangeFacetRequest freq : request.getRangeFacets()) {
final String name = freq.getName();
final Map<String, Expression[]> buckets = rangeFacetExpressions.get(name);
final NamedList<Object> bucketBase = new NamedList<>();
Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
for (Entry<String, Expression[]> bucket : iter) {
bucketBase.add(bucket.getKey(), export(bucket.getValue()));
}
facetList.add(name, bucketBase);
}
// Add the query facet buckets to the output
facetList = new NamedList<>();
base.add("queryFacets", facetList);
for (QueryFacetRequest freq : request.getQueryFacets()) {
final String name = freq.getName();
final Map<String, Expression[]> buckets = queryFacetExpressions.get(name);
final NamedList<Object> bucketBase = new NamedList<>();
Iterable<Entry<String, Expression[]>> iter = buckets.entrySet();
for (Entry<String, Expression[]> bucket : iter) {
bucketBase.add(bucket.getKey(), export(bucket.getValue()));
}
facetList.add(name, bucketBase);
}
return base;
}
Aggregations