Example usage for org.apache.solr.common.params FacetParams FACET_THREADS

List of usage examples for org.apache.solr.common.params FacetParams FACET_THREADS

Introduction

In this page you can find the example usage for org.apache.solr.common.params FacetParams FACET_THREADS.

Prototype

String FACET_THREADS

To view the source code for org.apache.solr.common.params FacetParams FACET_THREADS.

Click Source Link

Document

Numeric option indicating the maximum number of threads to be used in counting facet field vales

Usage

From source file:uk.co.flax.biosolr.HierarchicalFacets.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
public SimpleOrderedMap<NamedList> process(String[] facetTrees) throws IOException {
    if (!rb.doFacets || facetTrees == null || facetTrees.length == 0) {
        return null;
    }/*from  ww w . j  a va2  s  .  c om*/

    int maxThreads = req.getParams().getInt(FacetParams.FACET_THREADS, 0);
    Executor executor = maxThreads == 0 ? directExecutor : facetExecutor;
    final Semaphore semaphore = new Semaphore((maxThreads <= 0) ? Integer.MAX_VALUE : maxThreads);
    List<Future<NamedList>> futures = new ArrayList<>(facetTrees.length);

    SimpleOrderedMap<NamedList> treeResponse = new SimpleOrderedMap<>();
    try {
        FacetTreeBuilderFactory treeBuilderFactory = new FacetTreeBuilderFactory();
        PrunerFactory prunerFactory = new PrunerFactory(parameters);

        for (String fTree : facetTrees) {
            try {
                ParsedParams parsedParams = parseParams(FacetTreeParameters.LOCAL_PARAM_TYPE, fTree);
                SolrParams localParams = parsedParams.localParams;
                FacetTreeBuilder treeBuilder = treeBuilderFactory.constructFacetTreeBuilder(localParams);
                final String localKey = localParams.get(QueryParsing.V);

                final FacetTreeGenerator generator = new FacetTreeGenerator(treeBuilder,
                        localParams.get(FacetTreeParameters.COLLECTION_PARAM, null),
                        prunerFactory.constructPruner(localParams));
                final NamedList<Integer> termCounts = getTermCounts(localKey, parsedParams);
                Callable<NamedList> callable = new Callable<NamedList>() {
                    @Override
                    public NamedList call() throws Exception {
                        try {
                            List<SimpleOrderedMap<Object>> tree = generator.generateTree(rb, termCounts);
                            NamedList<List<SimpleOrderedMap<Object>>> nl = new NamedList<>();
                            nl.add(localKey, tree);
                            return nl;
                        } finally {
                            semaphore.release();
                        }
                    }
                };

                RunnableFuture<NamedList> runnableFuture = new FutureTask<>(callable);
                semaphore.acquire();// may block and/or interrupt
                executor.execute(runnableFuture);// releases semaphore when done
                futures.add(runnableFuture);
            } catch (SyntaxError e) {
                throw new SolrException(ErrorCode.BAD_REQUEST, e);
            }
        }

        // Loop over futures to get the values. The order is the same as
        // facetTrees but shouldn't matter.
        for (Future<NamedList> future : futures) {
            treeResponse.addAll(future.get());
        }
    } catch (InterruptedException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                "Error while processing facet tree fields: InterruptedException", e);
    } catch (ExecutionException ee) {
        Throwable e = ee.getCause();// unwrap
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                "Error while processing facet tree fields: " + e.toString(), e);
    }

    return treeResponse;
}