Example usage for org.apache.solr.client.solrj SolrClient query

List of usage examples for org.apache.solr.client.solrj SolrClient query

Introduction

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

Prototype

public QueryResponse query(SolrParams params) throws SolrServerException, IOException 

Source Link

Document

Performs a query to the Solr server

Usage

From source file:org.mousephenotype.cda.indexers.AbstractIndexer.java

License:Apache License

public long getImitsDocumentCount(SolrClient solrClient) throws IndexerException {
    Long numFound = getDocumentCount(solrClient);
    SolrQuery query = new SolrQuery().setQuery("*:*").setRows(0);
    query.setRequestHandler("selectCre");
    try {//from w w w  . jav  a  2  s .c o  m
        numFound += solrClient.query(query).getResults().getNumFound();
    } catch (SolrServerException | IOException e) {
        throw new IndexerException(e);
    }
    logger.debug("number found = " + numFound);
    return numFound;
}

From source file:org.mousephenotype.cda.indexers.AbstractIndexer.java

License:Apache License

public long getFacetCountTwoLevelPivot(SolrClient solr, SolrQuery q, String pivot)
        throws IOException, SolrServerException {

    Long count = new Long(0);
    List<PivotField> facetPivots = solr.query(q).getFacetPivot().get(pivot);

    for (PivotField p : facetPivots) {
        List<String> secondLevelFacets = new ArrayList<>();
        count += secondLevelFacets.size();
    }//from   www .jav  a2 s.  c o  m

    return count;
}

From source file:org.mousephenotype.cda.indexers.MPIndexer.java

License:Apache License

private RunStatus populateMpCallMaps(Map<String, Synonym> synonyms)
        throws IOException, SolrServerException, URISyntaxException, JSONException {
    RunStatus status = new RunStatus();

    List<SolrClient> ss = new ArrayList<>();

    ss.add(genotypePhenotypeCore);//from  w  w  w .j  ava 2 s. c o m

    for (int i = 0; i < ss.size(); i++) {

        SolrClient solrSvr = ss.get(i);
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setFacet(true);
        query.setRows(0);
        query.addFacetField(GenotypePhenotypeDTO.MP_TERM_ID);
        query.addFacetField(GenotypePhenotypeDTO.INTERMEDIATE_MP_TERM_ID);
        query.addFacetField(GenotypePhenotypeDTO.TOP_LEVEL_MP_TERM_ID);
        query.setFacetLimit(-1);

        for (FacetField facetGroup : solrSvr.query(query).getFacetFields()) {
            for (FacetField.Count facet : facetGroup.getValues()) {
                if (!mpCalls.containsKey(facet.getName())) {
                    mpCalls.put(facet.getName(), new Long(0));

                    Map<String, Integer> geneVariantCount = getPhenotypeGeneVariantCounts(facet.getName(),
                            status, synonyms);
                    int gvCount = geneVariantCount.get("sumCount");
                    mpGeneVariantCount.put(facet.getName(), gvCount);
                }
                mpCalls.put(facet.getName(), facet.getCount() + mpCalls.get(facet.getName()));

            }
        }
    }

    return status;
}

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

License:Apache License

public Map<String, String> getKeyValuePairs(SolrClient core, String pivotFacet) {

    Map<String, String> map = new HashMap<>();

    SolrQuery query = new SolrQuery();

    query.setQuery("*:*").setRows(0).setFacet(true).setFacetMinCount(1).setFacetLimit(-1).add("facet.pivot",
            pivotFacet);//from w  w w.j  a v  a 2 s. c o m

    try {
        QueryResponse response = core.query(query);

        for (PivotField pivot : response.getFacetPivot().get(pivotFacet)) {
            if (pivot.getPivot() != null) {
                for (PivotField parameter : pivot.getPivot()) {
                    String[] row = { pivot.getValue().toString(), parameter.getValue().toString() };
                    map.put(row[0], row[1]);
                }
            }
        }

    } catch (Exception e) {
        System.err.println("Exception: " + e.getLocalizedMessage());
        e.printStackTrace();
    }

    return map;
}

From source file:org.mousephenotype.cda.solr.SolrUtils.java

License:Apache License

/**
 * Fetch a map of image terms indexed by ma id
 *
 * @param imagesCore a valid solr connection
 * @return a map, indexed by child ma id, of all parent terms with
 * associations// ww  w  .j  av a  2  s  .c om
 * @throws SolrServerException, IOException
 */
public static Map<String, List<SangerImageDTO>> populateSangerImagesMap(SolrClient imagesCore)
        throws SolrServerException, IOException {
    Map<String, List<SangerImageDTO>> map = new HashMap();

    int pos = 0;
    long total = Integer.MAX_VALUE;
    SolrQuery query = new SolrQuery(SangerImageDTO.MA_ID + ":*");
    query.setRows(BATCH_SIZE);
    while (pos < total) {
        query.setStart(pos);
        QueryResponse response = null;
        response = imagesCore.query(query);

        total = response.getResults().getNumFound();
        List<SangerImageDTO> imageList = response.getBeans(SangerImageDTO.class);

        for (SangerImageDTO image : imageList) {
            for (String termId : image.getMaId()) {
                if (!map.containsKey(termId)) {
                    map.put(termId, new ArrayList<SangerImageDTO>());
                }
                String imageId = image.getId();
                List<SangerImageDTO> sangerImageList = map.get(termId);

                boolean imageFound = false;
                for (SangerImageDTO dto : sangerImageList) {
                    if (dto.getId().equalsIgnoreCase(imageId)) {
                        imageFound = true;
                        break;
                    }
                }
                // Don't add duplicate images.
                if (!imageFound) {
                    map.get(termId).add(image);
                }
            }
        }
        pos += BATCH_SIZE;
    }

    return map;
}

From source file:org.mousephenotype.cda.solr.SolrUtils.java

License:Apache License

/**
 * Fetch a map of image terms indexed by ma id
 *
 * @param imagesCore a valid solr connection
 * @return a map, indexed by child ma id, of all parent terms with
 * associations/*w w  w .j a  va2  s. c o m*/
 * @throws SolrServerException, IOException
 */
public static Map<String, List<SangerImageDTO>> populateSangerImagesByMgiAccession(SolrClient imagesCore)
        throws SolrServerException, IOException {
    Map<String, List<SangerImageDTO>> map = new HashMap();

    int pos = 0;
    long total = Integer.MAX_VALUE;
    SolrQuery query = new SolrQuery("mgi_accession_id:*");
    query.setRows(BATCH_SIZE);
    while (pos < total) {
        query.setStart(pos);
        QueryResponse response = null;
        response = imagesCore.query(query);
        total = response.getResults().getNumFound();
        List<SangerImageDTO> imageList = response.getBeans(SangerImageDTO.class);
        for (SangerImageDTO image : imageList) {

            if (!map.containsKey(image.getAccession())) {
                map.put(image.getAccession(), new ArrayList<SangerImageDTO>());
            }
            String imageId = image.getId();
            List<SangerImageDTO> sangerImageList = map.get(image.getAccession());

            boolean imageFound = false;
            for (SangerImageDTO dto : sangerImageList) {
                if (dto.getId().equalsIgnoreCase(imageId)) {
                    imageFound = true;
                    break;
                }
            }
            // Don't add duplicate images.
            if (!imageFound) {
                map.get(image.getAccession()).add(image);
            }

        }
        pos += BATCH_SIZE;
    }

    return map;
}

From source file:org.mousephenotype.cda.solr.SolrUtils.java

License:Apache License

/**
 * Fetch all alleles//from   w  w w  . j  a v  a  2 s. com
 *
 * @param alleleCore a valid solr connection
 * @return a list of all alleles
 *
 * @throws SolrServerException, IOException
 */
public static List<AlleleDTO> getAllAlleles(SolrClient alleleCore) throws SolrServerException, IOException {
    List<AlleleDTO> alleleList = new ArrayList<>();

    int pos = 0;
    long total = Integer.MAX_VALUE;
    SolrQuery query = new SolrQuery("*:*");

    query.setRows(Integer.MAX_VALUE);
    QueryResponse response = null;
    response = alleleCore.query(query);
    total = response.getResults().getNumFound();
    logger.info("  total alleles=" + total);
    alleleList = response.getBeans(AlleleDTO.class);

    logger.debug("  Loaded {} alleles", alleleList.size());

    return alleleList;
}

From source file:org.mousephenotype.cda.solr.SolrUtils.java

License:Apache License

/**
 * Fetch a map of mgi accessions to alleles
 *
 * @param alleleCore a valid solr connection
 * @return a map, indexed by MGI Accession id, of all alleles
 *
 * @throws SolrServerException, IOException
 *///from  w w  w  . j a  v a 2 s .com
public static Map<String, List<AlleleDTO>> populateAllelesMap(SolrClient alleleCore)
        throws SolrServerException, IOException {

    Map<String, List<AlleleDTO>> alleles = new HashMap<>();

    int pos = 0;
    long total = Integer.MAX_VALUE;
    SolrQuery query = new SolrQuery("*:*");
    query.setRows(BATCH_SIZE);
    while (pos < total) {
        query.setStart(pos);
        QueryResponse response = null;
        response = alleleCore.query(query);
        total = response.getResults().getNumFound();
        List<AlleleDTO> alleleList = response.getBeans(AlleleDTO.class);
        for (AlleleDTO allele : alleleList) {
            String key = allele.getMgiAccessionId();
            if (!alleles.containsKey(key)) {
                alleles.put(key, new ArrayList<>());
            }
            alleles.get(key).add(allele);
        }
        pos += BATCH_SIZE;
    }
    logger.debug("  Loaded {} alleles", alleles.size());

    return alleles;
}

From source file:org.mousephenotype.cda.solr.SolrUtils.java

License:Apache License

/**
 * Fetch a map of mp terms associated to hp terms, indexed by mp id.
 *
 * @param phenodigm_core a valid solr connection
 * @return a map, indexed by mp id, of all hp terms
 *
 * @throws SolrServerException, IOException
 *///from   w  w w.j a  v  a2 s. co m
public static Map<String, List<Map<String, String>>> populateMpToHpTermsMap(SolrClient phenodigm_core)
        throws SolrServerException, IOException {

    // url="q=mp_id:&quot;${nodeIds.term_id}&quot;&amp;rows=999&amp;fq=type:mp_hp&amp;fl=hp_id,hp_term"
    // processor="XPathEntityProcessor" >
    //
    // <field column="hp_id" xpath="/response/result/doc/str[@name='hp_id']"
    // />
    // <field column="hp_term"
    // xpath="/response/result/doc/str[@name='hp_term']" />
    Map<String, List<Map<String, String>>> mpToHp = new HashMap<>();

    int pos = 0;
    long total = Integer.MAX_VALUE;
    SolrQuery query = new SolrQuery("mp_id:*");
    query.addFilterQuery("type:mp_hp");// &amp;fl=hp_id,hp_term);
    query.add("fl=hp_id,hp_term");
    query.setRows(BATCH_SIZE);
    while (pos < total) {
        query.setStart(pos);
        QueryResponse response = null;
        response = phenodigm_core.query(query);
        total = response.getResults().getNumFound();
        SolrDocumentList solrDocs = response.getResults();
        for (SolrDocument doc : solrDocs) {
            if (doc.containsKey("hp_id")) {
                String hp = (String) doc.get("hp_id");
                if (doc.containsKey("mp_id")) {

                    String mp = (String) doc.get("mp_id");
                    List<Map<String, String>> mapList = new ArrayList<>();
                    Map<String, String> entryMap = new HashMap<>();
                    if (mpToHp.containsKey(mp)) {
                        mapList = mpToHp.get(mp);
                    }
                    entryMap.put("hp_id", hp);
                    if (doc.containsKey("hp_term")) {
                        String hpTerm = (String) doc.get("hp_term");
                        entryMap.put("hp_term", hpTerm);
                    }
                    mapList.add(entryMap);
                    mpToHp.put(mp, mapList);
                }
            }

        }
        pos += BATCH_SIZE;
    }

    return mpToHp;
}

From source file:org.mousephenotype.cda.solr.SolrUtils.java

License:Apache License

/**
 * Get a map of MpDTOs by key mgiAccesion
 *
 * @param mpSolrServer//from   www .j a v  a 2s  .c  o m
 * @return the map
 * @throws SolrServerException, IOException
 */
public static Map<String, List<MpDTO>> populateMgiAccessionToMp(SolrClient mpSolrServer)
        throws SolrServerException, IOException {

    Map<String, List<MpDTO>> mps = new HashMap<>();
    int pos = 0;
    long total = Integer.MAX_VALUE;
    SolrQuery query = new SolrQuery("mgi_accession_id:*");
    //query.add("fl=mp_id,mp_term,mp_definition,mp_term_synonym,ontology_subset,hp_id,hp_term,top_level_mp_id,top_level_mp_term,top_level_mp_term_synonym,intermediate_mp_id,intermediate_mp_term,intermediate_mp_term_synonym,child_mp_id,child_mp_term,child_mp_term_synonym,inferred_ma_id,inferred_ma_term,inferred_ma_term_synonym,inferred_selected_top_level_ma_id,inferred_selected_top_level_ma_term,inferred_selected_top_level_ma_term_synonym,inferred_child_ma_id,inferred_child_ma_term,inferred_child_ma_term_synonym");
    query.setRows(BATCH_SIZE);
    while (pos < total) {
        query.setStart(pos);
        QueryResponse response = null;
        response = mpSolrServer.query(query);
        total = response.getResults().getNumFound();
        List<MpDTO> mpBeans = response.getBeans(MpDTO.class);

        for (MpDTO mp : mpBeans) {
            if (mp.getMgiAccessionId() != null && !mp.getMgiAccessionId().equals("")) {
                for (String geneAccession : mp.getMgiAccessionId()) {

                    if (mps.containsKey(geneAccession)) {
                        mps.get(geneAccession).add(mp);
                    } else {
                        List<MpDTO> mpListPerGene = new ArrayList<>();
                        mpListPerGene.add(mp);
                        mps.put(geneAccession, mpListPerGene);
                    }
                }
            }
        }
        pos += BATCH_SIZE;
    }
    return mps;
}