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

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

Introduction

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

Prototype

String FACET_PIVOT

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

Click Source Link

Document

Comma separated list of fields to pivot example: author,type (for types by author / types within author)

Usage

From source file:de.uni_tuebingen.ub.ixTheo.handler.component.FacetPrefixSortComponent.java

License:Apache License

/**
 * Actually run the query//w  w  w . ja  v a 2  s. c o m
 */
@Override
public void process(ResponseBuilder rb) throws IOException {
    if (rb.doFacets) {
        final ModifiableSolrParams params = new ModifiableSolrParams();
        final SolrParams origParams = rb.req.getParams();
        final Iterator<String> iter = origParams.getParameterNamesIterator();
        setCollator(origParams.get("lang"));
        while (iter.hasNext()) {
            final String paramName = iter.next();
            // Deduplicate the list with LinkedHashSet, but _only_ for facet
            // params.
            if (!paramName.startsWith(FacetParams.FACET)) {
                params.add(paramName, origParams.getParams(paramName));
                continue;
            }
            final HashSet<String> deDupe = new LinkedHashSet<>(Arrays.asList(origParams.getParams(paramName)));
            params.add(paramName, deDupe.toArray(new String[deDupe.size()]));
        }

        final SimplePrefixSortFacets facets = new SimplePrefixSortFacets(rb.req, rb.getResults().docSet, params,
                rb);
        final NamedList<Object> counts = org.apache.solr.handler.component.FacetComponent
                .getFacetCounts(facets);

        final String[] pivots = params.getParams(FacetParams.FACET_PIVOT);
        if (pivots != null && pivots.length > 0) {
            PivotFacetProcessor pivotProcessor = new PivotFacetProcessor(rb.req, rb.getResults().docSet, params,
                    rb);
            SimpleOrderedMap<List<NamedList<Object>>> v = pivotProcessor.process(pivots);
            if (v != null) {
                counts.add(PIVOT_KEY, v);
            }
        }

        // Check whether we have to reorder out results
        // according to prefix

        final String sort = params.get(FacetParams.FACET_SORT);
        if (FacetPrefixSortParams.FACET_SORT_PREFIX.equals(sort)) {

            // Determine a score relative to the original query

            // Determine the query and make it compatible with our metric
            // class
            // by splitting the single terms
            String[] queryTerms = params.getParams(CommonParams.Q);
            final Collection<String> queryTermsCollection = new ArrayList<>();
            for (String s : queryTerms) {
                // Split at whitespace except we have a quoted term
                Matcher matcher = WHITE_SPACES_WITH_QUOTES_SPLITTING_PATTERN.matcher(s);
                while (matcher.find()) {
                    queryTermsCollection.add(matcher.group().replaceAll("^\"|\"$", ""));
                }
            }

            // In some contexts, i.e. in KWC that are derived from ordinary
            // keywords or if
            // wildcards occur, also add all the query terms as a single
            // phrase term
            // with stripped wildcards
            StringBuilder sb = new StringBuilder();
            for (String s : queryTermsCollection) {
                s = s.replace("*", "");
                sb.append(s);
                sb.append(" ");
            }

            queryTermsCollection.add(sb.toString().trim());

            final ArrayList<String> queryList = new ArrayList<>(queryTermsCollection);
            final String facetfield = params.get(FacetParams.FACET_FIELD);

            // Get the current facet entry and make it compatible with our
            // metric class
            // "facet_fields" itself contains a NamedList with the
            // facet.field as key

            final NamedList<Object> facetFieldsNamedList = (NamedList<Object>) counts.get("facet_fields");
            final NamedList<Object> facetFields = (NamedList<Object>) facetFieldsNamedList.get(facetfield);

            final List<Entry<Entry<String, Object>, Double>> facetPrefixListScored = new ArrayList<>();
            for (final Entry<String, Object> entry : facetFields) {
                final String facetTerms = entry.getKey();

                // Split up each KWC and calculate the scoring

                ArrayList<String> facetList = new ArrayList<>(
                        Arrays.asList(facetTerms.split("(?<!" + Pattern.quote("\\") + ")/")));

                // For usability reasons sort the result facets according to
                // the order of the search
                facetList = KeywordSort.sortToReferenceChain(queryList, facetList);

                final double score = KeywordChainMetric.calculateSimilarityScore(queryList, facetList);

                // Collect the result in a sorted list and throw away
                // garbage
                if (score > 0) {
                    String facetTermsSorted = StringUtils.join(facetList, "/");
                    Map.Entry<String, Object> sortedEntry = new AbstractMap.SimpleEntry<>(facetTermsSorted,
                            entry.getValue());
                    facetPrefixListScored.add(new AbstractMap.SimpleEntry<>(sortedEntry, score));
                }
            }

            Collections.sort(facetPrefixListScored, ENTRY_COMPARATOR);

            // Extract all the values wrap it back to NamedList again and
            // replace in the original structure

            facetFieldsNamedList.clear();
            NamedList<Object> facetNamedListSorted = new NamedList<>();

            // We had to disable all limits and offsets sort according
            // Handle this accordingly now

            int offset = (params.getInt(FacetParams.FACET_OFFSET) != null)
                    ? params.getInt(FacetParams.FACET_OFFSET)
                    : 0;
            int limit = (params.getInt(FacetParams.FACET_LIMIT) != null)
                    ? params.getInt(FacetParams.FACET_LIMIT)
                    : 100;

            // Strip uneeded elements
            int s = facetPrefixListScored.size();
            int off = (offset < s) ? offset : 0;
            limit = (limit < 0) ? s : limit; // Handle a negative limit
            // param, i.e. unlimited results
            int lim = (offset + limit <= s) ? (offset + limit) : s;

            final List<Entry<Entry<String, Object>, Double>> facetPrefixListScoredTruncated = facetPrefixListScored
                    .subList(off, lim);

            for (Entry<Entry<String, Object>, Double> e : facetPrefixListScoredTruncated) {
                facetNamedListSorted.add(e.getKey().getKey(), e.getKey().getValue());
            }

            facetFieldsNamedList.add(facetfield, facetNamedListSorted);
            NamedList<Object> countList = new NamedList<>();
            countList.add("count", facetPrefixListScored.size());
            facetFieldsNamedList.add(facetfield + "-count", countList);

            counts.remove("facet_fields");
            counts.add("facet_fields", facetFieldsNamedList);
        }

        rb.rsp.add("facet_counts", counts);
    }
}

From source file:org.springframework.data.solr.core.DefaultQueryParserTests.java

License:Apache License

private void assertPivotFactingPresent(SolrQuery solrQuery, String... expected) {
    Assert.assertArrayEquals(expected, solrQuery.getParams(FacetParams.FACET_PIVOT));
}