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

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

Introduction

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

Prototype

public SolrQuery addFacetField(String... fields) 

Source Link

Document

Add field(s) for facet computation.

Usage

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

License:Apache License

public Map<String, Set<String>> getFacets(String anatomyId) throws SolrServerException, IOException {
    Map<String, Set<String>> res = new HashMap<>();
    SolrQuery query = getBasicExpressionQuery(anatomyId); // only have expressed and
    // not expressed ingnore
    // ambiguous and no tissue
    //query.addFilterQuery("(" + ImageDTO.PARAMETER_ASSOCIATION_VALUE + ":\"no expression\" OR " + ImageDTO.PARAMETER_ASSOCIATION_VALUE
    //      + ":\"expression\"" + ")");
    query.setFacet(true);//from   ww w.  ja  v  a 2 s .  c  om
    query.setFacetLimit(-1);
    query.setFacetMinCount(1);
    query.addFacetField(ObservationDTO.ANATOMY_TERM);
    query.addFacetField(ObservationDTO.PHENOTYPING_CENTER);
    query.addFacetField(ObservationDTO.PROCEDURE_NAME);
    query.addFacetField(ObservationDTO.CATEGORY);

    QueryResponse response = experimentCore.query(query);
    for (FacetField facetField : response.getFacetFields()) {
        Set<String> filter = new TreeSet<>();
        for (Count facet : facetField.getValues()) {
            filter.add(facet.getName());

        }

        res.put(facetField.getName(), filter);
    }

    return res;
}

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

License:Apache License

/**
 * /*w  w  w.  ja va  2s  .  c om*/
 * @param geneIds
 * @return Number of genes (from the provided list) in each status of interest.
 */
public HashMap<String, Long> getStatusCount(Set<String> geneIds) {

    HashMap<String, Long> res = new HashMap<>();

    // build query for these genes
    String geneQuery = GeneDTO.MGI_ACCESSION_ID + ":(" + StringUtils.join(geneIds, " OR ").replace(":", "\\:")
            + ")";

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(geneQuery).setRows(1).setFacet(true);
    QueryResponse solrResponse;
    try {
        // add facet for latest_project_status 
        solrQuery.addFacetField(GeneDTO.LATEST_ES_CELL_STATUS);
        solrResponse = geneCore.query(solrQuery);
        // put all values in the hash
        for (Count c : solrResponse.getFacetField(GeneDTO.LATEST_ES_CELL_STATUS).getValues()) {
            res.put(c.getName(), c.getCount());
        }

        // add facet latest_es_cell_status
        solrQuery.removeFacetField(GeneDTO.LATEST_ES_CELL_STATUS);
        solrResponse = geneCore.query(solrQuery.addFacetField(GeneDTO.LATEST_PROJECT_STATUS));
        // put all values in the hash
        for (Count c : solrResponse.getFacetField(GeneDTO.LATEST_PROJECT_STATUS).getValues()) {
            res.put(c.getName(), c.getCount());
        }
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }

    return res;
}

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

License:Apache License

/**
 *
 * @param geneIds the input set of gene ids (e.g. idg genes)
 * @param statusField the status field/*from  w  w  w  .j a v a2  s. c  om*/
 * @return Number of genes (from the provided list) in each status of interest.
 */
public HashMap<String, Long> getStatusCount(Set<String> geneIds, String statusField) {

    HashMap<String, Long> res = new HashMap<>();
    SolrQuery solrQuery = new SolrQuery();
    QueryResponse solrResponse;

    if (geneIds != null) {
        String geneQuery = GeneDTO.MGI_ACCESSION_ID + ":("
                + StringUtils.join(geneIds, " OR ").replace(":", "\\:") + ")";
        solrQuery.setQuery(geneQuery);
    } else {
        solrQuery.setQuery("*:*");
    }
    solrQuery.setRows(1);
    solrQuery.setFacet(true);
    solrQuery.setFacetLimit(-1);
    try {
        solrQuery.addFacetField(statusField);
        solrResponse = geneCore.query(solrQuery);
        for (Count c : solrResponse.getFacetField(statusField).getValues()) {
            res.put(c.getName(), c.getCount());
        }
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }

    return res;
}

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

License:Apache License

/**
 * @param zygosity - optional (pass null if not needed)
 * @return Map <String, Long> : <top_level_mp_name, number_of_annotations>
 * @author tudose/* w w  w . j  a  v  a2 s.  c  o m*/
 */
public TreeMap<String, Long> getDistributionOfAnnotationsByMPTopLevel(ZygosityType zygosity,
        String resourceName) {

    SolrQuery query = new SolrQuery();

    if (zygosity != null) {
        query.setQuery(GenotypePhenotypeDTO.ZYGOSITY + ":" + zygosity.getName());
    } else if (resourceName != null) {
        query.setFilterQueries(GenotypePhenotypeDTO.RESOURCE_NAME + ":" + resourceName);
    } else {
        query.setQuery("*:*");
    }

    query.setFacet(true);
    query.setFacetLimit(-1);
    query.setFacetMinCount(1);
    query.setRows(0);
    query.addFacetField(GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME);

    try {
        QueryResponse response = genotypePhenotypeCore.query(query);
        TreeMap<String, Long> res = new TreeMap<>();
        res.putAll(getFacets(response).get(GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME));
        return res;
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }
    return null;
}

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

License:Apache License

public Map<String, Long> getHitsDistributionBySomethingNoIds(String fieldToDistributeBy,
        List<String> resourceName, ZygosityType zygosity, int facetMincount, Double maxPValue)
        throws SolrServerException, IOException, InterruptedException, ExecutionException {

    Map<String, Long> res = new HashMap<>();
    Long time = System.currentTimeMillis();
    SolrQuery q = new SolrQuery();

    if (resourceName != null) {
        q.setQuery(GenotypePhenotypeDTO.RESOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + GenotypePhenotypeDTO.RESOURCE_NAME + ":"));
    } else {/* w  w  w .  ja va2 s .  c o m*/
        q.setQuery("*:*");
    }

    if (zygosity != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.ZYGOSITY + ":" + zygosity.name());
    }

    if (maxPValue != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.P_VALUE + ":[0 TO " + maxPValue + "]");
    }

    q.addFacetField(fieldToDistributeBy);
    q.setFacetMinCount(facetMincount);
    q.setFacet(true);
    q.setRows(1);
    q.set("facet.limit", -1);

    logger.info("Solr url for getHitsDistributionByParameter " + SolrUtils.getBaseURL(genotypePhenotypeCore)
            + "/select?" + q);
    QueryResponse response = genotypePhenotypeCore.query(q);

    for (Count facet : response.getFacetField(fieldToDistributeBy).getValues()) {
        String value = facet.getName();
        long count = facet.getCount();
        res.put(value, count);
    }

    logger.info("Done in " + (System.currentTimeMillis() - time));
    return res;

}

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

License:Apache License

public PhenotypeFacetResult getMPByGeneAccessionAndFilter(String accId, List<String> topLevelMpTermName,
        List<String> resourceFullname) throws IOException, URISyntaxException, JSONException {

    String solrUrl = SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select?";
    SolrQuery q = new SolrQuery();

    q.setQuery(GenotypePhenotypeDTO.MARKER_ACCESSION_ID + ":\"" + accId + "\"");
    q.setRows(10000000);//from   w ww.ja v  a2 s  .c om
    q.setFacet(true);
    q.setFacetMinCount(1);
    q.setFacetLimit(-1);
    q.addFacetField(GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME);
    //q.addFacetField(GenotypePhenotypeDTO.RESOURCE_FULLNAME);
    q.set("wt", "json");
    q.setSort(GenotypePhenotypeDTO.P_VALUE, ORDER.asc);

    if (topLevelMpTermName != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME + ":(\""
                + StringUtils.join(topLevelMpTermName, "\" OR \"") + "\")");
    }

    solrUrl += q;

    return createPhenotypeResultFromSolrResponse(solrUrl);
}

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

License:Apache License

public PhenotypeFacetResult getMPCallByMPAccessionAndFilter(String phenotype_id, List<String> procedureName,
        List<String> markerSymbol, List<String> mpTermName, Map<String, Synonym> synonyms)
        throws IOException, URISyntaxException, JSONException {

    String url = SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select/?";
    SolrQuery q = new SolrQuery();

    q.setQuery("*:*");
    q.addFilterQuery("(" + GenotypePhenotypeDTO.MP_TERM_ID + ":\"" + phenotype_id + "\" OR "
            + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + phenotype_id + "\" OR "
            + GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + phenotype_id + "\")");
    q.setRows(10000000);// ww  w  . j av a  2 s  . c om
    q.setFacet(true);
    q.setFacetMinCount(1);
    q.setFacetLimit(-1);
    q.addFacetField(GenotypePhenotypeDTO.PROCEDURE_NAME);
    q.addFacetField(GenotypePhenotypeDTO.MARKER_SYMBOL);
    q.addFacetField(GenotypePhenotypeDTO.MP_TERM_NAME);
    q.set("wt", "json");
    q.setSort(GenotypePhenotypeDTO.P_VALUE, ORDER.asc);

    q.setFields(GenotypePhenotypeDTO.MP_TERM_NAME, GenotypePhenotypeDTO.MP_TERM_ID,
            GenotypePhenotypeDTO.MPATH_TERM_NAME, GenotypePhenotypeDTO.MPATH_TERM_ID,
            GenotypePhenotypeDTO.EXTERNAL_ID, GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID,
            GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME, GenotypePhenotypeDTO.ALLELE_SYMBOL,
            GenotypePhenotypeDTO.PHENOTYPING_CENTER, GenotypePhenotypeDTO.ALLELE_ACCESSION_ID,
            GenotypePhenotypeDTO.MARKER_SYMBOL, GenotypePhenotypeDTO.MARKER_ACCESSION_ID,
            GenotypePhenotypeDTO.PHENOTYPING_CENTER, GenotypePhenotypeDTO.ZYGOSITY, GenotypePhenotypeDTO.SEX,
            GenotypePhenotypeDTO.LIFE_STAGE_NAME, GenotypePhenotypeDTO.RESOURCE_NAME,
            GenotypePhenotypeDTO.PARAMETER_STABLE_ID, GenotypePhenotypeDTO.PARAMETER_NAME,
            GenotypePhenotypeDTO.PIPELINE_STABLE_ID, GenotypePhenotypeDTO.PROJECT_NAME,
            GenotypePhenotypeDTO.PROJECT_EXTERNAL_ID, GenotypePhenotypeDTO.P_VALUE,
            GenotypePhenotypeDTO.EFFECT_SIZE, GenotypePhenotypeDTO.PROCEDURE_STABLE_ID,
            GenotypePhenotypeDTO.PROCEDURE_NAME, GenotypePhenotypeDTO.PIPELINE_NAME);

    if (procedureName != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.PROCEDURE_NAME + ":(\""
                + StringUtils.join(procedureName, "\" OR \"") + "\")");
    }
    if (markerSymbol != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.MARKER_SYMBOL + ":(\""
                + StringUtils.join(markerSymbol, "\" OR \"") + "\")");
    }
    if (mpTermName != null) {
        q.addFilterQuery(
                GenotypePhenotypeDTO.MP_TERM_NAME + ":(\"" + StringUtils.join(mpTermName, "\" OR \"") + "\")");
    }

    url += q;

    return createPhenotypeResultFromSolrResponse(url, synonyms);

}

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

License:Apache License

/**
 * Get a list of gene symbols for this phenotype
 * @param phenotype_id/*  w w w  . j a va2  s  .  c o  m*/
 * @return
 * @throws IOException
 * @throws URISyntaxException
 * @throws SolrServerException
 */
public List<String> getGenesForMpId(String phenotype_id)
        throws IOException, URISyntaxException, SolrServerException {
    List<String> results = new ArrayList<>();
    String url = SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select/?";
    SolrQuery q = new SolrQuery();

    q.setQuery("*:*");
    q.addFilterQuery("(" + GenotypePhenotypeDTO.MP_TERM_ID + ":\"" + phenotype_id + "\" OR "
            + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + phenotype_id + "\" OR "
            + GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + phenotype_id + "\")");
    q.setRows(10000000);
    q.setFacet(true);
    q.setFacetMinCount(1);
    q.setFacetLimit(-1);
    //q.addFacetField(GenotypePhenotypeDTO.MARKER_ACCESSION_ID);
    q.addFacetField(GenotypePhenotypeDTO.MARKER_SYMBOL);
    // q.addFacetField(GenotypePhenotypeDTO.MP_TERM_NAME );
    q.set("wt", "json");
    q.setSort(GenotypePhenotypeDTO.P_VALUE, ORDER.asc);
    q.setFields(GenotypePhenotypeDTO.MARKER_SYMBOL);
    //               q.setFields(GenotypePhenotypeDTO.MP_TERM_NAME, GenotypePhenotypeDTO.MP_TERM_ID, GenotypePhenotypeDTO.MPATH_TERM_NAME, GenotypePhenotypeDTO.MPATH_TERM_ID, GenotypePhenotypeDTO.EXTERNAL_ID,
    //                     GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID, GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME, GenotypePhenotypeDTO.ALLELE_SYMBOL, 
    //                     GenotypePhenotypeDTO.PHENOTYPING_CENTER, GenotypePhenotypeDTO.ALLELE_ACCESSION_ID, GenotypePhenotypeDTO.MARKER_SYMBOL,
    //                     GenotypePhenotypeDTO.MARKER_ACCESSION_ID, GenotypePhenotypeDTO.PHENOTYPING_CENTER, GenotypePhenotypeDTO.ZYGOSITY, 
    //                     GenotypePhenotypeDTO.SEX, GenotypePhenotypeDTO.LIFE_STAGE_NAME, GenotypePhenotypeDTO.RESOURCE_NAME, GenotypePhenotypeDTO.PARAMETER_STABLE_ID, GenotypePhenotypeDTO.PARAMETER_NAME,
    //                     GenotypePhenotypeDTO.PIPELINE_STABLE_ID, GenotypePhenotypeDTO.PROJECT_NAME, GenotypePhenotypeDTO.PROJECT_EXTERNAL_ID,
    //                     GenotypePhenotypeDTO.P_VALUE, GenotypePhenotypeDTO.EFFECT_SIZE, GenotypePhenotypeDTO.PROCEDURE_STABLE_ID,
    //                     GenotypePhenotypeDTO.PROCEDURE_NAME, GenotypePhenotypeDTO.PIPELINE_NAME);

    QueryResponse response = genotypePhenotypeCore.query(q);
    for (Count facet : response.getFacetField(GenotypePhenotypeDTO.MARKER_SYMBOL).getValues()) {
        //System.out.println("facet="+facet.getName());
        results.add(facet.getName());
    }
    return results;
}

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

License:Apache License

/**
 * @param mpTermName//from  w w w  .j a  v  a 2 s  .com
 * @param resource
 * @return map <colony_id, occurences>
 */
public Map<String, Long> getAssociationsDistribution(String mpTermName, String resource) {

    String query = GenotypePhenotypeDTO.MP_TERM_NAME + ":\"" + mpTermName + "\"";
    if (resource != null) {
        query += " AND " + GenotypePhenotypeDTO.RESOURCE_NAME + ":" + resource;
    }

    SolrQuery q = new SolrQuery();
    q.setQuery(query);
    q.setFacet(true);
    q.setFacetMinCount(1);
    q.addFacetField(GenotypePhenotypeDTO.COLONY_ID);

    try {
        return (getFacets(genotypePhenotypeCore.query(q))).get(GenotypePhenotypeDTO.COLONY_ID);
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }

    return null;
}

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

License:Apache License

public Set<String> getFertilityAssociatedMps() {

    SolrQuery q = new SolrQuery();
    q.setQuery(GenotypePhenotypeDTO.PARAMETER_STABLE_ID + ":*_FER_*");
    q.setFacet(true);//from  w  w  w .j a v  a 2 s.c o  m
    q.setFacetMinCount(1);
    q.addFacetField(GenotypePhenotypeDTO.MP_TERM_NAME);

    try {
        return (getFacets(genotypePhenotypeCore.query(q))).get(GenotypePhenotypeDTO.MP_TERM_NAME).keySet();
    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }

    return null;
}