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

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

Introduction

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

Prototype

String FACET_MINCOUNT

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

Click Source Link

Document

Numeric option indicating the minimum number of hits before a facet should be included in the response.

Usage

From source file:at.newmedialab.lmf.util.solr.suggestion.service.SuggestionService.java

License:Apache License

private SolrQueryResponse query(String query, String df, String[] fields, String[] fqs) {

    SolrQueryResponse rsp = new SolrQueryResponse();

    //append *//w  w  w . j  av  a  2s.  co m
    if (!query.endsWith("*")) {
        query = query.trim() + "*";
    }

    //Prepare query
    ModifiableSolrParams params = new ModifiableSolrParams();
    SolrQueryRequest req = new LocalSolrQueryRequest(solrCore, params);
    params.add(CommonParams.Q, query.toLowerCase());
    params.add(CommonParams.DF, df);
    params.add("q.op", "AND");
    params.add(FacetParams.FACET, "true");
    params.add(FacetParams.FACET_LIMIT, internalFacetLimit);
    params.add(FacetParams.FACET_MINCOUNT, "1");
    for (String field : fields) {
        params.add(FacetParams.FACET_FIELD, field);
    }
    if (fqs != null) {
        for (String fq : fqs) {
            params.add(CommonParams.FQ, fq);
        }
    }

    if (spellcheck_enabled) {
        params.add("spellcheck", "true");
        params.add("spellcheck.collate", "true");
    }

    try {
        //execute query and return
        searchHandler.handleRequestBody(req, rsp);
        return rsp;
    } catch (SolrException se) {
        throw se;
    } catch (Exception e) {
        e.printStackTrace();
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "internal server error");
    } finally {
        req.close();
    }
}

From source file:com.tsgrp.solr.handler.NYPhilSearchHandler.java

License:Mozilla Public License

/**
 * @see org.apache.solr.handler.component.SearchHandler#handleRequestBody(org.apache.solr.request.SolrQueryRequest,
 *      org.apache.solr.request.SolrQueryResponse)
 *///from w w w.  java  2  s  .co m
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
        throws Exception, ParseException, InstantiationException, IllegalAccessException {
    SolrParams requestParams = req.getParams();

    NamedList<Object> params = requestParams.toNamedList();

    //for now lets echo the handler and all the params for debugging purposes
    //params.add( CommonParams.HEADER_ECHO_HANDLER, Boolean.TRUE );
    //params.add( CommonParams.HEADER_ECHO_PARAMS, CommonParams.EchoParamStyle.ALL );

    String rows = (String) params.get(CommonParams.ROWS);
    if (rows == null || rows.trim().length() < 1) {
        //setup items per page, default to 10 items
        params.add(CommonParams.ROWS, 10);
        rows = "10";
    }

    //enable faceting and only return facets with at least 1 item
    params.add(FacetParams.FACET, Boolean.TRUE);
    params.add(FacetParams.FACET_MINCOUNT, 1);

    //defult ordering to index order (alphabetical)
    params.add(FacetParams.FACET_SORT, FacetParams.FACET_SORT_INDEX);

    //only return a maximum of 10 facet values
    params.add(FacetParams.FACET_LIMIT, 10);

    //always add facets unless they are explicitly not requested
    String addFacets = (String) params.get(PARAM_GENERATE_FACETS);
    boolean generateFacets = (addFacets == null) ? true : addFacets.equalsIgnoreCase("true");

    //enable highlighting
    params.add(HighlightParams.HIGHLIGHT, Boolean.TRUE);

    //remove the query if provided, always want to use our translated query
    String originalQuery = (String) params.remove(CommonParams.Q);

    if (logger.isDebugEnabled()) {
        logger.debug("Original query: " + originalQuery);
    }

    //setup sorting params
    String sortColumn = (String) params.get(PARAM_SORT_COLUMN);
    String sortOrder = (String) params.get(PARAM_SORT_ORDER);
    if (sortColumn != null && sortOrder != null) {
        params.add(CommonParams.SORT, sortColumn + " " + sortOrder);
    }

    //get date fields
    String sDateFrom = (String) params.get(PARAM_DATE_FROM);
    String sDateTo = (String) params.get(PARAM_DATE_TO);
    Date dateFrom = null;
    Date dateTo = null;
    if (sDateFrom != null && sDateTo != null) {
        dateFrom = DATE_FORMAT_PARAM.parse(sDateFrom);
        dateTo = DATE_FORMAT_PARAM.parse(sDateTo);
    }

    String keywords = (String) params.get(PARAM_KEYWORDS);

    if (keywords == null || keywords.trim().length() < 1) {
        //query everything since nothing was provided
        keywords = "*";
    }

    //always use the extended dismax parser
    params.add("defType", ExtendedDismaxQParserPlugin.NAME);

    //the keywords sent in are the query since we're in DISMAX mode
    params.add(CommonParams.Q, keywords);

    String pageIndex = (String) params.get(PARAM_PAGE_INDEX);
    String resultsPerPage = (String) params.get(PARAM_RESULTS_PER_PAGE);
    if (resultsPerPage == null || resultsPerPage.trim().length() < 0) {
        resultsPerPage = "10";
    }

    if (pageIndex == null || pageIndex.trim().length() < 1) {
        pageIndex = "1";
    }

    //figure out the skip count, use the (pageIndex - 1) * resultsPerPage
    //ie pageIndex = 3, 10 results per page, we'll set start to (3-1)*10 = 20
    int start = (Integer.parseInt(pageIndex) - 1) * Integer.parseInt(resultsPerPage);
    params.add(CommonParams.START, start);

    String doctype = (String) params.get(PARAM_DOCTYPE);
    if (doctype == null || doctype.trim().length() < 1) {
        doctype = "";
    }

    String facetQuery = (String) params.get(PARAM_FACET_QUERY);
    if (facetQuery != null && facetQuery.trim().length() > 0) {
        //facetQuery is pre formatted and correct, just add it as a filter query
        params.add(CommonParams.FQ, facetQuery);
    }

    String suggestedQuery = (String) params.get(PARAM_SUGGESTED_QUERY);
    if (suggestedQuery != null && suggestedQuery.trim().length() > 0) {
        //suggestedQuery is pre formatted and correct, just add it as a filter query
        params.add(CommonParams.FQ, suggestedQuery);
    }

    //base fields
    params.add(DisMaxParams.QF, "nyp:DocumentType");
    params.add(DisMaxParams.QF, "nyp:Notes");

    //program fields
    params.add(DisMaxParams.QF, "npp:ProgramID");
    params.add(DisMaxParams.QF, "npp:Season");
    params.add(DisMaxParams.QF, "npp:OrchestraCode");
    params.add(DisMaxParams.QF, "npp:OrchestraName");
    params.add(DisMaxParams.QF, "npp:LocationName");
    params.add(DisMaxParams.QF, "npp:VenueName");
    params.add(DisMaxParams.QF, "npp:EventTypeName");
    params.add(DisMaxParams.QF, "npp:SubEventName");
    params.add(DisMaxParams.QF, "npp:ConductorName");
    params.add(DisMaxParams.QF, "npp:SoloistsNames");
    params.add(DisMaxParams.QF, "npp:SoloistsInstrumentName");
    params.add(DisMaxParams.QF, "npp:WorksComposerNames");
    params.add(DisMaxParams.QF, "npp:WorksTitle");
    params.add(DisMaxParams.QF, "npp:WorksShortTitle");
    params.add(DisMaxParams.QF, "npp:WorksConductorNames");

    //printedMusic fields
    params.add(DisMaxParams.QF, "npm:LibraryID");
    params.add(DisMaxParams.QF, "npm:ShortTitle");
    params.add(DisMaxParams.QF, "npm:ComposerName");
    params.add(DisMaxParams.QF, "npm:PublisherName");
    params.add(DisMaxParams.QF, "npm:ComposerNameTitle");
    params.add(DisMaxParams.QF, "npm:ScoreMarkingArtist");
    params.add(DisMaxParams.QF, "npm:ScoreEditionTypeDesc");
    params.add(DisMaxParams.QF, "npm:ScoreNotes");

    //part fields
    params.add(DisMaxParams.QF, "npm:PartTypeDesc");
    params.add(DisMaxParams.QF, "npm:PartMarkingArtist");
    params.add(DisMaxParams.QF, "npm:UsedByArtistName");

    //businessRecord fields
    params.add(DisMaxParams.QF, "npb:BoxNumber");
    params.add(DisMaxParams.QF, "npb:RecordGroup");
    params.add(DisMaxParams.QF, "npb:Series");
    params.add(DisMaxParams.QF, "npb:SubSeries");
    params.add(DisMaxParams.QF, "npb:Folder");
    params.add(DisMaxParams.QF, "npb:Names");
    params.add(DisMaxParams.QF, "npb:Subject");
    params.add(DisMaxParams.QF, "npb:Abstract");

    //visual fields
    params.add(DisMaxParams.QF, "npv:ID");
    params.add(DisMaxParams.QF, "npv:BoxNumber");
    params.add(DisMaxParams.QF, "npv:PhilharmonicSource");
    params.add(DisMaxParams.QF, "npv:OutsideSource");
    params.add(DisMaxParams.QF, "npv:Photographer");
    params.add(DisMaxParams.QF, "npv:CopyrightHolder");
    params.add(DisMaxParams.QF, "npv:PlaceOfImage");
    params.add(DisMaxParams.QF, "npv:PersonalNames");
    params.add(DisMaxParams.QF, "npv:CorporateNames");
    params.add(DisMaxParams.QF, "npv:Event");
    params.add(DisMaxParams.QF, "npv:ImageType");
    params.add(DisMaxParams.QF, "npv:LocationName");
    params.add(DisMaxParams.QF, "npv:VenueName");

    //audio fields
    params.add(DisMaxParams.QF, "npa:ProgramID");
    params.add(DisMaxParams.QF, "npa:Location");
    params.add(DisMaxParams.QF, "npa:EventTypeName");
    params.add(DisMaxParams.QF, "npa:ConductorName");
    params.add(DisMaxParams.QF, "npa:SoloistsAndInstruments");
    params.add(DisMaxParams.QF, "npa:ComposerWork");
    params.add(DisMaxParams.QF, "npa:OrchestraName");
    params.add(DisMaxParams.QF, "npa:IntermissionFeature");
    params.add(DisMaxParams.QF, "npa:LocationName");
    params.add(DisMaxParams.QF, "npa:VenueName");
    params.add(DisMaxParams.QF, "npa:SubEventName");
    params.add(DisMaxParams.QF, "npa:URLLocation");
    params.add(DisMaxParams.QF, "npa:IntermissionGuests");
    params.add(DisMaxParams.QF, "npa:Announcer");

    //video fields
    params.add(DisMaxParams.QF, "npx:ProgramID");
    params.add(DisMaxParams.QF, "npx:Location");
    params.add(DisMaxParams.QF, "npx:EventTypeName");
    params.add(DisMaxParams.QF, "npx:ConductorName");
    params.add(DisMaxParams.QF, "npx:SoloistsAndInstruments");
    params.add(DisMaxParams.QF, "npx:ComposerNameWork");
    params.add(DisMaxParams.QF, "npx:OrchestraName");
    params.add(DisMaxParams.QF, "npx:IntermissionFeature");
    params.add(DisMaxParams.QF, "npx:LocationName");
    params.add(DisMaxParams.QF, "npx:VenueName");
    params.add(DisMaxParams.QF, "npx:SubEventName");
    params.add(DisMaxParams.QF, "npx:IntermissionGuests");
    params.add(DisMaxParams.QF, "npx:Announcer");

    params.add(DisMaxParams.QF, "npt:tagged");

    //add in all the restrictions that can be applied to a document type count in the same filter query

    //this includes all DATE range queries
    //if a date query does not exist, the TYPE is queried so all results are shown
    //restriction is applied to business records so only the web publishable items are shown
    StringBuffer filterTypesCountsQuery = new StringBuffer();

    if (dateFrom != null && dateTo != null) {
        //program date query
        filterTypesCountsQuery.append("(npp\\:Date:[" + solrField.toExternal(dateFrom) + " TO "
                + solrField.toExternal(dateTo) + "])");

        //printedMusic (scores) and parts have no date range, so just or in the type so those results come back
        filterTypesCountsQuery.append(" OR (nyp\\:DocumentType:Printed Music)");
        filterTypesCountsQuery.append(" OR (nyp\\:DocumentType:Part)");

        //business record range AND web publishable restriction
        filterTypesCountsQuery
                .append(" OR (nyp\\:WebPublishable:true AND ((npb\\:DateFrom:[" + solrField.toExternal(dateFrom)
                        + " TO *] AND npb\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "])");
        filterTypesCountsQuery.append(" OR (npb\\:DateFrom:[* TO " + solrField.toExternal(dateFrom)
                + "] AND npb\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "] AND npb\\:DateTo:["
                + solrField.toExternal(dateFrom) + " TO *])");
        filterTypesCountsQuery.append(" OR (npb\\:DateFrom:[" + solrField.toExternal(dateFrom)
                + " TO *] AND npb\\:DateTo:[" + solrField.toExternal(dateTo)
                + " TO *] AND npb\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "])))");

        //visual
        filterTypesCountsQuery.append(" OR ((npv\\:DateFrom:[" + solrField.toExternal(dateFrom)
                + " TO *] AND npv\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "])");
        filterTypesCountsQuery.append(" OR (npv\\:DateFrom:[* TO " + solrField.toExternal(dateFrom)
                + "] AND npv\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "] AND npv\\:DateTo:["
                + solrField.toExternal(dateFrom) + " TO *])");
        filterTypesCountsQuery.append(" OR (npv\\:DateFrom:[" + solrField.toExternal(dateFrom)
                + " TO *] AND npv\\:DateTo:[" + solrField.toExternal(dateTo)
                + " TO *] AND npv\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "]))");

        //audio
        filterTypesCountsQuery.append(" OR (npa\\:Date:[" + solrField.toExternal(dateFrom) + " TO "
                + solrField.toExternal(dateTo) + "])");

        //video
        filterTypesCountsQuery.append(" OR (npx\\:Date:[" + solrField.toExternal(dateFrom) + " TO "
                + solrField.toExternal(dateTo) + "])");
    } else {
        //no date queries, just perform query based on type restrictions            
        filterTypesCountsQuery.append(
                "(nyp\\:DocumentType:Program) OR (nyp\\:DocumentType:Printed Music)  OR (nyp\\:DocumentType:Part) OR (nyp\\:DocumentType:Business Record AND nyp\\:WebPublishable:true)");
        filterTypesCountsQuery.append(
                " OR (nyp\\:DocumentType:Visual) OR (nyp\\:DocumentType:Audio) OR (nyp\\:DocumentType:Video)");
    }

    params.add(CommonParams.FQ, filterTypesCountsQuery);

    //default facet for document type that will always return the counts regardless of filter queries applied
    params.add(FacetParams.FACET_FIELD, "{!ex=test}nyp:DocumentType_facet");
    //allow this facet to always display all values, even when there are 0 results for the type
    params.add("f.nyp:DocumentType_facet.facet.mincount", 0);

    if (doctype.equalsIgnoreCase("program")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Program");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npp:ConductorName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:SoloistsNames_facet");
            params.add(FacetParams.FACET_FIELD, "npp:WorksComposerNames_facet");
            params.add(FacetParams.FACET_FIELD, "npp:LocationName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:VenueName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:EventTypeName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:Season_facet");
        }
    } else if (doctype.equalsIgnoreCase("printedMusic")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Printed Music");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npm:ScoreMarkingArtist_facet");
            params.add(FacetParams.FACET_FIELD, "npm:ComposerName_facet");
        }

    } else if (doctype.equalsIgnoreCase("part")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Part");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npm:ComposerName_facet");
            params.add(FacetParams.FACET_FIELD, "npm:UsedByArtistName_facet");
            params.add(FacetParams.FACET_FIELD, "npm:PartMarkingArtist_facet");
            params.add(FacetParams.FACET_FIELD, "npm:PartTypeDesc_facet");
        }

        // if sort parameter has been supplied (e.g. non-facet search) - apply additional part sorting
        if (sortColumn != null && sortOrder != null) {
            logger.debug("Addition additional sort parameter for PART type...");
            String prevParams = (String) params.get(CommonParams.SORT);
            String newParams = prevParams + ", npm:PartID ASC";
            params.add(CommonParams.SORT, newParams);
        }

    } else if (doctype.equalsIgnoreCase("businessRecord")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Business Record");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npb:Names_facet");
            params.add(FacetParams.FACET_FIELD, "npb:Subject_facet");
            params.add(FacetParams.FACET_FIELD, "npb:RecordGroup_facet");
            params.add(FacetParams.FACET_FIELD, "npb:Series_facet");
            params.add(FacetParams.FACET_FIELD, "npb:SubSeries_facet");
        }

    } else if (doctype.equalsIgnoreCase("visual")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Visual");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npv:Photographer_facet");
            params.add(FacetParams.FACET_FIELD, "npv:CopyrightHolder_facet");
            params.add(FacetParams.FACET_FIELD, "npv:ImageType_facet");
            params.add(FacetParams.FACET_FIELD, "npv:PlaceOfImage_facet");
            params.add(FacetParams.FACET_FIELD, "npv:Event_facet");
            params.add(FacetParams.FACET_FIELD, "npv:PersonalNames_facet");
            params.add(FacetParams.FACET_FIELD, "npv:LocationName_facet");
            params.add(FacetParams.FACET_FIELD, "npv:VenueName_facet");
        }

    } else if (doctype.equalsIgnoreCase("audio")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Audio");

        //no facets being generated
    } else if (doctype.equalsIgnoreCase("video")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Video");

        //no facets being generated
    } else {
        logger.error("Invalid document type: " + doctype);
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid document type: " + doctype);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Params: " + params);
    }

    //set the new request parameters, then call the default handler behavior
    req.setParams(SolrParams.toSolrParams(params));

    super.handleRequestBody(req, rsp);
}

From source file:com.tsgrp.solr.handler.NYPhilTagAutoCompleteHandler.java

License:Mozilla Public License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse res)
        throws Exception, ParseException, InstantiationException, IllegalAccessException {
    NamedList<Object> params = req.getParams().toNamedList();

    params.add(CommonParams.ROWS, 0);//from   w  w w. jav a  2 s . co m
    params.add(FacetParams.FACET, true);
    params.add(FacetParams.FACET_FIELD, NYPhilSolrConstants.NPT_CONTENT_FACET);
    params.add(FacetParams.FACET_MINCOUNT, 1);
    params.add(FacetParams.FACET_SORT, FacetParams.FACET_SORT_INDEX);
    params.add(CommonParams.HEADER_ECHO_PARAMS, "explicit");
    params.add(CommonParams.WT, "json");
    params.add("json.nl", "map");

    String query = (String) params.get(PARAM_VALUE);
    if (query == null || query.length() == 0) {
        query = "*";
    } else {
        query = QueryParser.escape(query.toLowerCase());
    }

    String[] queryTerms = query.split(" ");

    // wrap our query term in parentheses to allow searching on terms separated by whitespace
    StringBuffer q = new StringBuffer();

    // for each query term, require the term with a trailing wildcard match
    for (String queryTerm : queryTerms) {

        // remove all non-alphanumeric chars from the query term
        queryTerm = QUERY_TERM_REGEX.matcher(queryTerm.toLowerCase()).replaceAll("");
        q.append("+").append(NYPhilSolrConstants.NPT_CONTENT_ESC).append(":")
                .append(QueryParser.escape(queryTerm)).append("* ");
    }
    q.append("+").append(NYPhilSolrConstants.NPT_STATUS_ESC).append(":")
            .append(NYPhilSolrConstants.STATUS_APPROVED);

    params.add(CommonParams.Q, q.toString());

    if (logger.isDebugEnabled()) {
        logger.debug("Autocomplete Query: " + q.toString());
    }

    String cb = (String) params.get(PARAM_CALLBACK);
    if (cb != null && cb.length() > 0) {
        params.add("json.wrf", cb);
    }

    req.setParams(SolrParams.toSolrParams(params));

    super.handleRequestBody(req, res);
}

From source file:de.hybris.platform.solrfacetsearch.search.FacetParamsTest.java

License:Open Source License

@Test
public void testFacetMinCountParam() throws Exception {
    query.setLanguage("de");
    query.searchInField("manufacturerName", "EIZO");
    query.setCatalogVersion(hwOnline);/*from www. j a v  a 2  s . c  om*/
    query.addSolrParams(FacetParams.FACET_MINCOUNT, "0");
    final Map<String, String[]> params = query.getSolrParams();
    final String[] strParams = params.get(FacetParams.FACET_MINCOUNT);
    Assert.assertEquals(1, strParams.length);
    Assert.assertEquals("0", strParams[0]);
    final SearchResult result = facetSearchService.search(query);
    final Facet facet = result.getFacet("manufacturerName");
    final FacetValue HP = facet.getFacetValue("Hewlett-Packard");
    Assert.assertNotNull(HP);
    Assert.assertEquals(0, HP.getCount());
}

From source file:fi.nationallibrary.ndl.solr.request.RangeFieldFacets.java

License:Apache License

private <T extends Comparable<T>> NamedList getFacetRangeCounts(final SchemaField sf,
        final RangeEndpointCalculator<T> calc) throws IOException {

    final String f = sf.getName();
    final NamedList res = new SimpleOrderedMap();
    final NamedList counts = new NamedList();
    res.add("counts", counts);

    final T start = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_START));

    // not final, hardend may change this
    T end = calc.getValue(required.getFieldParam(f, FacetParams.FACET_RANGE_END));
    if (end.compareTo(start) < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "range facet 'end' comes before 'start': " + end + " < " + start);
    }/* w ww .j  a  va  2  s  . c  o m*/

    final String gap = required.getFieldParam(f, FacetParams.FACET_RANGE_GAP);
    String[] gaps = parseGaps(gap);
    // explicitly return the gap.  compute this early so we are more 
    // likely to catch parse errors before attempting math
    for (int i = 0; i < gaps.length; i++) {
        calc.getGap(gaps[i]);
    }
    res.add("gap", gap);

    final int minCount = params.getFieldInt(f, FacetParams.FACET_MINCOUNT, 0);

    final EnumSet<FacetRangeInclude> include = FacetRangeInclude
            .parseParam(params.getFieldParams(f, FacetParams.FACET_RANGE_INCLUDE));

    T low = start;
    int gapIdx = 0;
    int previousCount = 0;

    while (low.compareTo(end) < 0) {
        T high = calc.addGap(low, gaps[gapIdx]);
        if (end.compareTo(high) < 0) {
            if (params.getFieldBool(f, FacetParams.FACET_RANGE_HARD_END, false)) {
                high = end;
            } else {
                end = high;
            }
        }
        if (high.compareTo(low) < 0) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "range facet infinite loop (is gap negative? did the math overflow?)");
        }

        final boolean includeLower = (include.contains(FacetRangeInclude.LOWER)
                || (include.contains(FacetRangeInclude.EDGE) && 0 == low.compareTo(start)));
        final boolean includeUpper = (include.contains(FacetRangeInclude.UPPER)
                || (include.contains(FacetRangeInclude.EDGE) && 0 == high.compareTo(end)));

        final String lowS = calc.formatValue(low);
        final String highS = calc.formatValue(high);

        final int count = rangeCount(sf, lowS, highS, includeLower, includeUpper);
        if (count >= minCount && count != previousCount) {
            counts.add(lowS, count);
            previousCount = count;
        }

        low = high;

        gapIdx = Math.min(gaps.length - 1, gapIdx + 1);
    }

    // explicitly return the start and end so all the counts 
    // (including before/after/between) are meaningful - even if mincount
    // has removed the neighboring ranges
    res.add("start", start);
    res.add("end", end);

    final String[] othersP = params.getFieldParams(f, FacetParams.FACET_RANGE_OTHER);
    if (null != othersP && 0 < othersP.length) {
        Set<FacetRangeOther> others = EnumSet.noneOf(FacetRangeOther.class);

        for (final String o : othersP) {
            others.add(FacetRangeOther.get(o));
        }

        // no matter what other values are listed, we don't do
        // anything if "none" is specified.
        if (!others.contains(FacetRangeOther.NONE)) {

            boolean all = others.contains(FacetRangeOther.ALL);
            final String startS = calc.formatValue(start);
            final String endS = calc.formatValue(end);

            if (all || others.contains(FacetRangeOther.BEFORE)) {
                // include upper bound if "outer" or if first gap doesn't already include it
                res.add(FacetRangeOther.BEFORE.toString(),
                        rangeCount(sf, null, startS, false,
                                (include.contains(FacetRangeInclude.OUTER)
                                        || (!(include.contains(FacetRangeInclude.LOWER)
                                                || include.contains(FacetRangeInclude.EDGE))))));

            }
            if (all || others.contains(FacetRangeOther.AFTER)) {
                // include lower bound if "outer" or if last gap doesn't already include it
                res.add(FacetRangeOther.AFTER.toString(),
                        rangeCount(sf, endS, null,
                                (include.contains(FacetRangeInclude.OUTER)
                                        || (!(include.contains(FacetRangeInclude.UPPER)
                                                || include.contains(FacetRangeInclude.EDGE)))),
                                false));
            }
            if (all || others.contains(FacetRangeOther.BETWEEN)) {
                res.add(FacetRangeOther.BETWEEN.toString(), rangeCount(sf, startS, endS,
                        (include.contains(FacetRangeInclude.LOWER) || include.contains(FacetRangeInclude.EDGE)),
                        (include.contains(FacetRangeInclude.UPPER)
                                || include.contains(FacetRangeInclude.EDGE))));

            }
        }
    }
    return res;
}

From source file:org.alfresco.solr.SolrInformationServer.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
private NamedList<Integer> getFacets(SolrQueryRequest request, String query, String field, int minCount) {
    ModifiableSolrParams params = new ModifiableSolrParams(request.getParams()).set(CommonParams.Q, query)
            .set(CommonParams.ROWS, 0).set(FacetParams.FACET, true).set(FacetParams.FACET_FIELD, field)
            .set(FacetParams.FACET_MINCOUNT, minCount);

    SolrQueryResponse response = cloud.getResponse(nativeRequestHandler, request, params);
    NamedList facetCounts = (NamedList) response.getValues().get("facet_counts");
    NamedList facetFields = (NamedList) facetCounts.get("facet_fields");
    return (NamedList) facetFields.get(field);
}

From source file:org.dspace.app.xmlui.aspect.discovery.json.JSONSolrSearcher.java

License:BSD License

@Override
public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
        throws ProcessingException, SAXException, IOException {
    //Retrieve all the given parameters
    Request request = ObjectModelHelper.getRequest(objectModel);
    this.response = ObjectModelHelper.getResponse(objectModel);

    query = request.getParameter(CommonParams.Q);
    if (query == null) {
        query = "*:*";
    }/*from   ww w  . j av a  2  s.  c o m*/

    //Retrieve all our filter queries
    filterQueries = request.getParameterValues(CommonParams.FQ);

    //Retrieve our facet fields
    facetFields = request.getParameterValues(FacetParams.FACET_FIELD);

    //Retrieve our facet limit (if any)
    if (request.getParameter(FacetParams.FACET_LIMIT) != null) {
        try {
            facetLimit = Integer.parseInt(request.getParameter(FacetParams.FACET_LIMIT));
        } catch (Exception e) {
            //Should an invalid value be supplied use -1
            facetLimit = -1;
        }
    } else {
        facetLimit = -1;
    }

    //Retrieve our sorting value
    facetSort = request.getParameter(FacetParams.FACET_SORT);
    //Make sure we have a valid sorting value
    if (!FacetParams.FACET_SORT_INDEX.equals(facetSort) && !FacetParams.FACET_SORT_COUNT.equals(facetSort)) {
        facetSort = null;
    }

    //Retrieve our facet min count
    facetMinCount = 1;
    try {
        facetMinCount = Integer.parseInt(request.getParameter(FacetParams.FACET_MINCOUNT));
    } catch (Exception e) {
        facetMinCount = 1;
    }
    jsonWrf = request.getParameter("json.wrf");

    //Retrieve our discovery solr path
    ExtendedProperties props = null;
    //Method that will retrieve all the possible configs we have

    props = ExtendedProperties.convertProperties(ConfigurationManager.getProperties());

    InputStream is = null;
    try {
        File config = new File(props.getProperty("dspace.dir") + "/config/dspace-solr-search.cfg");
        if (config.exists()) {
            props.combine(new ExtendedProperties(config.getAbsolutePath()));
        } else {
            is = SolrServiceImpl.class.getResourceAsStream("dspace-solr-search.cfg");
            ExtendedProperties defaults = new ExtendedProperties();
            defaults.load(is);
            props.combine(defaults);
        }
    } catch (Exception e) {
        log.error("Error while retrieving solr url", e);
        e.printStackTrace();
    } finally {
        if (is != null) {
            is.close();
        }
    }

    if (props.getProperty("solr.search.server") != null) {
        this.solrServerUrl = props.getProperty("solr.search.server").toString();
    }

}

From source file:org.dspace.app.xmlui.aspect.discovery.json.JSONSolrSearcher.java

License:BSD License

public void generate() throws IOException, SAXException, ProcessingException {
    if (solrServerUrl == null) {
        return;// w w  w.  j a va2  s.  c o m
    }

    Map<String, String> params = new HashMap<String, String>();

    String solrRequestUrl = solrServerUrl + "/select";

    //Add our default parameters
    params.put(CommonParams.ROWS, "0");
    params.put(CommonParams.WT, "json");
    //We uwe json as out output type
    params.put("json.nl", "map");
    params.put("json.wrf", jsonWrf);
    params.put(FacetParams.FACET, Boolean.TRUE.toString());

    //Generate our json out of the given params
    try {
        params.put(CommonParams.Q, URLEncoder.encode(query, Constants.DEFAULT_ENCODING));
    } catch (UnsupportedEncodingException uee) {
        //Should never occur
        return;
    }

    params.put(FacetParams.FACET_LIMIT, String.valueOf(facetLimit));
    if (facetSort != null) {
        params.put(FacetParams.FACET_SORT, facetSort);
    }
    params.put(FacetParams.FACET_MINCOUNT, String.valueOf(facetMinCount));

    solrRequestUrl = AbstractDSpaceTransformer.generateURL(solrRequestUrl, params);
    if (facetFields != null || filterQueries != null) {
        StringBuilder urlBuilder = new StringBuilder(solrRequestUrl);
        if (facetFields != null) {

            //Add our facet fields
            for (String facetField : facetFields) {
                urlBuilder.append("&").append(FacetParams.FACET_FIELD).append("=");

                //This class can only be used for autocomplete facet fields
                if (!facetField.endsWith(".year") && !facetField.endsWith("_ac")) {
                    urlBuilder.append(URLEncoder.encode(facetField + "_ac", Constants.DEFAULT_ENCODING));
                } else {
                    urlBuilder.append(URLEncoder.encode(facetField, Constants.DEFAULT_ENCODING));
                }
            }

        }
        if (filterQueries != null) {
            for (String filterQuery : filterQueries) {
                urlBuilder.append("&").append(CommonParams.FQ).append("=")
                        .append(URLEncoder.encode(filterQuery, Constants.DEFAULT_ENCODING));
            }
        }

        solrRequestUrl = urlBuilder.toString();
    }

    try {
        GetMethod get = new GetMethod(solrRequestUrl);
        new HttpClient().executeMethod(get);
        String result = get.getResponseBodyAsString();
        if (result != null) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(result.getBytes("UTF-8"));

            byte[] buffer = new byte[8192];

            response.setHeader("Content-Length", String.valueOf(result.length()));
            int length;
            while ((length = inputStream.read(buffer)) > -1) {
                out.write(buffer, 0, length);
            }
            out.flush();
        }
    } catch (Exception e) {
        log.error("Error while getting json solr result for discovery search recommendation", e);
        e.printStackTrace();
    }

}

From source file:org.dspace.statistics.SolrLogger.java

License:BSD License

public static void shardSolrIndex() throws IOException, SolrServerException {
    /*/* w  ww  .  j a v  a 2s  . c o  m*/
    Start by faceting by year so we can include each year in a separate core !
     */
    SolrQuery yearRangeQuery = new SolrQuery();
    yearRangeQuery.setQuery("*:*");
    yearRangeQuery.setRows(0);
    yearRangeQuery.setFacet(true);
    yearRangeQuery.add(FacetParams.FACET_RANGE, "time");
    //We go back to 2000 the year 2000, this is a bit overkill but this way we ensure we have everything
    //The alternative would be to sort but that isn't recommended since it would be a very costly query !
    yearRangeQuery.add(FacetParams.FACET_RANGE_START,
            "NOW/YEAR-" + (Calendar.getInstance().get(Calendar.YEAR) - 2000) + "YEARS");
    //Add the +0year to ensure that we DO NOT include the current year
    yearRangeQuery.add(FacetParams.FACET_RANGE_END, "NOW/YEAR+0YEARS");
    yearRangeQuery.add(FacetParams.FACET_RANGE_GAP, "+1YEAR");
    yearRangeQuery.add(FacetParams.FACET_MINCOUNT, String.valueOf(1));

    //Create a temp directory to store our files in !
    File tempDirectory = new File(
            ConfigurationManager.getProperty("dspace.dir") + File.separator + "temp" + File.separator);
    tempDirectory.mkdirs();

    QueryResponse queryResponse = solr.query(yearRangeQuery);
    //We only have one range query !
    List<RangeFacet.Count> yearResults = queryResponse.getFacetRanges().get(0).getCounts();
    for (RangeFacet.Count count : yearResults) {
        long totalRecords = count.getCount();

        //Create a range query from this !
        //We start with out current year
        DCDate dcStart = new DCDate(count.getValue());
        Calendar endDate = Calendar.getInstance();
        //Advance one year for the start of the next one !
        endDate.setTime(dcStart.toDate());
        endDate.add(Calendar.YEAR, 1);
        DCDate dcEndDate = new DCDate(endDate.getTime());

        StringBuilder filterQuery = new StringBuilder();
        filterQuery.append("time:([");
        filterQuery.append(ClientUtils.escapeQueryChars(dcStart.toString()));
        filterQuery.append(" TO ");
        filterQuery.append(ClientUtils.escapeQueryChars(dcEndDate.toString()));
        filterQuery.append("]");
        //The next part of the filter query excludes the content from midnight of the next year !
        filterQuery.append(" NOT ").append(ClientUtils.escapeQueryChars(dcEndDate.toString()));
        filterQuery.append(")");

        Map<String, String> yearQueryParams = new HashMap<String, String>();
        yearQueryParams.put(CommonParams.Q, "*:*");
        yearQueryParams.put(CommonParams.ROWS, String.valueOf(10000));
        yearQueryParams.put(CommonParams.FQ, filterQuery.toString());
        yearQueryParams.put(CommonParams.WT, "csv");

        //Start by creating a new core
        String coreName = "statistics-" + dcStart.getYear();
        HttpSolrServer statisticsYearServer = createCore(solr, coreName);

        System.out.println("Moving: " + totalRecords + " into core " + coreName);
        log.info("Moving: " + totalRecords + " records into core " + coreName);

        List<File> filesToUpload = new ArrayList<File>();
        for (int i = 0; i < totalRecords; i += 10000) {
            String solrRequestUrl = solr.getBaseURL() + "/select";
            solrRequestUrl = generateURL(solrRequestUrl, yearQueryParams);

            HttpGet get = new HttpGet(solrRequestUrl);
            HttpResponse response = new DefaultHttpClient().execute(get);
            InputStream csvInputstream = response.getEntity().getContent();
            //Write the csv ouput to a file !
            File csvFile = new File(tempDirectory.getPath() + File.separatorChar + "temp." + dcStart.getYear()
                    + "." + i + ".csv");
            FileUtils.copyInputStreamToFile(csvInputstream, csvFile);
            filesToUpload.add(csvFile);

            //Add 10000 & start over again
            yearQueryParams.put(CommonParams.START, String.valueOf((i + 10000)));
        }

        for (File tempCsv : filesToUpload) {
            //Upload the data in the csv files to our new solr core
            ContentStreamUpdateRequest contentStreamUpdateRequest = new ContentStreamUpdateRequest(
                    "/update/csv");
            contentStreamUpdateRequest.setParam("stream.contentType", "text/plain;charset=utf-8");
            contentStreamUpdateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
            contentStreamUpdateRequest.addFile(tempCsv, "text/plain;charset=utf-8");

            statisticsYearServer.request(contentStreamUpdateRequest);
        }
        statisticsYearServer.commit(true, true);

        //Delete contents of this year from our year query !
        solr.deleteByQuery(filterQuery.toString());
        solr.commit(true, true);

        log.info("Moved " + totalRecords + " records into core: " + coreName);
    }

    FileUtils.deleteDirectory(tempDirectory);
}

From source file:org.dspace.statistics.SolrLoggerServiceImpl.java

License:BSD License

@Override
public void shardSolrIndex() throws IOException, SolrServerException {
    /*/*from  w  w  w  .ja  v a2s .  c  o  m*/
    Start by faceting by year so we can include each year in a separate core !
     */
    SolrQuery yearRangeQuery = new SolrQuery();
    yearRangeQuery.setQuery("*:*");
    yearRangeQuery.setRows(0);
    yearRangeQuery.setFacet(true);
    yearRangeQuery.add(FacetParams.FACET_RANGE, "time");
    //We go back to 2000 the year 2000, this is a bit overkill but this way we ensure we have everything
    //The alternative would be to sort but that isn't recommended since it would be a very costly query !
    yearRangeQuery.add(FacetParams.FACET_RANGE_START,
            "NOW/YEAR-" + (Calendar.getInstance().get(Calendar.YEAR) - 2000) + "YEARS");
    //Add the +0year to ensure that we DO NOT include the current year
    yearRangeQuery.add(FacetParams.FACET_RANGE_END, "NOW/YEAR+0YEARS");
    yearRangeQuery.add(FacetParams.FACET_RANGE_GAP, "+1YEAR");
    yearRangeQuery.add(FacetParams.FACET_MINCOUNT, String.valueOf(1));

    //Create a temp directory to store our files in !
    File tempDirectory = new File(
            configurationService.getProperty("dspace.dir") + File.separator + "temp" + File.separator);
    tempDirectory.mkdirs();

    QueryResponse queryResponse = solr.query(yearRangeQuery);
    //We only have one range query !
    List<RangeFacet.Count> yearResults = queryResponse.getFacetRanges().get(0).getCounts();
    for (RangeFacet.Count count : yearResults) {
        long totalRecords = count.getCount();

        //Create a range query from this !
        //We start with out current year
        DCDate dcStart = new DCDate(count.getValue());
        Calendar endDate = Calendar.getInstance();
        //Advance one year for the start of the next one !
        endDate.setTime(dcStart.toDate());
        endDate.add(Calendar.YEAR, 1);
        DCDate dcEndDate = new DCDate(endDate.getTime());

        StringBuilder filterQuery = new StringBuilder();
        filterQuery.append("time:([");
        filterQuery.append(ClientUtils.escapeQueryChars(dcStart.toString()));
        filterQuery.append(" TO ");
        filterQuery.append(ClientUtils.escapeQueryChars(dcEndDate.toString()));
        filterQuery.append("]");
        //The next part of the filter query excludes the content from midnight of the next year !
        filterQuery.append(" NOT ").append(ClientUtils.escapeQueryChars(dcEndDate.toString()));
        filterQuery.append(")");

        Map<String, String> yearQueryParams = new HashMap<String, String>();
        yearQueryParams.put(CommonParams.Q, "*:*");
        yearQueryParams.put(CommonParams.ROWS, String.valueOf(10000));
        yearQueryParams.put(CommonParams.FQ, filterQuery.toString());
        yearQueryParams.put(CommonParams.WT, "csv");

        //Start by creating a new core
        String coreName = "statistics-" + dcStart.getYear();
        HttpSolrServer statisticsYearServer = createCore(solr, coreName);

        System.out.println("Moving: " + totalRecords + " into core " + coreName);
        log.info("Moving: " + totalRecords + " records into core " + coreName);

        List<File> filesToUpload = new ArrayList<File>();
        for (int i = 0; i < totalRecords; i += 10000) {
            String solrRequestUrl = solr.getBaseURL() + "/select";
            solrRequestUrl = generateURL(solrRequestUrl, yearQueryParams);

            HttpGet get = new HttpGet(solrRequestUrl);
            HttpResponse response = new DefaultHttpClient().execute(get);
            InputStream csvInputstream = response.getEntity().getContent();
            //Write the csv ouput to a file !
            File csvFile = new File(tempDirectory.getPath() + File.separatorChar + "temp." + dcStart.getYear()
                    + "." + i + ".csv");
            FileUtils.copyInputStreamToFile(csvInputstream, csvFile);
            filesToUpload.add(csvFile);

            //Add 10000 & start over again
            yearQueryParams.put(CommonParams.START, String.valueOf((i + 10000)));
        }

        for (File tempCsv : filesToUpload) {
            //Upload the data in the csv files to our new solr core
            ContentStreamUpdateRequest contentStreamUpdateRequest = new ContentStreamUpdateRequest(
                    "/update/csv");
            contentStreamUpdateRequest.setParam("stream.contentType", "text/plain;charset=utf-8");
            contentStreamUpdateRequest.setParam("skip", "_version_");
            contentStreamUpdateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
            contentStreamUpdateRequest.addFile(tempCsv, "text/plain;charset=utf-8");

            statisticsYearServer.request(contentStreamUpdateRequest);
        }
        statisticsYearServer.commit(true, true);

        //Delete contents of this year from our year query !
        solr.deleteByQuery(filterQuery.toString());
        solr.commit(true, true);

        log.info("Moved " + totalRecords + " records into core: " + coreName);
    }

    FileUtils.deleteDirectory(tempDirectory);
}