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

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

Introduction

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

Prototype

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

Source Link

Document

Replace any existing parameter with the given name.

Usage

From source file:org.lilyproject.lilyservertestfw.test.KeepDataTest.java

License:Apache License

private List<RecordId> querySolr(String name) throws SolrServerException {
    SolrServer solr = lilyProxy.getSolrProxy().getSolrServer();
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.set("df", "name");
    solrQuery.setQuery(name);//from w w  w  .  j  av a2  s.  c  om
    solrQuery.set("fl", "lily.id");

    QueryResponse response = solr.query(solrQuery);
    // Convert query result into a list of record IDs
    SolrDocumentList solrDocumentList = response.getResults();
    List<RecordId> recordIds = new ArrayList<RecordId>();
    for (SolrDocument solrDocument : solrDocumentList) {
        String recordId = (String) solrDocument.getFirstValue("lily.id");
        recordIds.add(repository.getIdGenerator().fromString(recordId));
    }
    return recordIds;
}

From source file:org.lilyproject.lilyservertestfw.test.LilyProxyTest.java

License:Apache License

private List<RecordId> querySolr(String name) throws SolrServerException, IOException {
    SolrServer solr = lilyProxy.getSolrProxy().getSolrServer();
    solr.commit();//from w w w .  ja v  a  2  s  .c  om
    SolrQuery solrQuery = new SolrQuery();
    //set default search field (no longer supported in schema.xml
    solrQuery.set("df", "name");
    solrQuery.setQuery(name);
    solrQuery.set("fl", "lily.id");
    QueryResponse response = solr.query(solrQuery);
    // Convert query result into a list of record IDs
    SolrDocumentList solrDocumentList = response.getResults();
    List<RecordId> recordIds = new ArrayList<RecordId>();
    for (SolrDocument solrDocument : solrDocumentList) {
        String recordId = (String) solrDocument.getFirstValue("lily.id");
        recordIds.add(repository.getIdGenerator().fromString(recordId));
    }
    return recordIds;
}

From source file:org.mousephenotype.cda.solr.repositories.image.ImagesSolrJ.java

License:Apache License

/**
 * Returns documents from Solr Index filtered by the passed in query
 * filterField e.g. annotationTerm:large ear
 * @throws SolrServerException, IOException
 *
 *
 *///from   www  .jav  a  2 s .  c  o  m
public QueryResponse getFilteredDocsForQuery(String query, List<String> filterFields, String qf, String defType,
        int start, int length) throws SolrServerException, IOException {

    SolrQuery solrQuery = new SolrQuery();

    //if query field is set (such as auto_suggest), add this to the search
    if (!qf.equals("")) {
        solrQuery.set("qf", qf);
        log.debug("added qf=" + qf);
    }

    //if defType is set (such as edismax), add this to the search
    if (!defType.equals("")) {
        solrQuery.set("defType", defType);
        log.debug("set defType=" + defType);
    }

    solrQuery.setQuery(query);
    solrQuery.setStart(start);
    solrQuery.setRows(length);

    for (String fieldNValue : filterFields) {
        String[] colonStrings = fieldNValue.split(":");
        String filterField = colonStrings[0];
        String filterParam = fieldNValue.substring(fieldNValue.indexOf(":") + 1, fieldNValue.length());

        filterParam = processValueForSolr(filterParam);
        String fq = filterField + ":" + filterParam;

        log.debug("adding filter fieldNValue=" + fq);

        solrQuery.addFilterQuery(fq);
    }

    log.debug("Query is: " + solrQuery.toString());

    return sangerImagesCore.query(solrQuery);
}

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 {//ww w. ja v  a  2s .  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 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 {//w  w w .j a  v  a  2s  .  c  o  m
        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

private List<String[]> getHitsDistributionBySomething(String field, List<String> resourceName)
        throws SolrServerException, IOException, InterruptedException, ExecutionException {

    List<String[]> res = new ArrayList<>();
    Long time = System.currentTimeMillis();
    String pivotFacet = "";
    SolrQuery q = new SolrQuery();

    if (field.equals(GenotypePhenotypeDTO.PARAMETER_STABLE_ID)) {
        pivotFacet = GenotypePhenotypeDTO.PARAMETER_STABLE_ID + "," + StatisticalResultDTO.PARAMETER_NAME;
    } else if (field.equals(GenotypePhenotypeDTO.PROCEDURE_STABLE_ID)) {
        pivotFacet = GenotypePhenotypeDTO.PROCEDURE_STABLE_ID + "," + StatisticalResultDTO.PROCEDURE_NAME;
    }//from w w w  .  j a  v  a  2s.  com

    if (resourceName != null) {
        q.setQuery(GenotypePhenotypeDTO.RESOURCE_NAME + ":"
                + StringUtils.join(resourceName, " OR " + GenotypePhenotypeDTO.RESOURCE_NAME + ":"));
    } else {
        q.setQuery("*:*");
    }
    q.set("facet.pivot", pivotFacet);
    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 (PivotField pivot : response.getFacetPivot().get(pivotFacet)) {
        if (pivot.getPivot() != null) {
            String id = pivot.getValue().toString();
            String name = pivot.getPivot().get(0).getValue().toString();
            int count = pivot.getPivot().get(0).getCount();
            String[] row = { id, name, Integer.toString(count) };
            res.add(row);
        }
    }

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

}

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

License:Apache License

public List<Group> getGenesBy(String mpId, String sex, boolean onlyB6N)
        throws SolrServerException, IOException {

    // males only
    SolrQuery q = new SolrQuery().setQuery("(" + GenotypePhenotypeDTO.MP_TERM_ID + ":\"" + mpId + "\" OR "
            + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + mpId + "\" OR "
            + GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + mpId + "\")");
    if (onlyB6N) {
        q.setFilterQueries("(" + GenotypePhenotypeDTO.STRAIN_ACCESSION_ID + ":\""
                + StringUtils.join(OverviewChartsConstants.B6N_STRAINS,
                        "\" OR " + GenotypePhenotypeDTO.STRAIN_ACCESSION_ID + ":\"")
                + "\")");
    }/*w w  w  . java 2 s  .c  o  m*/
    q.setRows(10000000);
    q.set("group.field", "" + GenotypePhenotypeDTO.MARKER_SYMBOL);
    q.set("group", true);
    q.set("group.limit", 0);
    if (sex != null) {
        q.addFilterQuery(GenotypePhenotypeDTO.SEX + ":" + sex);
    }
    QueryResponse results = genotypePhenotypeCore.query(q);

    return results.getGroupResponse().getValues().get(0).getValues();
}

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

License:Apache License

public List<String> getGenesAssocByParamAndMp(String parameterStableId, String phenotype_id)
        throws SolrServerException, IOException {

    List<String> res = new ArrayList<String>();
    SolrQuery query = new SolrQuery()
            .setQuery("(" + GenotypePhenotypeDTO.MP_TERM_ID + ":\"" + phenotype_id + "\" OR "
                    + GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID + ":\"" + phenotype_id + "\" OR "
                    + GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID + ":\"" + phenotype_id + "\") AND ("
                    + GenotypePhenotypeDTO.STRAIN_ACCESSION_ID + ":\""
                    + StringUtils.join(OverviewChartsConstants.B6N_STRAINS,
                            "\" OR " + GenotypePhenotypeDTO.STRAIN_ACCESSION_ID + ":\"")
                    + "\") AND " + GenotypePhenotypeDTO.PARAMETER_STABLE_ID + ":\"" + parameterStableId + "\"")
            .setRows(100000000);/* w ww .j  a va 2s .c  o  m*/
    query.set("group.field", GenotypePhenotypeDTO.MARKER_ACCESSION_ID);
    query.set("group", true);
    List<Group> groups = genotypePhenotypeCore.query(query).getGroupResponse().getValues().get(0).getValues();
    for (Group gr : groups) {
        if (!res.contains((String) gr.getGroupValue())) {
            res.add((String) gr.getGroupValue());
        }
    }
    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  www . ja va  2s.co m*/
    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);
}