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

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

Introduction

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

Prototype

public SolrQuery addSort(String field, ORDER order) 

Source Link

Document

Adds a single sort clause to the end of the current sort information.

Usage

From source file:org.codelibs.fess.web.admin.DocumentAction.java

License:Apache License

protected SessionIdList<Map<String, String>> getSessionIdList(final String groupName) {
    final SessionIdList<Map<String, String>> sessionIdList = new SessionIdList<Map<String, String>>();

    SolrGroup serverGroup;//from  w ww.  j av a 2 s . c om
    try {
        serverGroup = solrGroupManager.getSolrGroup(groupName);
    } catch (final Exception e) {
        if (logger.isInfoEnabled()) {
            logger.info(e.getMessage());
        }
        return sessionIdList;
    }

    final SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.setFacet(true);
    query.addFacetField(fieldHelper.segmentField);
    query.addSort(fieldHelper.segmentField, ORDER.desc);

    final QueryResponse queryResponse = serverGroup.query(query);
    final List<FacetField> facets = queryResponse.getFacetFields();
    for (final FacetField facet : facets) {
        final List<FacetField.Count> facetEntries = facet.getValues();
        if (facetEntries != null) {
            for (final FacetField.Count fcount : facetEntries) {
                final Map<String, String> map = new HashMap<String, String>(3);
                map.put("label", fcount.getName() + " (" + fcount.getCount() + ")");
                map.put("value", fcount.getName());
                map.put("count", Long.toString(fcount.getCount()));
                sessionIdList.add(map);
                sessionIdList.addTotalCount(fcount.getCount());
            }
        }
    }
    return sessionIdList;
}

From source file:org.dspace.util.SolrUpgradePre6xStatistics.java

License:BSD License

private int updateRecords(String query) throws SolrServerException, SQLException, IOException {
    int initNumProcessed = numProcessed;
    SolrQuery sQ = new SolrQuery();
    sQ.setQuery(query);/*w ww  .  j a v  a2  s  .c om*/
    sQ.setRows(batchSize);

    // Ensure that items are grouped by id
    // Sort by id fails due to presense of id and string fields. The ord function
    // seems to help
    sQ.addSort("type", SolrQuery.ORDER.desc);
    sQ.addSort("scopeType", SolrQuery.ORDER.desc);
    sQ.addSort("ord(owningItem)", SolrQuery.ORDER.desc);
    sQ.addSort("id", SolrQuery.ORDER.asc);
    sQ.addSort("scopeId", SolrQuery.ORDER.asc);

    QueryResponse sr = server.query(sQ);
    SolrDocumentList sdl = sr.getResults();

    for (int i = 0; i < sdl.size() && (numProcessed < numRec); i++) {
        SolrDocument sd = sdl.get(i);
        SolrInputDocument input = ClientUtils.toSolrInputDocument(sd);
        input.remove("_version_");
        for (FIELD col : FIELD.values()) {
            mapField(input, col);
        }

        docs.add(input);
        ++numProcessed;
    }
    return numProcessed - initNumProcessed;
}

From source file:org.gbif.common.search.builder.SolrQueryBuilder.java

License:Apache License

/**
 * Sets the primary sort order query information.
 * //from  ww  w. j ava2  s .  c o  m
 * @param solrQuery to be modified.
 */
private void setPrimarySortOrder(SolrQuery solrQuery) {
    if (primarySortOrder != null) {
        for (Map.Entry<String, SolrQuery.ORDER> so : primarySortOrder.entrySet()) {
            solrQuery.addSort(so.getKey(), so.getValue());
        }
    }
}

From source file:org.geotools.data.solr.SolrDataStore.java

License:Open Source License

/**
 * Builds the SolrJ query with support of subset of fields, limit/offset, sorting, OGC filter
 * encoding and viewParams <br>// w w  w  .  ja  v  a  2  s  . co m
 * The SOLR query always need the order by PK field to enable pagination and efficient data
 * retrieving <br>
 * Currently only additional "q" and "fq" SOLR parameters can be passed using vireParams, this
 * conditions are added in AND with others
 * 
 * @param featureType the feature type to query
 * @param q the OGC query to translate in SOLR request
 * 
 * @see {@link Hints#VIRTUAL_TABLE_PARAMETERS}
 * 
 */
protected SolrQuery select(SimpleFeatureType featureType, Query q) {
    SolrQuery query = new SolrQuery();
    query.setParam("omitHeader", true);
    try {
        // Column names
        if (q.getPropertyNames() != null) {
            for (String prop : q.getPropertyNames()) {
                query.addField(prop);
            }
        }
        query.setQuery("*:*");

        // Encode limit/offset, if necessary
        if (q.getStartIndex() != null && q.getStartIndex() >= 0) {
            query.setStart(q.getStartIndex());
        }
        if (q.getMaxFeatures() > 0) {
            query.setRows(q.getMaxFeatures());
        }

        // Sort
        ORDER naturalSortOrder = ORDER.asc;
        if (q.getSortBy() != null) {
            for (SortBy sort : q.getSortBy()) {
                if (sort.getPropertyName() != null) {
                    query.addSort(sort.getPropertyName().getPropertyName(),
                            sort.getSortOrder().equals(SortOrder.ASCENDING) ? ORDER.asc : ORDER.desc);
                } else {
                    naturalSortOrder = sort.getSortOrder().equals(SortOrder.ASCENDING) ? ORDER.asc : ORDER.desc;
                }
            }
        }

        // Always add natural sort by PK to support pagination
        query.addSort(getPrimaryKey(featureType.getTypeName()).getName(), naturalSortOrder);

        // Encode OGC filer
        FilterToSolr f2s = initializeFilterToSolr(featureType);
        String fq = this.field + ":" + featureType.getTypeName();
        Filter simplified = SimplifyingFilterVisitor.simplify(q.getFilter());
        String ffq = f2s.encodeToString(simplified);
        if (ffq != null && !ffq.isEmpty()) {
            fq = fq + " AND " + ffq;
        }
        query.setFilterQueries(fq);

        // Add viewpPrams
        addViewparams(q, query);

    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
    }
    return query;
}

From source file:org.mousephenotype.cda.solr.service.ExpressionService.java

License:Apache License

/**
 *
 * @param mgiAccession//ww  w  .  j a  v a 2s  .  co  m
 *            if mgi accesion null assume a request for control data
 * @param fields
 * @return
 * @throws SolrServerException, IOException
 */
private QueryResponse getCategoricalAdultLacZData(String mgiAccession, boolean embryo, String... fields)
        throws SolrServerException, IOException {
    // e.g.
    // http://ves-ebi-d0.ebi.ac.uk:8090/mi/impc/dev/solr/experiment/select?q=gene_accession_id:%22MGI:1351668%22&facet=true&facet.field=parameter_name&facet.mincount=1&fq=(procedure_name:%22Adult%20LacZ%22)&rows=10000
    SolrQuery solrQuery = new SolrQuery();
    if (mgiAccession != null) {
        solrQuery.setQuery(ImageDTO.GENE_ACCESSION_ID + ":\"" + mgiAccession + "\"");
    } else {
        // http://ves-ebi-d0.ebi.ac.uk:8090/mi/impc/dev/solr/impc_images/select?q=biological_sample_group:control&facet=true&facet.field=ma_term&facet.mincount=1&fq=(parameter_name:%22LacZ%20Images%20Section%22%20OR%20parameter_name:%22LacZ%20Images%20Wholemount%22)&rows=100000
        solrQuery.setQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":\"" + "control" + "\"");
    }
    if (embryo) {
        solrQuery.addFilterQuery(ImageDTO.PROCEDURE_NAME + ":\"Embryo LacZ\"");
        solrQuery.addFilterQuery("-" + ImageDTO.PARAMETER_NAME + ":\"LacZ images section\"");
        solrQuery.addFilterQuery("-" + ImageDTO.PARAMETER_NAME + ":\"LacZ images wholemount\"");
        solrQuery.addFilterQuery(ObservationDTO.OBSERVATION_TYPE + ":\"categorical\"");
    } else {
        solrQuery.addFilterQuery(ImageDTO.PROCEDURE_NAME + ":\"Adult LacZ\"");
        solrQuery.addFilterQuery("-" + ImageDTO.PARAMETER_NAME + ":\"LacZ Images Section\"");
        solrQuery.addFilterQuery("-" + ImageDTO.PARAMETER_NAME + ":\"LacZ Images Wholemount\"");
        solrQuery.addFilterQuery(ObservationDTO.OBSERVATION_TYPE + ":\"categorical\"");
    }

    solrQuery.addSort(ImageDTO.ID, SolrQuery.ORDER.asc);
    solrQuery.setFields(fields);
    solrQuery.setRows(Integer.MAX_VALUE);
    solrQuery.setSort(ObservationDTO.ID, SolrQuery.ORDER.asc);

    QueryResponse response = experimentCore.query(solrQuery);
    return response;
}

From source file:org.mousephenotype.cda.solr.service.ImageService.java

License:Apache License

public QueryResponse getHeadlineImages(String mgiAccession, String parameterStableId,
        int numberOfImagesToRetrieve, SexType sex, String parameterAssociationValue,
        String parameterAssociationStableId) throws SolrServerException, IOException {

    //need equivalent to this in order to get both control and experimental images filtered by gene if experimental image
    //https://wwwdev.ebi.ac.uk/mi/impc/dev/solr/impc_images/select?q=*:*&fq=(gene_accession_id:%22MGI:2446296%22%20OR%20biological_sample_group:%22control%22)&fq=parameter_association_stable_id:%22MGP_IMM_086_001%22&rows=1000
    SolrQuery solrQuery = new SolrQuery().setQuery("*:*");
    //gene accession will take precedence if both acc and symbol supplied
    if (StringUtils.isNotEmpty(mgiAccession)) {
        solrQuery.addFilterQuery(ObservationDTO.GENE_ACCESSION_ID + ":\"" + mgiAccession + "\" OR "
                + ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":control");
    }/*from   w w w. j  a  v a 2  s  .co m*/
    if (sex != null) {
        solrQuery.addFilterQuery("sex:" + sex.name());
    }
    if (StringUtils.isNotEmpty(parameterStableId)) {
        solrQuery.addFilterQuery(ObservationDTO.PARAMETER_STABLE_ID + ":" + parameterStableId);
    }
    if (StringUtils.isNotEmpty(parameterAssociationValue)) {
        solrQuery.addFilterQuery(
                ObservationDTO.PARAMETER_ASSOCIATION_VALUE + ":" + "\"" + parameterAssociationValue + "\"");//put in quotes for "no expression" query
    }

    if (StringUtils.isNotEmpty(parameterAssociationStableId)) {
        solrQuery.addFilterQuery(ObservationDTO.PARAMETER_ASSOCIATION_STABLE_ID + ":" + "\""
                + parameterAssociationStableId + "\"");//put in quotes for "no expression" query
    }

    solrQuery.setRows(numberOfImagesToRetrieve);
    //group controls and experimental together
    solrQuery.addSort(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP, SolrQuery.ORDER.desc);
    logger.info("solr Query in image service " + solrQuery);
    QueryResponse response = impcImagesCore.query(solrQuery);
    return response;
}

From source file:org.opencb.opencga.storage.core.search.solr.SolrQueryParser.java

License:Apache License

/**
 * Create a SolrQuery object from Query and QueryOptions.
 *
 * @param query         Query// w  w w .  j a  v a2s.c o m
 * @param queryOptions  Query Options
 * @return              SolrQuery
 */
public SolrQuery parse(Query query, QueryOptions queryOptions) {
    List<String> filterList = new ArrayList<>();

    SolrQuery solrQuery = new SolrQuery();

    //-------------------------------------
    // QueryOptions processing
    //-------------------------------------
    if (queryOptions.containsKey(QueryOptions.INCLUDE)) {
        solrQuery.setFields(queryOptions.getAsStringList(QueryOptions.INCLUDE).toString());
    }

    if (queryOptions.containsKey(QueryOptions.LIMIT)) {
        solrQuery.setRows(queryOptions.getInt(QueryOptions.LIMIT));
    }

    if (queryOptions.containsKey(QueryOptions.SKIP)) {
        solrQuery.setStart(queryOptions.getInt(QueryOptions.SKIP));
    }

    if (queryOptions.containsKey(QueryOptions.SORT)) {
        solrQuery.addSort(queryOptions.getString(QueryOptions.SORT), getSortOrder(queryOptions));
    }

    //-------------------------------------
    // Query processing
    //-------------------------------------

    // OR conditions
    // create a list for xrefs (without genes), genes, regions and cts
    // the function classifyIds function differentiates xrefs from genes
    List<String> xrefs = new ArrayList<>();
    List<String> genes = new ArrayList<>();
    List<Region> regions = new ArrayList<>();
    List<String> consequenceTypes = new ArrayList<>();

    // xref
    classifyIds(VariantQueryParams.ANNOT_XREF.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.ID.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.GENE.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.ANNOT_CLINVAR.key(), query, xrefs, genes);
    classifyIds(VariantQueryParams.ANNOT_COSMIC.key(), query, xrefs, genes);
    //        classifyIds(VariantQueryParams.ANNOT_HPO.key(), query, xrefs, genes);

    // Convert region string to region objects
    if (query.containsKey(VariantQueryParams.REGION.key())) {
        regions = Region.parseRegions(query.getString(VariantQueryParams.REGION.key()));
    }

    // consequence types (cts)
    if (query.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())
            && StringUtils.isNotEmpty(query.getString(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()))) {
        consequenceTypes = Arrays
                .asList(query.getString(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()).split("[,;]"));
    }

    // goal: [((xrefs OR regions) AND cts) OR (genes AND cts)] AND ... AND ...
    if (consequenceTypes.size() > 0) {
        if (genes.size() > 0) {
            // consequence types and genes
            String or = buildXrefOrRegionAndConsequenceType(xrefs, regions, consequenceTypes);
            if (xrefs.size() == 0 && regions.size() == 0) {
                // no xrefs or regions: genes AND cts
                filterList.add(buildGeneAndCt(genes, consequenceTypes));
            } else {
                // otherwise: [((xrefs OR regions) AND cts) OR (genes AND cts)]
                filterList.add("(" + or + ") OR (" + buildGeneAndCt(genes, consequenceTypes) + ")");
            }
        } else {
            // consequence types but no genes: (xrefs OR regions) AND cts
            // in this case, the resulting string will never be null, because there are some consequence types!!
            filterList.add(buildXrefOrRegionAndConsequenceType(xrefs, regions, consequenceTypes));
        }
    } else {
        // no consequence types: (xrefs OR regions) but we must add "OR genes", i.e.: xrefs OR regions OR genes
        // no consequence types: (xrefs OR regions) but we must add "OR genMINes", i.e.: xrefs OR regions OR genes
        // we must make an OR with xrefs, genes and regions and add it to the "AND" filter list
        String orXrefs = buildXrefOrGeneOrRegion(xrefs, genes, regions);
        if (!orXrefs.isEmpty()) {
            filterList.add(orXrefs);
        }
    }

    // now we continue with the other AND conditions...
    // type (t)
    String key = VariantQueryParams.STUDIES.key();
    if (isValidParam(query, VariantQueryParams.STUDIES)) {
        try {
            String value = query.getString(key);
            VariantDBAdaptorUtils.QueryOperation op = checkOperator(value);
            Set<Integer> studyIds = new HashSet<>(
                    variantDBAdaptorUtils.getStudyIds(splitValue(value, op), queryOptions));
            List<String> studyNames = new ArrayList<>(studyIds.size());
            Map<String, Integer> map = variantDBAdaptorUtils.getStudyConfigurationManager().getStudies(null);
            if (map != null && map.size() > 1) {
                map.forEach((name, id) -> {
                    if (studyIds.contains(id)) {
                        String[] s = name.split(":");
                        studyNames.add(s[s.length - 1]);
                    }
                });

                if (op == null || op == VariantDBAdaptorUtils.QueryOperation.OR) {
                    filterList.add(parseCategoryTermValue("studies", StringUtils.join(studyNames, ",")));
                } else {
                    filterList.add(parseCategoryTermValue("studies", StringUtils.join(studyNames, ";")));
                }
            }
        } catch (NullPointerException e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    // type (t)
    key = VariantQueryParams.TYPE.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("type", query.getString(key)));
    }

    // Gene biotype
    key = VariantQueryParams.ANNOT_BIOTYPE.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("biotypes", query.getString(key)));
    }

    // protein-substitution
    key = VariantQueryParams.ANNOT_PROTEIN_SUBSTITUTION.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseScoreValue(query.getString(key)));
    }

    // conservation
    key = VariantQueryParams.ANNOT_CONSERVATION.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseScoreValue(query.getString(key)));
    }

    // cadd, functional score
    key = VariantQueryParams.ANNOT_FUNCTIONAL_SCORE.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseScoreValue(query.getString(key)));
    }

    // maf population frequency
    // in the model: "popFreq__1kG_phase3__CLM":0.005319148767739534
    key = VariantQueryParams.ANNOT_POPULATION_MINOR_ALLELE_FREQUENCY.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parsePopValue("popFreq", query.getString(key)));
    }

    // stats maf
    // in the model: "stats__1kg_phase3__ALL"=0.02
    key = VariantQueryParams.STATS_MAF.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parsePopValue("stats", query.getString(key)));
    }

    // GO
    key = VariantQueryParams.ANNOT_GO.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        List<String> gos = Arrays.asList(query.getString(key).split(","));
        Set genesByGo = variantDBAdaptorUtils.getGenesByGo(gos);
        if (genesByGo != null && genesByGo.size() > 0) {
            filterList.add(parseCategoryTermValue("xrefs", StringUtils.join(genesByGo, ",")));
        }
    }

    // hpo
    key = VariantQueryParams.ANNOT_HPO.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("traits", query.getString(key)));
    }

    // clinvar
    key = VariantQueryParams.ANNOT_CLINVAR.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("traits", query.getString(key)));
    }

    // traits
    key = VariantQueryParams.ANNOT_TRAITS.key();
    if (StringUtils.isNotEmpty(query.getString(key))) {
        filterList.add(parseCategoryTermValue("traits", query.getString(key)));
    }

    //-------------------------------------
    // Facet processing
    //-------------------------------------

    if (query.containsKey("facet.field")) {
        solrQuery.addFacetField((query.get("facet.field").toString()));
    }

    if (query.containsKey("facet.fields")) {
        solrQuery.addFacetField((query.get("facet.fields").toString().split(",")));
    }

    if (query.containsKey("facet.query")) {
        solrQuery.addFacetQuery(query.get("facet.query").toString());
    }

    if (query.containsKey("facet.prefix")) {
        solrQuery.setFacetPrefix(query.get("facet.prefix").toString());
    }

    if (query.containsKey("facet.range")) {

        Map<String, Map<String, Number>> rangeFields = (Map<String, Map<String, Number>>) query
                .get("facet.range");

        for (String k : rangeFields.keySet()) {
            Number rangeStart = rangeFields.get(k).get("facet.range.start");
            Number rangeEnd = rangeFields.get(k).get("facet.range.end");
            Number rangeGap = rangeFields.get(k).get("facet.range.gap");
            solrQuery.addNumericRangeFacet(k, rangeStart, rangeEnd, rangeGap);
        }
    }

    logger.debug("query = {}\n", query.toJson());

    solrQuery.setQuery("*:*");
    filterList.forEach(filter -> {
        solrQuery.addFilterQuery(filter);
        logger.debug("Solr fq: {}\n", filter);
    });

    return solrQuery;
}

From source file:org.opencommercesearch.RuleManager.java

License:Apache License

/**
 * Loads the rules that matches the given query
 * //from ww w  . j av a  2s.com
 * @param q is the user query
 * @param categoryPath is the current category path, used to filter out rules (i.e. rule based pages)
 * @param categoryFilterQuery is the current category search token that will be used for filtering out rules and facets
 * @param isSearch indicates if we are browsing or searching the site
 * @param isRuleBasedPage tells whether or not we are on a rule based page
 * @param catalog the current catalog we are browsing/searching
 * @param isOutletPage tells whether or not the current page is outlet
 * @param brandId is the current brand id currently browsed, if any.
 * @throws RepositoryException if an exception happens retrieving a rule from the repository
 * @throws SolrServerException if an exception happens querying the search engine
 */
void loadRules(String q, String categoryPath, String categoryFilterQuery, boolean isSearch,
        boolean isRuleBasedPage, RepositoryItem catalog, boolean isOutletPage, String brandId,
        Set<String> includeExperiments, Set<String> excludeExperiments)
        throws RepositoryException, SolrServerException {
    if (isSearch && StringUtils.isBlank(q)) {
        throw new IllegalArgumentException("Missing query");
    }

    SolrQuery query = new SolrQuery("*:*");
    query.setStart(DEFAULT_START);
    query.setRows(DEFAULT_ROWS);
    query.addSort(FIELD_SORT_PRIORITY, ORDER.asc);
    query.addSort(FIELD_SCORE, ORDER.asc);
    query.addSort(FIELD_ID, ORDER.asc);
    query.add(CommonParams.FL, FIELD_ID, FIELD_BOOST_FUNCTION, FIELD_FACET_FIELD, FIELD_COMBINE_MODE,
            FIELD_QUERY, FIELD_CATEGORY, FIELD_EXPERIMENTAL);

    StringBuilder reusableStringBuilder = new StringBuilder();
    query.addFilterQuery(getTargetFilter(reusableStringBuilder, isSearch, q));
    query.addFilterQuery(getCategoryFilter(reusableStringBuilder, categoryFilterQuery, categoryPath));
    query.addFilterQuery(getSiteFilter(reusableStringBuilder, catalog));
    query.addFilterQuery(getBrandFilter(reusableStringBuilder, brandId));
    query.addFilterQuery(getSubTargetFilter(reusableStringBuilder, isOutletPage));

    StringBuilder catalogFilter = reuseStringBuilder(reusableStringBuilder);
    catalogFilter.append("catalogId:").append(WILDCARD).append(" OR ").append("catalogId:")
            .append(catalog.getRepositoryId());
    query.addFilterQuery(catalogFilter.toString());

    //Notice how the current datetime (NOW wildcard on Solr) is rounded to days (NOW/DAY). This allows filter caches
    //to be reused and hopefully improve performance. If you don't round to day, NOW is very precise (up to milliseconds); so every query
    //would need a new entry on the filter cache...
    //Also, notice that NOW/DAY is midnight from last night, and NOW/DAY+1DAY is midnight today.
    //The below query is intended to match rules with null start or end dates, or start and end dates in the proper range.
    query.addFilterQuery(
            "-(((startDate:[* TO *]) AND -(startDate:[* TO NOW/DAY+1DAY])) OR (endDate:[* TO *] AND -endDate:[NOW/DAY+1DAY TO *]))");

    int queryTime = 0;
    QueryResponse res = server.query(query);
    queryTime += res.getQTime();

    if (res.getResults() == null || res.getResults().getNumFound() == 0) {
        rules = Collections.emptyMap();
        loadRulesTime = queryTime;
        return;
    }

    rules = new HashMap<String, List<RepositoryItem>>(RuleType.values().length);
    ruleDocs = new HashMap<String, SolrDocument>();
    SolrDocumentList docs = res.getResults();
    int total = (int) docs.getNumFound();
    int processed = 0;
    while (processed < total) {
        for (int i = 0; i < docs.size(); i++) {
            ++processed;
            SolrDocument doc = docs.get(i);

            if (isSearch && !matchesQuery(q, doc)) {
                // skip this rule
                continue;
            }

            RepositoryItem rule = searchRepository.getItem((String) doc.getFieldValue("id"),
                    SearchRepositoryItemDescriptor.RULE);

            //for rule based categories, include all facet rules and ranking rules of only that category
            if (rule != null) {

                if (excludeExperiments.contains(rule.getRepositoryId())) {
                    continue;
                }

                Boolean experimental = (Boolean) doc.getFieldValue(FIELD_EXPERIMENTAL);
                if (experimental != null && experimental
                        && !includeExperiments.contains(rule.getRepositoryId())) {
                    continue;
                }

                String ruleType = (String) rule.getPropertyValue(RuleProperty.RULE_TYPE);
                if (ruleType.equals(RuleProperty.TYPE_FACET_RULE)) {
                    buildRuleLists(ruleType, rule, doc);
                } else {
                    if (categoryPath != null && isRuleBasedPage) {
                        List<String> ruleCategories = (List<String>) doc.getFieldValue(FIELD_CATEGORY);
                        if (ruleCategories != null) {
                            if (ruleCategories.contains(categoryPath)) {
                                buildRuleLists(ruleType, rule, doc);
                            }
                        }
                    } else {
                        buildRuleLists(ruleType, rule, doc);
                    }
                }
            } else {
                //TODO gsegura: add logging that we couldn't find the rule item in the DB
            }
        }
        if (processed < total) {
            query.setStart(processed);
            res = server.query(query);
            queryTime += res.getQTime();
            docs = res.getResults();
        }
    }

    loadRulesTime = queryTime;
}

From source file:org.phenotips.variantstore.db.solr.SolrUtils.java

License:Open Source License

/**
 * Loop over all the documents returned by the query. This method queries the DB multiple times. Every time we get
 * data back, we pass it onto a processor, and stop processing data if the processor tells us it's had enough.
 *
 * @param server    the solr db/*from  www.j  a v a 2  s .  co m*/
 * @param q         the query
 * @param uniqueKey the solr uniqueKey field to sort on. Required for solr's Cursor functionality.
 *@param processor the processor to handle the data. If the function returns true, we stop fetching more data.
 *  @throws IOException
 * @throws SolrServerException
 */
static void processAllDocs(SolrClient server, SolrQuery q, String uniqueKey,
        Function<Collection<SolrDocument>, Boolean> processor) throws IOException, SolrServerException {
    boolean done = false;
    String oldCursorMark;
    String cursorMark = CursorMarkParams.CURSOR_MARK_START;
    QueryResponse resp;

    // Cursor functionality requires a sort containing a uniqueKey field tie breaker
    q.addSort(uniqueKey, SolrQuery.ORDER.desc);

    while (!done) {
        oldCursorMark = cursorMark;
        q.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
        resp = server.query(q);
        done = processor.apply(resp.getResults());
        cursorMark = resp.getNextCursorMark();
        done = done || oldCursorMark.equals(cursorMark);
    }
}

From source file:org.phenotips.vocabulary.internal.solr.OncoTree.java

License:Open Source License

/**
 * Adds dynamic solr query parameters to {@code query}, based on the received {@code rawQuery raw query string},
 * {@code rows the maximum number of results to return}, {@code sort the sorting order}, and {@code customFilter a
 * custom filter}.//from   w w w . j av a2 s. co  m
 *
 * @param rawQuery unprocessed query string
 * @param rows the maximum number of search items to return
 * @param sort the optional sort parameter
 * @param customFilter custom filter for the results
 * @param query a {@link SolrQuery solr query} object
 * @return the updated {@link SolrQuery solr query} object
 */
@Nonnull
private SolrQuery addDynamicQueryParam(@Nonnull final String rawQuery, @Nonnull final Integer rows,
        @Nullable final String sort, @Nullable final String customFilter, @Nonnull SolrQuery query) {
    final String queryString = rawQuery.trim();
    final String escapedQuery = ClientUtils.escapeQueryChars(queryString);
    if (StringUtils.isNotBlank(customFilter)) {
        query.setFilterQueries(customFilter);
    }
    query.setQuery(escapedQuery);
    query.set(SpellingParams.SPELLCHECK_Q, queryString);
    final String lastWord = StringUtils.defaultIfBlank(
            StringUtils.substringAfterLast(escapedQuery, StringUtils.SPACE), escapedQuery) + "*";
    query.set(DisMaxParams.BQ, String.format("nameSpell:%1$s^20 text:%1$s^1 textSpell:%1$s^2", lastWord));
    query.setRows(rows);
    if (StringUtils.isNotBlank(sort)) {
        for (final String sortItem : sort.split("\\s*,\\s*")) {
            query.addSort(StringUtils.substringBefore(sortItem, StringUtils.SPACE),
                    sortItem.endsWith(" desc") || sortItem.startsWith("-") ? SolrQuery.ORDER.desc
                            : SolrQuery.ORDER.asc);
        }
    }
    return query;
}