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

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

Introduction

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

Prototype

public ModifiableSolrParams add(String name, String... val) 

Source Link

Document

Add the given values to any existing name

Usage

From source file:org.intermine.api.searchengine.solr.SolrKeywordSearchHandler.java

License:GNU General Public License

private QueryResponse performSearch(InterMineAPI im, String queryString, Map<String, String> facetValues,
        List<Integer> ids, int offSet, int rowSize) {

    SolrClient solrClient = SolrClientManager.getClientInstance(im.getObjectStore());

    QueryResponse resp = null;/*from  w w  w  .  j  a v  a  2  s  .  c om*/

    KeywordSearchPropertiesManager keywordSearchPropertiesManager = KeywordSearchPropertiesManager
            .getInstance(im.getObjectStore());

    Vector<KeywordSearchFacetData> facets = keywordSearchPropertiesManager.getFacets();

    Map<ClassDescriptor, Float> classBoost = keywordSearchPropertiesManager.getClassBoost();

    List<String> fieldNames = getFieldNamesFromSolrSchema(solrClient);

    try {

        SolrQuery newQuery = new SolrQuery();
        newQuery.setQuery(queryString);
        newQuery.setStart(offSet);
        newQuery.setRows(rowSize);
        newQuery.addField("score");
        newQuery.addField("id");
        newQuery.add("defType", "edismax");

        for (KeywordSearchFacetData keywordSearchFacetData : facets) {
            newQuery.addFacetField("facet_" + keywordSearchFacetData.getField());
        }

        // add faceting selections
        for (Map.Entry<String, String> facetValue : facetValues.entrySet()) {
            if (facetValue != null) {
                newQuery.addFilterQuery(facetValue.getKey() + ":" + facetValue.getValue());
            }
        }

        //limiting the query based on search bag
        if (ids != null && !ids.isEmpty()) {
            for (int id : ids) {
                newQuery.addFilterQuery("id", Integer.toString(id));
            }
        }

        String boostQuery = "";

        for (Map.Entry<ClassDescriptor, Float> boostValue : classBoost.entrySet()) {
            if (boostValue != null) {
                boostQuery += "classname:" + boostValue.getKey().getUnqualifiedName() + "^"
                        + boostValue.getValue() + " ";
            }
        }

        LOG.info("BoostQuery : " + boostQuery);

        String fieldListQuery = "";

        for (String field : fieldNames) {
            fieldListQuery = fieldListQuery + field;
            if (field.endsWith("_raw")) {
                fieldListQuery = fieldListQuery + "^2.0";
            }
            fieldListQuery = fieldListQuery + " ";
        }

        LOG.info("Field List Query : " + fieldListQuery);

        newQuery.add("bq", boostQuery);
        newQuery.add("qf", fieldListQuery);

        resp = solrClient.query(newQuery, SolrRequest.METHOD.POST);

        return resp;

    } catch (SolrServerException e) {
        LOG.error("Query performed on solr failed for search term : " + queryString, e);
        e.printStackTrace();

    } catch (IOException e) {
        LOG.error("Query performed on solr failed for search term : " + queryString, e);
        e.printStackTrace();

    }

    return resp;
}

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

License:Apache License

public TreeSet<CountTableRow> getAssociationsCount(String mpId, List<String> resourceName)
        throws IOException, SolrServerException {

    TreeSet<CountTableRow> list = new TreeSet<>(CountTableRow.getComparatorByCount());
    SolrQuery q = new SolrQuery().setFacet(true).setRows(0);
    q.set("facet.limit", -1);

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

    if (mpId != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.MP_TERM_ID + ":\"" + mpId + "\" OR "
                + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + mpId + "\" OR "
                + GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + mpId + "\"");
    }

    String pivot = GenotypePhenotypeDTO.MP_TERM_ID + "," + GenotypePhenotypeDTO.MP_TERM_NAME + ","
            + GenotypePhenotypeDTO.MARKER_ACCESSION_ID;
    q.add("facet.pivot", pivot);

    logger.info("Solr url for getAssociationsCount " + SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select?"
            + q);

    QueryResponse response = genotypePhenotypeCore.query(q);
    for (PivotField p : response.getFacetPivot().get(pivot)) {
        if (p.getPivot() != null) {
            String mpTermId = p.getValue().toString();
            String mpName = p.getPivot().get(0).getValue().toString();
            List<PivotField> pivotFields = p.getPivot().get(0).getPivot();
            Set<String> uniqueAccessions = new HashSet<>();
            if (pivotFields != null) {
                for (PivotField accessionField : pivotFields) {
                    String accession = accessionField.getValue().toString();
                    uniqueAccessions.add(accession);
                }
            }
            int count = uniqueAccessions.size();//we are setting this to the size of the unique set of gene accessions for this MP term and not the count as the count has male and female results and doesn't relate to the number of unique genes which is what we are currently displaying on the interface
            list.add(new CountTableRow(mpName, mpTermId, count));
        }
    }

    return list;
}

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

License:Apache License

public Map<String, List<String>> getMpTermByGeneMap(List<String> geneSymbols, String facetPivot,
        List<String> resourceName)
        throws SolrServerException, IOException, InterruptedException, ExecutionException {

    Map<String, List<String>> mpTermsByGene = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

    SolrQuery q = new SolrQuery().setFacet(true).setRows(1);
    q.set("facet.limit", -1);

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

    if (facetPivot != null) {
        q.add("facet.pivot", facetPivot);
    }

    logger.info("Solr url for getOverviewGenesWithMoreProceduresThan "
            + SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select?" + q);

    QueryResponse response = genotypePhenotypeCore.query(q);
    for (PivotField pivot : response.getFacetPivot().get(facetPivot)) {

        if (pivot.getPivot() != null) {
            String mpTerm = pivot.getValue().toString();

            if (!mpTermsByGene.containsKey(mpTerm)) {
                mpTermsByGene.put(mpTerm, new ArrayList<String>());
            }

            for (PivotField genePivot : pivot.getPivot()) {
                String gene = genePivot.getValue().toString();
                if (geneSymbols.contains(gene)) {
                    mpTermsByGene.get(mpTerm).add(gene);
                }
            }
        }
    }

    return mpTermsByGene;
}

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

License:Apache License

public String getPleiotropyDownload(List<String> topLevelMpTerms, Boolean idg, String idgClass)
        throws IOException, SolrServerException, SQLException {

    //SolrQuery query = getBiologicalSystemPleiotropyDownloadQuery(topLevelMpTerms,idg, idgClass, biologicalSystem);
    SolrQuery query = getPleiotropyQuery(topLevelMpTerms, idg, idgClass);
    query.add("wt", "xslt");
    query.add("tr", "pivot.xsl");

    HttpURLConnection connection = (HttpURLConnection) new URL(
            SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select?" + query).openConnection();
    System.out.println("download query=" + SolrUtils.getBaseURL(genotypePhenotypeCore) + "/select?" + query);
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    // Return list of genes, uniue entrie only. The splitting is based on the order of pivot facets.
    return br.lines().collect(Collectors.joining("\n"));

}

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   w w  w.j  av 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

/**
 *
 * @param mpId/*from  w w  w .  j  av a  2s .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 List<Group> getPhenotypeAssociatedImages(String geneAcc, String mpId, List<String> anatomyIds,
        boolean experimentalOnly, int count) throws SolrServerException, IOException {

    List<Group> groups = new ArrayList<>();
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("*:*");
    String fq = "";
    if (geneAcc != null) {
        solrQuery.addFilterQuery(ImageDTO.GENE_ACCESSION_ID + ":\"" + geneAcc + "\"");
    }/*from  www.ja v  a2  s .c  om*/
    if (mpId != null) {
        fq = ImageDTO.MP_ID + ":\"" + mpId + "\" OR " + ImageDTO.INTERMEDIATE_MP_ID + ":\"" + mpId + "\" OR "
                + ImageDTO.TOP_LEVEL_MP_ID + ":\"" + mpId + "\"";
    }
    if (experimentalOnly) {
        solrQuery.addFilterQuery(ImageDTO.BIOLOGICAL_SAMPLE_GROUP + ":" + BiologicalSampleType.experimental);
    }
    if (anatomyIds != null && !anatomyIds.isEmpty()) {
        fq += (fq.isEmpty()) ? "" : " OR ";
        fq += anatomyIds.stream().collect(
                Collectors.joining("\" OR " + ImageDTO.ANATOMY_ID + ":\"", ImageDTO.ANATOMY_ID + ":\"", "\""));
        fq += " OR " + anatomyIds.stream()
                .collect(Collectors.joining("\" OR " + ImageDTO.INTERMEDIATE_ANATOMY_ID + ":\"",
                        ImageDTO.INTERMEDIATE_ANATOMY_ID + ":\"", "\""));
    }
    solrQuery.addFilterQuery(fq);
    solrQuery.add("group", "true").add("group.field", ImageDTO.PARAMETER_STABLE_ID).add("group.limit",
            Integer.toString(count));
    logger.info("associated images solr query: " + solrQuery);
    QueryResponse response = impcImagesCore.query(solrQuery);
    List<GroupCommand> groupResponse = response.getGroupResponse().getValues();
    for (GroupCommand groupCommand : groupResponse) {
        List<Group> localGroups = groupCommand.getValues();
        groups.addAll(localGroups);

    }
    return groups;

}

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

License:Apache License

public List<String> getGenesWithMoreProcedures(int n, List<String> resourceName)
        throws SolrServerException, IOException, 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 {/* w  ww.  ja  v a2s . co 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);

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

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

    return genes;
}

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

License:Apache License

public NamedList<List<PivotField>> getHistopathGeneParameterNameCategoryPivots()
        throws SolrServerException, IOException {

    //http://ves-hx-d1.ebi.ac.uk:8986/solr/experiment/select?q=*:*&rows=0&sort=id+asc&fq=parameter_stable_id:*HIS*&facet=true&facet.pivot=gene_symbol,category&facet.limit=-1
    //we need the significance score only can't filter based on a parameter ids as all different for each anatomy but can do search for "Significance score " in parameter_name string
    SolrQuery q = new SolrQuery().setQuery("*:*").setRows(0)//we don't care about the observations themselfs so don't return them only which anatomy has significant Histopath data on which genes.
            .setSort(ObservationDTO.ID, SolrQuery.ORDER.asc)
            //.setFields(fields)
            .addFilterQuery("parameter_stable_id:*HIS*").addFilterQuery("parameter_name:*Significance*");//.addFilterQuery("gene_symbol:Prkab1");
    //.addFilterQuery("category:Significant");
    //.addFilterQuery("category:Significant");//otherwise query takes too long
    q.setFacet(true);//ww w . ja v a  2 s . c  o m
    String pivotFacet = ObservationDTO.GENE_SYMBOL + "," + ObservationDTO.PARAMETER_NAME + ","
            + ObservationDTO.CATEGORY;

    q.add("facet.pivot", pivotFacet);
    q.setFacetLimit(-1);
    System.out.println("solr query in getObservationByProcedureNameAndGene=" + q);
    return experimentCore.query(q).getFacetPivot();
}

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

License:Apache License

public Set<String> getChartPivots(String baseUrl, String acc, ParameterDTO parameter,
        List<String> pipelineStableIds, List<String> zyList, List<String> phenotypingCentersList,
        List<String> strainsParams, List<String> metaDataGroup, List<String> alleleAccession)
        throws IOException, SolrServerException, URISyntaxException {

    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    if (acc != null) {
        query.addFilterQuery("gene_accession_id" + ":\"" + acc + "\"");
    }//from w  w  w . j a va 2 s. c o m
    if (parameter != null) {
        query.addFilterQuery(StatisticalResultDTO.PARAMETER_STABLE_ID + ":" + parameter.getStableId());
    }
    if (pipelineStableIds != null && pipelineStableIds.size() > 0) {
        query.addFilterQuery(pipelineStableIds.stream()
                .collect(Collectors.joining(" OR ", StatisticalResultDTO.PIPELINE_STABLE_ID + ":(", ")")));
    }
    if (zyList != null && zyList.size() > 0) {
        query.addFilterQuery(
                zyList.stream().collect(Collectors.joining(" OR ", StatisticalResultDTO.ZYGOSITY + ":(", ")")));
    }
    if (phenotypingCentersList != null && phenotypingCentersList.size() > 0) {
        query.addFilterQuery(phenotypingCentersList.stream().collect(
                Collectors.joining("\" OR \"", StatisticalResultDTO.PHENOTYPING_CENTER + ":(\"", "\")")));
    }
    if (strainsParams != null && strainsParams.size() > 0) {
        query.addFilterQuery(strainsParams.stream().collect(
                Collectors.joining("\" OR \"", StatisticalResultDTO.STRAIN_ACCESSION_ID + ":(\"", "\")")));
    }
    if (metaDataGroup != null && metaDataGroup.size() > 0) {
        query.addFilterQuery(metaDataGroup.stream()
                .collect(Collectors.joining("\" OR \"", StatisticalResultDTO.METADATA_GROUP + ":(\"", "\")")));
    }
    if (alleleAccession != null && alleleAccession.size() > 0) {
        query.addFilterQuery(alleleAccession.stream().collect(
                Collectors.joining("\" OR \"", StatisticalResultDTO.ALLELE_ACCESSION_ID + ":(\"", "\")")));
    }
    query.setFacet(true);

    // If you add/change order of pivots, make sure you do the same in the for loops below
    String pivotFacet = StatisticalResultDTO.PIPELINE_STABLE_ID + "," + StatisticalResultDTO.ZYGOSITY + ","
            + StatisticalResultDTO.PHENOTYPING_CENTER + "," + StatisticalResultDTO.STRAIN_ACCESSION_ID + ","
            + StatisticalResultDTO.ALLELE_ACCESSION_ID;
    if (metaDataGroup != null && metaDataGroup.size() > 0) {
        pivotFacet += "," + StatisticalResultDTO.METADATA_GROUP;

    }
    query.add("facet.pivot", pivotFacet);

    query.setFacetLimit(-1);

    Set<String> resultParametersForCharts = new HashSet<>();
    NamedList<List<PivotField>> facetPivot = experimentCore.query(query).getFacetPivot();
    for (PivotField pivot : facetPivot.get(pivotFacet)) {
        getParametersForChartFromPivot(pivot, baseUrl, resultParametersForCharts);
    }
    return resultParametersForCharts;
}