Example usage for org.apache.solr.client.solrj SolrQuery setFacetLimit

List of usage examples for org.apache.solr.client.solrj SolrQuery setFacetLimit

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery setFacetLimit.

Prototype

public SolrQuery setFacetLimit(int lim) 

Source Link

Document

set the facet limit

Usage

From source file:org.ambraproject.search.service.SolrSearchService.java

License:Apache License

/**
 * Populate facets of the search object.
 *
 * If no search results and hence facets are found remove defined filters and try
 * the search again.  Journals and ArticleType facets will always be the complete list.
 *  /* ww  w .ja  v  a 2 s  . c  o m*/
 *
 * @param searchParameters The search parameters
 * @return a populared SearchResultSinglePage object
 * @throws ApplicationException
 */
public SearchResultSinglePage getFilterData(SearchParameters searchParameters) throws ApplicationException {
    //TODO: This function queries SOLR for the journal and article type list
    //We should migrate this away from config and into a database when it is
    //available

    //Does not impact unformattedQuery field.
    SearchParameters sp = cleanStrings(searchParameters);

    String q = searchParameters.getUnformattedQuery().trim();

    //In this use case, if the query string is empty, we want to get facets for everything
    if (q.length() == 0) {
        q = "*:*";
    }

    if (log.isDebugEnabled()) {
        log.debug("Solr Search performed to get facet data on the unformattedSearch String: " + q);
    }

    //We want a complete set of facet data.  So first, lets get it all
    SolrQuery query = createQuery("*:*", 0, 0, false);

    //Remove facets we don't use in this case
    query.removeFacetField("author_facet");
    query.removeFacetField("editor_facet");
    query.removeFacetField("affiliate_facet");
    //Add the one we do want in this case.
    query.addFacetField("cross_published_journal_key");
    query.setFacetLimit(MAX_FACET_SIZE);

    //Related to JO: http://joborder.plos.org/view.php?id=17480
    //(for now) we don't want to search on Issue Images
    query.addFilterQuery(createFilterNoIssueImageDocuments());

    SearchResultSinglePage preFilterResults = search(query);

    setFilters(query, sp, true);

    query.setQuery(q);

    SearchResultSinglePage results = null;
    try {
        results = search(query);
    } catch (SolrException e) {
        query.setQuery("*:*");
        if (log.isWarnEnabled()) {
            log.warn("Solr Search failed on the unformattedSearch String: { " + query.getQuery()
                    + " } so the query will be re-run using the String *:* to populate the Filters"
                    + " on the Advanced Search page.", e);
        }
    }

    if (results == null || results.getTotalNoOfResults() == 0) {
        //If no results, remove optional filters and try again
        for (String filter : query.getFilterQueries()) {
            if (filter.indexOf(createFilterFullDocuments()) < 0) {
                query.removeFilterQuery(filter);
            }
        }

        results = search(query);

        //If results are STILL empty.  We must return something for subjects.
        //So let's use the global list
        if (results.getTotalNoOfResults() == 0) {
            results.setSubjectFacet(preFilterResults.getSubjectFacet());
        }

        results.setFiltersReset(true);
    }

    //Lets always return ALL values for journals and article types
    //These lists will not be dependant on the user's other
    //selections other then the query
    //However, subjects will be!
    results.setJournalFacet(preFilterResults.getJournalFacet());
    results.setArticleTypeFacet(preFilterResults.getArticleTypeFacet());

    return results;
}

From source file:org.ambraproject.search.service.SolrSearchService.java

License:Apache License

private SolrQuery createQuery(String queryString, int startPage, int pageSize, boolean useDismax) {
    SolrQuery query = new SolrQuery(queryString);
    query.setTimeAllowed(queryTimeout);//  w  ww.  j  ava  2 s .  c o  m
    query.setIncludeScore(true); // The relevance (of each results element) to the search terms.
    query.setHighlight(true);

    if (useDismax) {
        query.set("defType", "dismax");
    }

    //TODO: Put The "options" from the "queryField" picklist into a config file.
    //This list matches the "options" from the "queryField" picklist on unformattedSearch.ftl,
    //without the "date" fields.

    query.set("hl.fl", this.highlightFields);
    query.set("hl.requireFieldMatch", true);
    query.setStart(startPage * pageSize); // Which results element to return first in this batch.
    query.setRows(pageSize); // The number of results elements to return.
    // request only fields that we need to display
    query.setFields("id", "score", "title_display", "publication_date", "eissn", "journal", "article_type",
            "author_display");
    query.addFacetField("subject_facet");
    query.addFacetField("author_facet");
    query.addFacetField("editor_facet");
    query.addFacetField("article_type_facet");
    query.addFacetField("affiliate_facet");
    query.set("facet.method", "fc");
    query.setFacetLimit(MAX_FACET_SIZE);
    query.setFacetMinCount(MIN_FACET_COUNT);
    // Add a filter to ensure that Solr never returns partial documents
    query.addFilterQuery(createFilterFullDocuments());

    return query;
}

From source file:org.ambraproject.search.service.SolrSearchService.java

License:Apache License

private SolrQuery createJournalFacetQuery(String queryString, boolean useDismax) {
    SolrQuery query = new SolrQuery(queryString);
    query.setTimeAllowed(queryTimeout);//from ww w.  jav  a 2s  .  c o  m
    query.setIncludeScore(false);
    query.setHighlight(false);
    query.setRows(0);
    query.setFacetLimit(MAX_FACET_SIZE);
    query.setFacetMinCount(MIN_FACET_COUNT);

    if (useDismax) {
        query.set("defType", "dismax");
    }

    query.addFacetField("cross_published_journal_key");
    // Add a filter to ensure that Solr never returns partial documents
    query.addFilterQuery(createFilterFullDocuments());

    return query;
}

From source file:org.ambraproject.search.service.SolrSearchService.java

License:Apache License

private SolrQuery createKeywordFacetQuery(String queryString) {
    SolrQuery query = new SolrQuery();
    query.setTimeAllowed(queryTimeout);/*from   ww  w. j a  va  2 s.c o  m*/
    query.setIncludeScore(false);
    query.setHighlight(false);
    query.setRows(0);
    query.set("defType", "dismax");
    query.set("qf", "doc_partial_body");
    query.addFacetField("doc_partial_type");
    query.setFacetLimit(MAX_FACET_SIZE);
    query.setFacetMinCount(MIN_FACET_COUNT);
    // Add a filter to ensure that Solr never returns partial documents
    query.addFilterQuery(createFilterPartialDocuments());
    query.setQuery(queryString);

    return query;
}

From source file:org.ambraproject.service.search.SolrSearchService.java

License:Apache License

/**
 * Populate facets of the search object.
 * <p/>/*  ww w.  j  a v a2  s  . com*/
 * If no search results and hence facets are found remove defined filters and try the search again.  Journals will
 * always be the complete list.
 *
 * @param searchParameters The search parameters
 * @return a populared SearchResultSinglePage object
 * @throws ApplicationException
 */
public SearchResultSinglePage getFilterData(SearchParameters searchParameters) throws ApplicationException {
    //TODO: This function queries SOLR for the journal and article type list
    //We should migrate this away from config and into a database when it is
    //available

    //Does not impact unformattedQuery field.
    SearchParameters sp = cleanStrings(searchParameters);

    String q = searchParameters.getUnformattedQuery().trim();

    //In this use case, if the query string is empty, we want to get facets for everything
    if (q.length() == 0) {
        q = "*:*";
    }

    if (log.isDebugEnabled()) {
        log.debug("Solr Search performed to get facet data on the unformattedSearch String: " + q);
    }

    //We want a complete set of facet data.  So first, lets get it all
    SolrQuery query = createQuery("*:*", 0, 0, false);

    //Remove facets we don't use in this case
    query.removeFacetField("author_facet");
    query.removeFacetField("editor_facet");
    query.removeFacetField("affiliate_facet");
    //Add the one we do want in this case.
    query.addFacetField("cross_published_journal_key");
    query.addFacetField("article_type");
    query.setFacetLimit(MAX_FACET_SIZE);

    //Related to JO: http://joborder.plos.org/view.php?id=17480
    //(for now) we don't want to search on Issue Images
    query.addFilterQuery(createFilterNoIssueImageDocuments());

    SearchResultSinglePage preFilterResults = search(query);

    setFilters(query, sp, false, false);

    query.setQuery(q);

    SearchResultSinglePage results = null;
    try {
        results = search(query);
    } catch (SolrException e) {
        query.setQuery("*:*");
        if (log.isWarnEnabled()) {
            log.warn("Solr Search failed on the unformattedSearch String: { " + query.getQuery()
                    + " } so the query will be re-run using the String *:* to populate the Filters"
                    + " on the Advanced Search page.", e);
        }
    }

    if (results == null || results.getTotalNoOfResults() == 0) {
        //If no results, remove optional filters and try again
        for (String filter : query.getFilterQueries()) {
            if (filter.indexOf(createFilterFullDocuments()) < 0) {
                query.removeFilterQuery(filter);
            }
        }

        results = search(query);

        //If results are STILL empty.  We must return something for subjects and article type.
        //So let's use the global list
        if (results.getTotalNoOfResults() == 0) {
            results.setSubjectFacet(preFilterResults.getSubjectFacet());
            results.setArticleTypeFacet(preFilterResults.getArticleTypeFacet());
        }

        results.setFiltersReset(true);
    }

    //Lets always return ALL values for journals
    //These lists will not be dependant on the user's other
    //selections other then the query
    //However, subjects and article type will be!
    results.setJournalFacet(preFilterResults.getJournalFacet());
    results.setArticleTypeFacet(preFilterResults.getArticleTypeFacet());

    return results;
}

From source file:org.ambraproject.service.search.SolrSearchService.java

License:Apache License

/**
 * Executes a search where results are grouped by one of the subject facets in the solr schema.
 *
 * @param facetName the subject facet of interest.  Depending on the application, this should be
 *     either "subject_facet" or "subject_hierarchy".  The first does not include the entire taxonomy
 *     path, while the second does./*from  www  .  j  av  a2  s  .  c  o m*/
 * @param journal journal of interest
 * @return solr server response
 * @throws ApplicationException
 */
private QueryResponse executeSubjectFacetSearch(String facetName, String journal) throws ApplicationException {
    SolrQuery query = createQuery("*:*", 0, 0, false);

    // We don't care about results, just facet counts.
    query.setRows(0);

    // We only care about full documents
    query.addFilterQuery(createFilterFullDocuments());
    query.addFilterQuery(createFilterNoIssueImageDocuments());

    // Remove facets we don't use in this case.
    query.removeFacetField("author_facet");
    query.removeFacetField("editor_facet");
    query.removeFacetField("affiliate_facet");
    query.removeFacetField("subject_facet");
    query.removeFacetField("subject_hierarchy");

    // Add the one we do want.
    query.addFacetField(facetName);

    if (journal != null && journal.length() > 0) {
        query.addFilterQuery("cross_published_journal_key:" + journal);
    }

    query.setFacetLimit(-1); // unlimited
    return getSOLRResponse(query);
}

From source file:org.ambraproject.service.search.SolrSearchService.java

License:Apache License

private SortedMap<String, Long> getTopSubjectsFromSOLR() throws ApplicationException {
    SolrQuery query = createQuery("*:*", 0, 0, false);

    // We don't care about results, just facet counts.
    query.setRows(0);/*from ww w.  j av a  2  s  .co  m*/

    // We only care about full documents
    query.addFilterQuery(createFilterFullDocuments());

    // Remove facets we don't use in this case.
    query.removeFacetField("author_facet");
    query.removeFacetField("editor_facet");
    query.removeFacetField("affiliate_facet");
    query.removeFacetField("subject_facet");

    // Add the one we do want.
    query.addFacetField("subject_level_1");
    query.setFacetLimit(-1); // unlimited

    QueryResponse queryResponse = getSOLRResponse(query);
    FacetField facet = queryResponse.getFacetField("subject_level_1");

    SortedMap<String, Long> results = new TreeMap<String, Long>();

    //If there is no facet.  Should never happen outside a unit test
    if (facet.getValues() == null) {
        log.warn("No subject_level_1 facet");
    } else {
        for (FacetField.Count count : facet.getValues()) {
            results.put(count.getName(), count.getCount());
        }
    }

    return results;
}

From source file:org.ambraproject.service.search.SolrSearchService.java

License:Apache License

private SolrQuery createQuery(String queryString, int startPage, int pageSize, boolean useDismax) {
    SolrQuery query = new SolrQuery(queryString);
    query.setTimeAllowed(queryTimeout);/* w w  w .  j av  a2s .c  o m*/
    query.setIncludeScore(true); // The relevance (of each results element) to the search terms.
    query.setHighlight(false);

    if (useDismax) {
        query.set("defType", "dismax");
    }

    //TODO: Put The "options" from the "queryField" picklist into a config file.
    //This list matches the "options" from the "queryField" picklist on unformattedSearch.ftl,
    //without the "date" fields.

    query.setStart(startPage * pageSize); // Which results element to return first in this batch.
    query.setRows(pageSize); // The number of results elements to return.
    // request only fields that we need to display
    query.setFields("id", "score", "title_display", "publication_date", "eissn", "journal", "article_type",
            "author_display", "abstract", "abstract_primary_display", "striking_image", "figure_table_caption",
            "subject", "expression_of_concern", "retraction");
    query.addFacetField("subject_facet");
    query.addFacetField("author_facet");
    query.addFacetField("editor_facet");
    query.addFacetField("article_type_facet");
    query.addFacetField("affiliate_facet");
    query.set("facet.method", "fc");
    query.setFacetLimit(MAX_FACET_SIZE);
    query.setFacetMinCount(MIN_FACET_COUNT);
    // Add a filter to ensure that Solr never returns partial documents
    query.addFilterQuery(createFilterFullDocuments());

    return query;
}

From source file:org.ambraproject.service.search.SolrSearchService.java

License:Apache License

private SolrQuery createFacetsQuery(String queryString, String field, boolean useDismax) {
    SolrQuery query = new SolrQuery(queryString);
    query.setTimeAllowed(queryTimeout);/*from   ww  w. j a v a2  s.c  o m*/
    query.setIncludeScore(false);
    query.setHighlight(false);
    query.setRows(0);
    query.setFacetLimit(MAX_FACET_SIZE);
    query.setFacetMinCount(MIN_FACET_COUNT);

    if (useDismax) {
        query.set("defType", "dismax");
    }

    query.addFacetField(field);
    // Add a filter to ensure that Solr never returns partial documents
    query.addFilterQuery(createFilterFullDocuments());

    return query;
}

From source file:org.apache.drill.exec.store.solr.SolrClientAPIExec.java

License:Apache License

public QueryResponse getSolr4Docs(String solrServer, String solrCoreName, String uniqueKey, List<String> fields,
        Long solrDocFectCount, String cursorMark, StringBuilder filters, List<SolrAggrParam> solrAggrParams,
        List<SolrSortParam> solrSortParams, List<String> aggrFieldNames, boolean isGroup,
        boolean useFacetPivotFromGroupCount) {

    String solrUrl = solrServer;//from   w ww .j a  v a2s . com
    if (solrCoreName != null) {
        solrUrl = solrServer + solrCoreName;
    }
    SolrClient solrClient = new HttpSolrClient(solrUrl);
    String fieldStr = null;
    String[] fieldListArr = null;
    List<String> statsFieldList = Lists.newArrayList();

    SolrQuery solrQuery = new SolrQuery().setTermsRegexFlag("case_insensitive").setQuery(uniqueKey + ":*")
            .setRows(0);

    if (filters.length() > 0) {
        solrQuery.setParam("fq", filters.toString());
        SolrClientAPIExec.logger.debug("Filter query [ " + filters.toString() + " ]");
    }

    if (!fields.isEmpty()) {
        fieldStr = Joiner.on(",").join(fields);
        solrQuery.setParam("fl", fieldStr);
        solrQuery.setRows(solrDocFectCount.intValue());
        SolrClientAPIExec.logger.debug("Response field list [" + fieldStr + "]");
    }
    // facet.pivot={!stats=s1}category,manufacturer
    // stats.field={!key=avg_price tag=s1 mean=true}price
    // stats.field={!tag=s1 min=true max=true}user_rating

    if (solrAggrParams != null && !solrAggrParams.isEmpty() && !useFacetPivotFromGroupCount) {
        solrQuery.setGetFieldStatistics(true);
        for (SolrAggrParam solrAggrParam : solrAggrParams) {
            String statsField = solrAggrParam.getFieldName();
            if (!fields.contains(statsField)) {
                statsField = uniqueKey;
            }
            if (!statsFieldList.contains(statsField)) {
                statsFieldList.add(statsField);
            }
            fields.remove(statsField);
        }
        if (!fields.isEmpty()) {
            fieldListArr = fields.toArray(new String[fields.size()]);
        }

        for (String statsField : statsFieldList) {
            solrQuery.setGetFieldStatistics(statsField);
            SolrClientAPIExec.logger.debug("Adding stats field parameter.. [ " + statsField + " ]");
            if (isGroup) {
                List<String> groupFields = Lists.newArrayList();
                for (String aggrField : aggrFieldNames) {
                    if (fields.contains(aggrField)) {
                        groupFields.add(aggrField);
                    }
                }
                SolrClientAPIExec.logger.debug("Adding stats facet parameters.. [ " + groupFields + " ]");
                solrQuery.addStatsFieldFacets(statsField, groupFields.toArray(new String[groupFields.size()]));
            }

        }
        solrQuery.setRows(0);
    } else if (isGroup) {
        fieldListArr = fields.toArray(new String[fields.size()]);
        solrQuery.setFacet(true);
        if (fields.size() == 1) {
            solrQuery.addFacetField(fieldListArr);
            solrQuery.setFacetLimit(-1);
        } else {
            solrQuery.addFacetPivotField(Joiner.on(",").join(fields));
        }
        solrQuery.setRows(0);
        solrQuery.setFacetMinCount(1);

        // solrQuery.set(GroupParams.GROUP, true);
        // solrQuery.set(GroupParams.GROUP_FIELD, fieldListArr);
        // solrQuery.set(GroupParams.GROUP_MAIN, true);
        // solrQuery.set(GroupParams.GROUP_FORMAT, "simple");
        // solrQuery.set("group.ngroups", "true");
    }
    if (!solrSortParams.isEmpty()) {
        for (SolrSortParam solrSortParam : solrSortParams) {
            String solrSortField = solrSortParam.getSortFieldName();
            ORDER solrSortDir = solrSortParam.getSortDir();
            solrQuery.addSort(solrSortField, solrSortDir);

        }
    } else {
        solrQuery.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
        solrQuery.setSort(SolrQuery.SortClause.desc(uniqueKey));
    }
    QueryResponse rsp = null;
    try {
        SolrClientAPIExec.logger
                .info("Submitting Query :" + solrServer + "/select" + solrQuery.toQueryString());

        rsp = solrClient.query(solrQuery);

        SolrClientAPIExec.logger.info("Response recieved from [ " + solrServer + " ] core [ " + solrCoreName
                + " ] in " + rsp.getQTime() + " MS.");

    } catch (SolrServerException | IOException e) {
        SolrClientAPIExec.logger
                .debug("Error occured while fetching results from solr server " + e.getMessage());
    } finally {
        try {
            solrClient.close();
        } catch (IOException e) {
            SolrClientAPIExec.logger
                    .debug("Error occured while closing connection of solr server " + e.getMessage());
        }
    }

    return rsp;
}