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.GenotypePhenotypeService.java

License:Apache License

/**
 * The query differs depending on topLevelMpTerms. If at least one term is passed, we need to find out the list of
 * genes that have the required phenotypes and use them in the query. We can't use the list of MP terms themselves
 * as t will filter out the other phenotype associations for the genese we're interested in.
 * @param topLevelMpTerms/*from  www. j  a v  a  2  s.  c o  m*/
 * @author ilinca
 * @return Solr query for g-p associations of genes that have all phenotypes passed in topLevelMpTerms.
 * @throws IOException
 * @throws SolrServerException
 */
private SolrQuery getPleiotropyQuery(List<String> topLevelMpTerms, Boolean idg, String idgClass)
        throws IOException, SolrServerException, SQLException {

    String pivot = GenotypePhenotypeDTO.MARKER_SYMBOL + "," + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME;
    SolrQuery query = new SolrQuery().setQuery("*:*").setFacet(true).setFacetLimit(-1);
    query.add("facet.pivot", pivot);
    query.addFacetField(GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME);

    if (idg != null && idg) {
        Set<GenesSecondaryProject> projectBeans = genesSecondaryProjectRepository
                .getAllBySecondaryProjectIdAndGroupLabel("idg", idgClass);
        query.addFilterQuery(
                GenotypePhenotypeDTO.MARKER_ACCESSION_ID + ":(\"" + projectBeans.stream().map(gene -> {
                    return gene.getMgiGeneAccessionId();
                }).collect(Collectors.joining("\" OR \"")) + "\")");
    }

    if (topLevelMpTerms != null) {

        // We want data for genes that have ALL top level phenotypes in the list
        String interimPivot = GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME + ","
                + GenotypePhenotypeDTO.MARKER_SYMBOL;
        SolrQuery interimQuery = new SolrQuery().setFacet(true).setFacetLimit(-1).setQuery("*:*")
                .addFilterQuery(topLevelMpTerms.stream().collect(Collectors.joining("\" OR \"",
                        GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_NAME + ":(\"", "\")")));
        interimQuery.add("facet.pivot", interimPivot);

        // Filter out the pivot facets for un-wanted MP top level terms. We can get other top levels in the facets due to multiple parents.
        Map<String, Set<String>> genesByMpTopLevel = getFacetPivotResultsKeepCount(
                genotypePhenotypeCore.query(interimQuery), interimPivot).entrySet().stream()
                        .filter(entry -> topLevelMpTerms.contains(entry.getKey()))
                        .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue().keySet()));

        // Instantiate commonGenes with itself as it will work as identity on first set intersection (intersection of same sets)
        Set<String> commonGenes = genesByMpTopLevel.values().iterator().next();
        // Keep only genes that are present in all top level mp groups
        commonGenes = genesByMpTopLevel.values().stream().reduce(commonGenes, (a, b) -> {
            a.retainAll(b);
            return a;
        });

        query.addFilterQuery(commonGenes.stream()
                .collect(Collectors.joining(" OR ", GenotypePhenotypeDTO.MARKER_SYMBOL + ":(", ")")));

    }

    return query;
}

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

License:Apache License

Map<String, Long> getDocumentCountByMgiGeneAccessionId() {

    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.setRows(0);//from   w w  w . ja v  a2  s. c  om
    query.setFacet(true);
    query.addFacetField(GenotypePhenotypeDTO.MARKER_ACCESSION_ID);
    query.setFacetLimit(-1);
    query.setFacetMinCount(1);

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

    return new HashMap<>();

}

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

License:Apache License

/**
 *
 * @param mpId//  ww w.  j  a va2  s.  c  om
 * @param filterOnAccessions list of marker accessions to restrict the results by e.g. for the hearing page we only want results for the 67 genes for the paper
 * @return
 * @throws JSONException
 */
public JSONArray getTopLevelPhenotypeIntersection(String mpId, Set<String> filterOnAccessions)
        throws JSONException {

    String pivot = GenotypePhenotypeDTO.MARKER_ACCESSION_ID + "," + GenotypePhenotypeDTO.MARKER_SYMBOL;
    SolrQuery query = new SolrQuery();
    query.setQuery(mpId == null ? "*:*"
            : GenotypePhenotypeDTO.MP_TERM_ID + ":\"" + mpId + "\" OR "
                    + GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + mpId + "\" OR "
                    + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + mpId + "\"");
    query.setRows(0);
    query.setFacet(true);
    query.addFacetField(GenotypePhenotypeDTO.MARKER_ACCESSION_ID);
    query.setFacetLimit(-1);
    query.setFacetMinCount(1);
    query.add("facet.pivot", pivot);

    try {
        QueryResponse response = genotypePhenotypeCore.query(query);
        Map<String, Long> countByGene = getFacets(response).get(GenotypePhenotypeDTO.MARKER_ACCESSION_ID);
        Map<String, List<String>> geneAccSymbol = getFacetPivotResults(response, pivot);

        Set<String> jitter = new HashSet<>();
        JSONArray array = new JSONArray();
        for (String markerAcc : countByGene.keySet()) {
            JSONObject obj = new JSONObject();
            Double y = new Double(countByGene.get(markerAcc));
            Double x = (getDocumentCountByMgiGeneAccessionId().get(markerAcc) - y);
            obj = addJitter(x, y, jitter, obj);
            obj.accumulate("markerAcc", markerAcc);
            obj.accumulate("markerSymbol", geneAccSymbol.get(markerAcc).get(0));
            if (filterOnAccessions != null && filterOnAccessions.contains(markerAcc)) {
                array.put(obj);
            } else if (filterOnAccessions == null) {
                array.put(obj);
            }
        }

        return array;

    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }
    return null;
}

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

License:Apache License

public Map<String, Set<String>> getFacets(String anatomyId) throws SolrServerException, IOException {

    Map<String, Set<String>> res = new HashMap<>();
    SolrQuery query = new SolrQuery();
    query.setQuery(ImageDTO.PROCEDURE_NAME + ":*LacZ");

    if (anatomyId != null) {
        query.addFilterQuery("(" + ImageDTO.ANATOMY_ID + ":\"" + anatomyId + "\" OR "
                + ImageDTO.INTERMEDIATE_ANATOMY_ID + ":\"" + anatomyId + "\" OR "
                + ImageDTO.SELECTED_TOP_LEVEL_ANATOMY_ID + ":\"" + anatomyId + "\")");
    }/*  w  ww.  j  a v  a2  s.  c  o  m*/
    query.addFilterQuery(ImageDTO.BIOLOGICAL_SAMPLE_GROUP + ":\"experimental\"")
            .addFilterQuery("(" + ImageDTO.PARAMETER_ASSOCIATION_VALUE + ":\"no expression\" OR "
                    + ObservationDTO.PARAMETER_ASSOCIATION_VALUE + ":\"expression\"" + ")"); // only have expressed and
    // not expressed ingnore
    // ambiguous and no tissue
    query.setFacet(true);
    query.setFacetLimit(-1);
    query.setFacetMinCount(1);
    query.addFacetField(ImageDTO.ANATOMY_TERM);
    query.addFacetField(ImageDTO.PHENOTYPING_CENTER);
    query.addFacetField(ImageDTO.PROCEDURE_NAME);
    query.addFacetField(ImageDTO.PARAMETER_ASSOCIATION_VALUE);
    QueryResponse response = impcImagesCore.query(query);

    for (FacetField facetField : response.getFacetFields()) {
        Set<String> filter = new TreeSet<>();
        for (Count facet : facetField.getValues()) {
            if (!facet.getName().equals("tissue not available") && !facet.getName().equals("ambiguous")) {
                filter.add(facet.getName());
            }
        }
        res.put(facetField.getName(), filter);

    }

    return res;
}

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

License:Apache License

public QueryResponse getProcedureFacetsForGeneByProcedure(String mgiAccession, String experimentOrControl)
        throws SolrServerException, IOException {

    // Map<String, ResponseWrapper<ImageDTO>> map=new HashMap<String,
    // ResponseWrapper<ImageDTO>>();
    // String queryString = "q=gene_accession_id:\"" + mgiAccession +
    // "\"&fq=" + ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":" +
    // experimentOrControl+"&facet=true&facet.field=procedure_name&facet.mincount=1";
    // log.debug("queryString in ImageService getFacets=" + queryString);

    // make a facet request first to get the procedures and then reuturn
    // make requests for each procedure
    // http://wwwdev.ebi.ac.uk/mi/impc/dev/solr/impc_images/select?q=gene_accession_id:%22MGI:2384986%22&&fq=biological_sample_group:experimental&facet=true&facet.field=procedure_name
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery(ImageDTO.GENE_ACCESSION_ID + ":\"" + mgiAccession + "\"");
    solrQuery.addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":" + experimentOrControl);
    solrQuery.setFacetMinCount(1);/*from www .  j  ava 2 s  . co m*/
    solrQuery.setFacet(true);
    solrQuery.addFacetField("procedure_name");
    // solrQuery.setRows(0);
    QueryResponse response = impcImagesCore.query(solrQuery);
    return response;
}

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

License:Apache License

public List<String[]> getLaczExpressionSpreadsheet(String imageCollectionLinkBase) {
    SolrQuery query = new SolrQuery();
    ArrayList<String[]> res = new ArrayList<>();
    String[] aux = new String[0];

    query.setQuery(ImageDTO.PROCEDURE_NAME + ":\"Adult LacZ\" AND " + ImageDTO.BIOLOGICAL_SAMPLE_GROUP
            + ":experimental");
    query.setRows(1000000);//from  w w w. ja  v a2  s  .c  o  m
    query.addField(ImageDTO.GENE_SYMBOL);
    query.addField(ImageDTO.GENE_ACCESSION_ID);
    query.addField(ImageDTO.ALLELE_SYMBOL);
    query.addField(ImageDTO.COLONY_ID);
    query.addField(ImageDTO.BIOLOGICAL_SAMPLE_ID);
    query.addField(ImageDTO.ZYGOSITY);
    query.addField(ImageDTO.SEX);
    query.addField(ImageDTO.PARAMETER_ASSOCIATION_NAME);
    query.addField(ImageDTO.PARAMETER_STABLE_ID);
    query.addField(ImageDTO.PARAMETER_ASSOCIATION_VALUE);
    query.addField(ImageDTO.GENE_ACCESSION_ID);
    query.addField(ImageDTO.PHENOTYPING_CENTER);
    query.setFacet(true);
    query.setFacetLimit(100);
    query.addFacetField(ImageDTO.PARAMETER_ASSOCIATION_NAME);
    query.set("group", true);
    query.set("group.limit", 100000);
    query.set("group.field", ImageDTO.BIOLOGICAL_SAMPLE_ID);

    try {
        QueryResponse solrResult = impcImagesCore.query(query);
        ArrayList<String> allParameters = new ArrayList<>();
        List<String> header = new ArrayList<>();
        header.add("Gene Symbol");
        header.add("MGI Gene Id");
        header.add("Allele Symbol");
        header.add("Colony Id");
        header.add("Biological Sample Id");
        header.add("Zygosity");
        header.add("Sex");
        header.add("Phenotyping Centre");

        logger.info(SolrUtils.getBaseURL(impcImagesCore) + "/select?" + query);

        // Get facets as we need to turn them into columns
        for (Count facet : solrResult.getFacetField(ImageDTO.PARAMETER_ASSOCIATION_NAME).getValues()) {
            allParameters.add(facet.getName());
            header.add(facet.getName());
        }
        header.add("image_collection_link");
        res.add(header.toArray(aux));
        for (Group group : solrResult.getGroupResponse().getValues().get(0).getValues()) {

            List<String> row = new ArrayList<>();
            ArrayList<String> params = new ArrayList<>();
            ArrayList<String> paramValues = new ArrayList<>();
            String urlToImagePicker = imageCollectionLinkBase + "/imageComparator?acc=";

            for (SolrDocument doc : group.getResult()) {
                if (row.size() == 0) {
                    row.add(doc.getFieldValues(ImageDTO.GENE_SYMBOL).iterator().next().toString());
                    row.add(doc.getFieldValues(ImageDTO.GENE_ACCESSION_ID).iterator().next().toString());
                    urlToImagePicker += doc.getFieldValue(ImageDTO.GENE_ACCESSION_ID) + "&parameter_stable_id=";
                    urlToImagePicker += doc.getFieldValue(ImageDTO.PARAMETER_STABLE_ID);
                    if (doc.getFieldValue(ImageDTO.ALLELE_SYMBOL) != null) {
                        row.add(doc.getFieldValue(ImageDTO.ALLELE_SYMBOL).toString());
                    }
                    row.add(doc.getFieldValue(ImageDTO.COLONY_ID).toString());
                    row.add(doc.getFieldValue(ImageDTO.BIOLOGICAL_SAMPLE_ID).toString());
                    if (doc.getFieldValue(ImageDTO.ZYGOSITY) != null) {
                        row.add(doc.getFieldValue(ImageDTO.ZYGOSITY).toString());
                    }
                    row.add(doc.getFieldValue(ImageDTO.SEX).toString());
                    row.add(doc.getFieldValue(ImageDTO.PHENOTYPING_CENTER).toString());
                }

                if (doc.getFieldValues(ImageDTO.PARAMETER_ASSOCIATION_NAME) != null) {
                    for (int i = 0; i < doc.getFieldValues(ImageDTO.PARAMETER_ASSOCIATION_NAME).size(); i++) {
                        params.add(doc.getFieldValues(ImageDTO.PARAMETER_ASSOCIATION_NAME)
                                .toArray(new Object[0])[i].toString());

                        if (doc.getFieldValues(ImageDTO.PARAMETER_ASSOCIATION_VALUE) != null) {
                            paramValues.add(doc.getFieldValues(ImageDTO.PARAMETER_ASSOCIATION_VALUE)
                                    .toArray(new Object[0])[i].toString());
                        } else {
                            paramValues.add(Constants.NO_INFORMATION_AVAILABLE);
                        }
                    }
                }
            }

            for (String tissue : allParameters) {
                if (params.contains(tissue)) {
                    row.add(paramValues.get(params.indexOf(tissue)));
                } else {
                    row.add("");
                }
            }
            row.add(urlToImagePicker);
            res.add(row.toArray(aux));
        }

    } catch (SolrServerException | IOException e) {
        e.printStackTrace();
    }
    return res;
}

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

License:Apache License

public QueryResponse getParameterFacetsForGeneByProcedure(String acc, String procedureName,
        String controlOrExperimental) throws SolrServerException, IOException {

    // e.g./*from   w w  w  . ja v  a2  s . co  m*/
    // http://ves-ebi-d0.ebi.ac.uk:8090/mi/impc/dev/solr/impc_images/query?q=gene_accession_id:%22MGI:2384986%22&fq=biological_sample_group:experimental&fq=procedure_name:X-ray&facet=true&facet.field=parameter_stable_id
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("gene_accession_id:\"" + acc + "\"");
    solrQuery.addFilterQuery(ObservationDTO.BIOLOGICAL_SAMPLE_GROUP + ":" + controlOrExperimental);
    solrQuery.setFacetMinCount(1);
    solrQuery.setFacet(true);
    solrQuery.addFilterQuery(ObservationDTO.PROCEDURE_NAME + ":\"" + procedureName + "\"");
    solrQuery.addFacetField(ObservationDTO.PARAMETER_STABLE_ID);
    // solrQuery.setRows(0);
    QueryResponse response = impcImagesCore.query(solrQuery);
    return response;

}

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

License:Apache License

/**
 *
 * @param acc//  w  ww  .j  a v  a2  s  . c  o m
 * @return a map containing the mp and colony_id combinations so that if we have these then we show an image link on the phenotype table on the gene page. Each row in table could have a different colony_id as well as mp id
 * @throws SolrServerException, IOException
 */

public Map<String, Set<String>> getImagePropertiesThatHaveMp(String acc)
        throws SolrServerException, IOException {
    //http://ves-ebi-d0.ebi.ac.uk:8090/mi/impc/dev/solr/impc_images/select?q=gene_accession_id:%22MGI:1913955%22&fq=mp_id:*&facet=true&facet.mincount=1&facet.limit=-1&facet.field=colony_id&facet.field=mp_id&facet.field=mp_term&rows=0
    Map<String, Set<String>> mpToColony = new HashMap<>();
    SolrQuery query = new SolrQuery();

    query.setQuery(ImageDTO.GENE_ACCESSION_ID + ":\"" + acc + "\"").setRows(100000000);
    query.addFilterQuery(ImageDTO.MP_ID_TERM + ":*");
    query.setFacet(true);
    query.setFacetLimit(-1);
    query.setFacetMinCount(1);

    String pivotFacet = ImageDTO.MP_ID_TERM + "," + ImageDTO.COLONY_ID;
    query.set("facet.pivot", pivotFacet);
    query.addFacetField(ObservationDTO.COLONY_ID);
    logger.debug("solr query for images properties for mp = " + query);
    QueryResponse response = impcImagesCore.query(query);
    for (PivotField pivot : response.getFacetPivot().get(pivotFacet)) {
        if (pivot.getPivot() != null) {
            //logger.info("pivot="+pivot.getValue());
            String mpIdAndName = pivot.getValue().toString();
            //logger.info("mpIdAndName" +mpIdAndName);
            String mpId = "";
            Set<String> colonIds = new TreeSet<>();
            if (mpIdAndName.contains("_")) {
                mpId = (mpIdAndName.split("_")[0]);
            }
            for (PivotField mp : pivot.getPivot()) {

                //logger.info("adding mp="+pivot.getValue()+" adding value="+mp.getValue());
                String colonyId = mp.getValue().toString();
                colonIds.add(colonyId);
            }
            mpToColony.put(mpId, colonIds);
        }

    }
    return mpToColony;

}

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

License:Apache License

public Set<BasicBean> getAllTopLevelPhenotypesAsBasicBeans() throws SolrServerException, IOException {

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.addFacetField("top_level_mp_term_id");
    solrQuery.setRows(0);//from w ww .j av a2  s . c  om
    QueryResponse rsp = mpCore.query(solrQuery);

    HashSet<BasicBean> allTopLevelPhenotypes = new LinkedHashSet<BasicBean>();
    for (FacetField ff : rsp.getFacetFields()) {
        for (Count count : ff.getValues()) {
            String mpArray[] = count.getName().split("___");
            BasicBean bean = new BasicBean();
            bean.setName(mpArray[1]);
            bean.setId(mpArray[0]);
            allTopLevelPhenotypes.add(bean);
        }
    }

    return allTopLevelPhenotypes;
}

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

License:Apache License

/**
 * Returns a collection of biological sample ids for all mice matching the PROCEDURE_STABLE_ID.
 *
 * @param procedureStableId the procedure stable id (e.g. "IMPC_CAL_*" or "IMPC_IPG_*")
 *
 * @return a collection of biological sample ids for all mice matching the PROCEDURE_STABLE_ID
 *
 * @throws SolrServerException, IOException
 *//*w  w w  .  java2  s. co m*/
public Collection<String> getMetabolismReportBiologicalSampleIds(String procedureStableId)
        throws SolrServerException, IOException {
    SolrQuery query = new SolrQuery();

    query.setQuery(String.format("%s:%s", ObservationDTO.PROCEDURE_STABLE_ID, procedureStableId));
    query.setRows(0);
    query.setFacetMinCount(1);
    query.setFacetLimit(100000);
    query.addFacetField(ObservationDTO.BIOLOGICAL_SAMPLE_ID);

    logger.info(SolrUtils.getBaseURL(experimentCore) + "/select?" + query);

    return getFacets(experimentCore.query(query)).get(ObservationDTO.BIOLOGICAL_SAMPLE_ID).keySet();
}