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

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

Introduction

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

Prototype

public SolrQuery setFacetMinCount(int cnt) 

Source Link

Document

set facet minimum count

Usage

From source file:uk.ac.ebi.atlas.solr.query.SpeciesLookupService.java

License:Apache License

public Result fetchSpeciesForGeneSet(String term) {
    // eg: property_value_lower:"IPR027417"
    String queryText = PROPERTY_LOWER_FIELD + ":" + encloseInQuotes(term)
            + " AND property_name:(pathwayid OR go OR po OR interpro OR REACT_303889)"; // Needed to exclude Entrez numerical ids, identical to Plant Reactome ids (pathwayid)
    LOGGER.debug("fetch species for geneset " + queryText);

    SolrQuery query = new SolrQuery(queryText);

    query.addFacetField(SPECIES_FIELD);/*from   w  w w . j a  v a  2  s .c o m*/
    query.setRows(0);
    query.setFacet(true);

    // just get first 2 species for performance reasons, we only need to know if multi-species exist, not what they are
    query.setFacetLimit(2);
    query.setFacetMinCount(1);

    QueryResponse solrResponse = solrServer.query(query);
    ImmutableSet<String> species = SolrUtil.extractFirstFacetValues(solrResponse);

    return new Result(species);
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.IntactSolrSearcher.java

License:Apache License

public Multimap<String, InteractorIdCount> searchInteractors(SolrQuery originalQuery,
        IntactFacetField[] intactFacetFields) throws IntactSolrException {
    SolrQuery query = originalQuery.getCopy();
    query.setRows(0);/*from   w  ww. j  av  a2 s  . c  o  m*/

    // we allow faceting
    query.setFacet(true);

    // we want all the facet fields with min count = 1. The facet fields with count = 0 are not interesting
    query.setFacetMinCount(1);

    Multimap<String, InteractorIdCount> interactors = HashMultimap.create();

    if (intactFacetFields != null) {

        // we sort the results : the biggest count first
        query.setFacetSort(FacetParams.FACET_SORT_COUNT);

        for (IntactFacetField facetField : intactFacetFields) {
            final String fieldName = createFieldName(facetField.getFieldName());

            // important optimization. We don't want to return all the fields, only a certain number for pagination
            if (facetField.getFirst() != null) {
                query.set("f." + fieldName + ".facet.offset", facetField.getFirst());
            }
            if (facetField.getMax() != null) {
                query.set("f." + fieldName + ".facet.limit", facetField.getMax());
            }

            query.addFacetField(fieldName);
        }
    }

    QueryResponse queryResponse = executeQuery(query);

    List<FacetField> facetFields = queryResponse.getFacetFields();

    if (facetFields == null || facetFields.isEmpty()) {
        return interactors;
    }

    for (FacetField ff : facetFields) {
        if (ff != null && ff.getValues() != null) {
            for (FacetField.Count c : ff.getValues()) {
                interactors.put(extractInteractorTypeFromFieldName(ff.getName()),
                        new InteractorIdCount(c.getName(), c.getCount()));
            }
        }
    }

    return interactors;
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.IntactSolrSearcher.java

License:Apache License

public Map<String, Integer> countAllInteractors(SolrQuery originalQuery, String[] interactorTypeMis)
        throws IntactSolrException {
    boolean endOfFacetResults = false;

    int firstResults = 0;

    Map<String, Integer> results = new HashMap<String, Integer>();

    while (!endOfFacetResults) {
        int chunkResults = 0;

        SolrQuery query = originalQuery.getCopy();
        query.setRows(0);/*from ww w . j  a  va 2  s.  c o  m*/

        // we allow faceting
        query.setFacet(true);

        // we want all the facet fields with min count = 1. The facet fields with count = 0 are not interesting
        query.setFacetMinCount(1);

        // important optimization. We don't want to return all the fields, only a certain number for pagination
        query.set(FacetParams.FACET_OFFSET, firstResults);
        query.setFacetLimit(CHUNK_FACET_THRESHOLD);

        // we sort the results : the biggest count first
        query.setFacetSort(FacetParams.FACET_SORT_COUNT);

        for (String mi : interactorTypeMis) {
            final String fieldName = createFieldName(mi);

            query.addFacetField(fieldName);
        }

        QueryResponse queryResponse = executeQuery(query);

        List<FacetField> facetFields = queryResponse.getFacetFields();
        if (facetFields == null || facetFields.isEmpty()) {
            endOfFacetResults = true;
        } else {
            for (FacetField facetField : facetFields) {
                if (facetField.getValueCount() > 0) {
                    chunkResults += facetField.getValueCount();

                    if (results.containsKey(facetField.getName())) {
                        int current = results.get(facetField.getName());
                        results.put(facetField.getName(), current + facetField.getValueCount());
                    } else {
                        results.put(facetField.getName(), facetField.getValueCount());
                    }
                } else if (!results.containsKey(facetField.getName())) {
                    results.put(facetField.getName(), 0);
                }
            }

            if (chunkResults < CHUNK_FACET_THRESHOLD) {
                endOfFacetResults = true;
            }
        }

        firstResults += CHUNK_FACET_THRESHOLD;
    }

    return results;
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.solr.ontology.OntologySearcher.java

License:Apache License

public Set<String> getOntologyNames() throws SolrServerException {
    if (ontologyNames == null) {
        SolrQuery query = new SolrQuery("*:*");
        query.setStart(0);/* ww  w . jav a  2s.  com*/
        query.setRows(0);

        // prepare faceting
        query.setFacet(true);
        query.setParam(FacetParams.FACET_FIELD, "ontology");
        query.setFacetLimit(10);
        query.setFacetMinCount(1);
        query.set(FacetParams.FACET_OFFSET, 0);

        // order by unique id
        query.addSortField(OntologyFieldNames.ID, SolrQuery.ORDER.asc);

        QueryResponse response = search(query, null);
        FacetField facetField = response.getFacetField("ontology");

        if (facetField.getValues() == null) {
            return Collections.EMPTY_SET;
        }

        ontologyNames = new HashSet<String>(facetField.getValues().size());

        for (FacetField.Count c : facetField.getValues()) {
            ontologyNames.add(c.getName());
        }
    }

    return ontologyNames;
}

From source file:uk.ac.ebi.phenotype.imaging.springrest.images.dao.ImagesSolrJ.java

License:Apache License

private QueryResponse runFacetQuery(String query, String facetField, int start, int length, String filterQuery)
        throws SolrServerException {

    SolrQuery solrQuery = new SolrQuery();
    //   System.out.println("facet solr query=" + query);
    solrQuery.setQuery(query);/*ww w.ja  v  a2s  .  c  o m*/
    solrQuery.setStart(start);
    solrQuery.setRows(length);
    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(1);
    solrQuery.addFacetField(facetField);
    if (filterQuery != "") {
        solrQuery.addFilterQuery(filterQuery);
    }
    //   System.out.println("exp facet query="+solrQuery.toString());
    return server.query(solrQuery);
}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

License:Apache License

public List<String> getGenesWithMoreProcedures(int n, ArrayList<String> resourceName)
        throws SolrServerException, InterruptedException, ExecutionException {
    List<String> genes = new ArrayList<>();
    SolrQuery q = new SolrQuery();

    if (resourceName != null) {
        q.setQuery(ObservationDTO.DATASOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + ObservationDTO.DATASOURCE_NAME + ":"));
    } else {//from ww  w .  j  a  va 2 s .c o m
        q.setQuery("*:*");
    }

    String geneProcedurePivot = ObservationDTO.GENE_SYMBOL + "," + ObservationDTO.PROCEDURE_NAME;

    q.add("facet.pivot", geneProcedurePivot);

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

    System.out.println(
            "Solr url for getOverviewGenesWithMoreProceduresThan " + solr.getBaseURL() + "/select?" + q);
    QueryResponse response = solr.query(q);

    for (PivotField pivot : response.getFacetPivot().get(geneProcedurePivot)) {
        if (pivot.getPivot().size() >= n) {
            genes.add(pivot.getValue().toString());
        }
    }

    return genes;
}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

License:Apache License

public Map<String, Set<String>> getColoniesByPhenotypingCenter(ArrayList<String> resourceName,
        ZygosityType zygosity) throws SolrServerException, InterruptedException {

    Map<String, Set<String>> res = new HashMap<>();
    SolrQuery q = new SolrQuery();
    String pivotFacet = ObservationDTO.PHENOTYPING_CENTER + "," + ObservationDTO.COLONY_ID;
    NamedList<List<PivotField>> response;

    if (resourceName != null) {
        q.setQuery(ObservationDTO.DATASOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + ObservationDTO.DATASOURCE_NAME + ":"));
    } else {//from   ww w.j a  v  a 2s  .c om
        q.setQuery("*:*");
    }

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

    q.addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":experimental");
    q.addFacetPivotField(pivotFacet);
    q.setFacet(true);
    q.setFacetLimit(-1);
    q.setFacetMinCount(1);
    q.setRows(0);

    try {
        response = solr.query(q).getFacetPivot();
        for (PivotField genePivot : response.get(pivotFacet)) {
            String center = genePivot.getValue().toString();
            HashSet<String> colonies = new HashSet<>();
            for (PivotField f : genePivot.getPivot()) {
                colonies.add(f.getValue().toString());
            }
            res.put(center, colonies);
        }
    } catch (SolrServerException e) {
        e.printStackTrace();
    }
    return res;
}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

License:Apache License

public Set<String> getAllGeneIdsByResource(List<String> resourceName, boolean experimentalOnly) {

    SolrQuery q = new SolrQuery();
    q.setFacet(true);//from w  ww  . j a  va2s  . com
    q.setFacetMinCount(1);
    q.setFacetLimit(-1);
    q.setRows(0);
    q.addFacetField(ObservationDTO.GENE_ACCESSION_ID);
    if (resourceName != null) {
        q.setQuery(ObservationDTO.DATASOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + ObservationDTO.DATASOURCE_NAME + ":"));
    } else {
        q.setQuery("*:*");
    }

    if (experimentalOnly) {
        q.addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":experimental");
    }

    LOG.info("Solr URL getAllGeneIdsByResource " + solr.getBaseURL() + "/select?" + q);
    try {
        return getFacets(solr.query(q)).get(ObservationDTO.GENE_ACCESSION_ID).keySet();
    } catch (SolrServerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:uk.ac.ebi.phenotype.service.ObservationService.java

License:Apache License

public Set<String> getAllColonyIdsByResource(List<String> resourceName, boolean experimentalOnly) {

    SolrQuery q = new SolrQuery();
    q.setFacet(true);/*from  www .  jav  a2  s.c o  m*/
    q.setFacetMinCount(1);
    q.setFacetLimit(-1);
    q.setRows(0);
    q.addFacetField(ObservationDTO.COLONY_ID);

    if (resourceName != null) {
        q.setQuery(ObservationDTO.DATASOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + ObservationDTO.DATASOURCE_NAME + ":"));
    } else {
        q.setQuery("*:*");
    }

    if (experimentalOnly) {
        q.addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":experimental");
    }

    LOG.info("Solr URL getAllColonyIdsByResource " + solr.getBaseURL() + "/select?" + q);
    try {
        return getFacets(solr.query(q)).get(ObservationDTO.COLONY_ID).keySet();
    } catch (SolrServerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:uk.ac.ebi.phenotype.service.StatisticalResultService.java

License:Apache License

public Map<String, Long> getColoniesNoMPHit(ArrayList<String> resourceName, ZygosityType zygosity)
        throws SolrServerException {
    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 {/*from  www  . ja v  a  2 s .c  o  m*/
        q.setQuery("*:*");
    }

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

    q.addFilterQuery(GenotypePhenotypeDTO.P_VALUE + ":[" + this.P_VALUE_THRESHOLD + " TO 1]");

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

    System.out.println("Solr url for getColoniesNoMPHit " + solr.getBaseURL() + "/select?" + q);
    QueryResponse response = solr.query(q);

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

    System.out.println("Done in " + (System.currentTimeMillis() - time));
    return res;

}