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

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

Introduction

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

Prototype

public SolrQuery(String q) 

Source Link

Document

Create a new SolrQuery

Usage

From source file:edu.harvard.lib.lcloud.ItemDAO.java

License:Open Source License

public Item getItem(String id) {
    SolrDocumentList docs;/*from   ww w  .  j  a va2 s.  co  m*/
    SolrDocument doc;
    Item item = new Item();
    HttpSolrServer server = null;
    try {
        server = SolrServer.getSolrConnection();
        SolrQuery query = new SolrQuery("recordIdentifier:" + id);
        QueryResponse response = server.query(query);
        docs = response.getResults();
        if (docs.size() == 0)
            item = null;
        else {
            doc = docs.get(0);
            item = setItem(doc);
        }
    } catch (SolrServerException se) {
        log.error(se.getMessage());
        se.printStackTrace();
    }
    return item;
}

From source file:edu.harvard.liblab.ecru.LoadCsvData.java

License:Open Source License

/**
 * @param id  The Unique Key in solr//from  w  w  w  . j a v a 2 s.c  o m
 * @return  A record, or null
 */
private static SolrDocument getDocFromSolr(String id) {
    SolrQuery sq = new SolrQuery("id:" + id);
    SolrDocument sdoc = null;
    try {
        QueryResponse qr = solrSrvr.query(sq);
        if (qr != null && qr.getResults() != null && qr.getResults().size() == 1) {
            sdoc = qr.getResults().get(0);
        }
    } catch (SolrServerException e) {
        if (isVerbose) {
            System.err.println("SolrServerException for id:" + id);
        }
        e.printStackTrace();
    }
    return sdoc;
}

From source file:edu.harvard.liblab.ecru.LoadCsvData.java

License:Open Source License

/**
 * @param cId  The Unique Key for a course record
 * @return   true if there are Readings with this key for their 
 *           readings.course_id//from   w w  w . j av a2 s  .  c o  m
 */
private static boolean hasReadings(String cId) {
    boolean retVal = false;
    SolrQuery sq = new SolrQuery("reading.course_id:" + cId);
    try {
        QueryResponse qr = solrSrvr.query(sq);
        retVal = (qr != null && qr.getResults() != null && qr.getResults().size() > 0);
    } catch (SolrServerException e) {
        if (isVerbose) {
            System.err.println("SolrServerException for reading.course_id:" + cId);
        }
        e.printStackTrace();
    }
    return retVal;

}

From source file:edu.harvard.liblab.ecru.LoadCsvData.java

License:Open Source License

/**
 * @param cId    The course ID//w w  w.j av a 2s. c  om
 * @param term   The updated term
 * @param startDate The updated startDate
 * @param endDate  The updated endDate
 * 
 * If a Course record already existed in the solr db, and the incoming 
 * Course record has an updated Term, Start Date, and/or End Date, all the 
 * associated Reading records in the solr db must be updated as well.
 */
private static void updateReadingsTermDates(String cId, String term, Date startDate, Date endDate) {
    SolrQuery sq = new SolrQuery("reading.course_id:" + cId);
    try {
        QueryResponse qr = solrSrvr.query(sq);
        if (qr != null && qr.getResults() != null && qr.getResults().size() > 0) {
            for (SolrDocument sd : qr.getResults()) {
                SolrInputDocument doc = new SolrInputDocument();
                Map<String, String> partialUpdate = new HashMap<String, String>();
                doc.setField("id", sd.get("id"));
                partialUpdate.put("set", term);
                doc.addField("term", partialUpdate);
                Map<String, Date> partialStart = new HashMap<String, Date>();
                partialStart.put("set", startDate);
                doc.addField("start_date", partialStart);
                Map<String, Date> partialEnd = new HashMap<String, Date>();
                partialEnd.put("set", endDate);
                doc.addField("end_date", partialEnd);
                docUpdates.add(doc);
            }
        }
    } catch (SolrServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:edu.tamu.tcat.trc.digires.books.hathitrust.HTFilesSearchService.java

License:Apache License

@Override
public CopySearchResult find(ContentQuery query) throws ResourceAccessException {
    if (solrServerFuture == null)
        throw new IllegalStateException("Service is disposed");

    // HACK hard coded. Should be provided to the service
    try {/* w w w.j  av a2  s .  c o m*/
        HttpSolrClient solrServer = solrServerFuture.get(2, TimeUnit.MINUTES);
        Objects.requireNonNull(solrServer, "No active connection to Solr Server");

        String queryString = formatQueryString(query);

        SolrQuery solrQuery = new SolrQuery(queryString);
        solrQuery.setRows(Integer.valueOf(query.getLimit()));
        solrQuery.setStart(Integer.valueOf(query.getOffset()));
        //         solrQuery.addFilterQuery(buildDateFilter(query));
        QueryResponse response = solrServer.query(solrQuery);
        return getSearchResults(response);
    } catch (Exception ex) {
        throw new ResourceAccessException("", ex);
    }

}

From source file:edu.ucsb.nceas.mdqengine.solr.SolrIndex.java

License:Open Source License

/**
 * Get all indexed ids in the solr server.
 * @return an empty list if there is no index.
 * @throws SolrServerException/* ww w  .  ja v a2  s .  co m*/
 */
public List<String> getSolrIds() throws SolrServerException, IOException {
    List<String> list = new ArrayList<String>();
    SolrQuery query = new SolrQuery(IDQUERY);
    query.setRows(Integer.MAX_VALUE);
    query.setFields(ID);
    QueryResponse response = solrClient.query(query);
    SolrDocumentList docs = response.getResults();
    if (docs != null) {
        for (SolrDocument doc : docs) {
            String identifier = (String) doc.getFieldValue(ID);
            list.add(identifier);
        }
    }
    return list;
}

From source file:edu.vt.vbi.patric.cache.DataLandingGenerator.java

License:Apache License

private JSONObject getGenomeCounts() {
    JSONArray series = new JSONArray();

    // TODO: reimplement with json facet using sub faceting
    SolrQuery queryComplete = new SolrQuery("genome_status:Complete");
    SolrQuery queryWGS = new SolrQuery("genome_status:WGS");

    queryComplete.setFacet(true).setRows(0).setFacetSort(FacetParams.FACET_SORT_INDEX);
    //.set("json.facet", "{genome_count:{range:{field:completion_date,start:\"2010-01-01T00:00:00.000Z\",end:\"2016-01-01T00:00:00.000Z\",gap:\"%2B1YEAR\",other:\"before\"}}}");
    queryWGS.setFacet(true).setRows(0).setFacetSort(FacetParams.FACET_SORT_INDEX);
    //.set("json.facet", "{genome_count:{range:{field:completion_date,start:\"2010-01-01T00:00:00.000Z\",end:\"2016-01-01T00:00:00.000Z\",gap:\"%2B1YEAR\",other:\"before\"}}}");

    try {/*from  w w w . j a  va2s  .  c  o m*/
        Date rangeStartDate = DateUtil.parseDate("2010-01-01'T'00:00:00.000'Z'");
        Date rangeEndDate = DateUtil.parseDate("2016-01-01'T'00:00:00.000'Z'");

        queryComplete.addDateRangeFacet("completion_date", rangeStartDate, rangeEndDate, "+1YEAR")
                .add(FacetParams.FACET_RANGE_OTHER, "before");
        queryWGS.addDateRangeFacet("completion_date", rangeStartDate, rangeEndDate, "+1YEAR")
                .add(FacetParams.FACET_RANGE_OTHER, "before");
    } catch (java.text.ParseException e) {
        LOGGER.error(e.getMessage(), e);
    }

    try {
        LOGGER.debug("getGenomeCount: [{}] {}", SolrCore.GENOME.getSolrCoreName(), queryComplete);

        String apiResponse = dataApi.solrQuery(SolrCore.GENOME, queryComplete);

        Map resp = jsonReader.readValue(apiResponse);
        Map completeFacets = (Map) ((Map) ((Map) resp.get("facet_counts")).get("facet_ranges"))
                .get("completion_date");

        LOGGER.debug("getGenomeCount: [{}] {}", SolrCore.GENOME.getSolrCoreName(), queryWGS);

        apiResponse = dataApi.solrQuery(SolrCore.GENOME, queryWGS);

        resp = jsonReader.readValue(apiResponse);
        Map wgsFacets = (Map) ((Map) ((Map) resp.get("facet_counts")).get("facet_ranges"))
                .get("completion_date");

        int countComplete = (Integer) completeFacets.get("before");
        int countWGS = (Integer) wgsFacets.get("before");

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

        List listComplete = (List) completeFacets.get("counts");
        for (int i = 0; i < listComplete.size(); i = i + 2) {
            countComplete = countComplete + (Integer) listComplete.get(i + 1);
            mapCountComplete.put(((String) listComplete.get(i)).substring(0, 4), countComplete);
        }
        List listWGS = (List) wgsFacets.get("counts");
        for (int i = 0; i < listWGS.size(); i = i + 2) {
            String year = ((String) listWGS.get(i)).substring(0, 4);

            countWGS = countWGS + (Integer) listWGS.get(i + 1);
            mapCountWGS.put(year, countWGS);

            JSONObject item = new JSONObject();
            item.put("year", Integer.parseInt(year));
            item.put("complete", mapCountComplete.get(year));
            item.put("wgs", mapCountWGS.get(year));
            series.add(item);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    JSONObject jsonData = new JSONObject();
    jsonData.put("chart_title", "Number of Bacterial Genomes");
    jsonData.put("data", series);

    return jsonData;
}

From source file:edu.vt.vbi.patric.cache.DataLandingGenerator.java

License:Apache License

private JSONObject getPopularGenomes() {
    JSONObject jsonData = null;//from ww w. j  a v a 2s  . co  m
    JSONArray list = new JSONArray();

    DataApiHandler dataApi = new DataApiHandler();

    for (String genomeId : REFERENCE_GENOME_IDS) {

        Genome genome = dataApi.getGenome(genomeId);

        // construct genome
        JSONObject popGenome = new JSONObject();
        popGenome.put("link", URL_GENOMEOVERVIEW_TAB.replace("{cType}", "genome").replace("{cId}", genomeId));
        popGenome.put("popularName", genome.getGenomeName());
        popGenome.put("gb_link", URL_GENOMEBROWSER.replace("{cType}", "genome").replace("{cId}", genomeId));

        // meta data
        JSONObject meta = new JSONObject();
        meta.put("genome_status", genome.getGenomeStatus());
        meta.put("completion_date", genome.hasCompletionDate() ? genome.getCompletionDate() : "");
        meta.put("collection_date", genome.hasCollectionDate() ? genome.getCollectionDate() : "");
        meta.put("isolation_country", genome.hasIsolationCountry() ? genome.getIsolationCountry() : "");
        meta.put("host_name", genome.hasHostName() ? genome.getHostName() : "");
        meta.put("disease", genome.hasDisease() ? StringUtils.join(genome.getDisease(), ", ") : "");
        meta.put("chromosomes", genome.getChromosomes());
        meta.put("plasmids", genome.getPlasmids());
        meta.put("contigs", genome.getContigs());
        meta.put("genome_length", genome.getGenomeLength());

        popGenome.put("metadata", meta);

        JSONArray data = new JSONArray();

        // Features
        JSONObject ft = new JSONObject();
        ft.put("description", "Features");
        ft.put("link", URL_FEATURETABLE_TAB.replace("{cType}", "genome").replace("{cId}", genomeId)
                .replace("{featureType}", "").replace("{filterType}", ""));
        ft.put("picture", "/patric/images/icon-popular-feature.png");
        ft.put("data", genome.getPatricCds());
        data.add(ft);

        // Pathways
        JSONObject pw = new JSONObject();
        pw.put("description", "Pathways");
        pw.put("link",
                URL_PATHWAY_TAB.replace("{cType}", "genome").replace("{cId}", genomeId).replace("{pId}", ""));
        pw.put("picture", "/patric/images/icon-popular-pathway.png");

        int cntPathway = 0;
        try {
            SolrQuery query = new SolrQuery("genome_id:" + genomeId);
            // {stat:{field:{field:genome_id,facet:{pathway_count:"unique(pathway_id)"}}}}}
            query.setRows(0).setFacet(true).set("json.facet",
                    "{stat:{field:{field:genome_id,facet:{pathway_count:\"unique(pathway_id)\"}}}}}");

            LOGGER.trace("[{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query);

            String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query);

            Map resp = jsonReader.readValue(apiResponse);
            Map facets = (Map) resp.get("facets");
            List<Map> buckets = (List) ((Map) facets.get("stat")).get("buckets");
            Map firstPathway = buckets.get(0);
            cntPathway = (Integer) firstPathway.get("pathway_count");
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        pw.put("data", cntPathway);
        data.add(pw);

        // Protein Family
        JSONObject pf = new JSONObject();
        pf.put("description", "Protein Families");
        pf.put("link", URL_PROTEINFAMILY_TAB.replace("{cType}", "genome").replace("{cId}", genomeId));
        pf.put("picture", "/patric/images/icon-popular-proteinfamily.png");

        // Experiment
        JSONObject tr = new JSONObject();
        tr.put("description", "Transcriptomic Experiments");
        tr.put("link", URL_TRANSCRIPTOMICS_TAB.replace("{cType}", "genome").replace("{cId}", genomeId)
                .replace("{kw}", ""));

        long numFound = 0;
        try {
            SolrQuery query = new SolrQuery("genome_ids:" + genomeId);
            query.setRows(0);

            LOGGER.trace("[{}] {}", SolrCore.TRANSCRIPTOMICS_EXPERIMENT.getSolrCoreName(), query);

            String apiResponse = dataApi.solrQuery(SolrCore.TRANSCRIPTOMICS_EXPERIMENT, query);

            Map resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");
            numFound = (Integer) respBody.get("numFound");
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        tr.put("picture", "/patric/images/icon-popular-experiment.png");
        tr.put("data", (int) numFound);

        data.add(tr);

        try {
            SolrQuery query = new SolrQuery(
                    "figfam_id:[* TO *] AND annotation:PATRIC AND genome_id:" + genomeId);
            query.setRows(0);

            LOGGER.trace("[{}] {}", SolrCore.FEATURE.getSolrCoreName(), query);

            String apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query);

            Map resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");

            pf.put("data", respBody.get("numFound"));
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        data.add(pf);

        popGenome.put("popularData", data);

        list.add(popGenome);
    }
    if (list.size() > 0) {
        jsonData = new JSONObject();
        jsonData.put("popularList", list);
        jsonData.put("popularTitle", "Select Genomes");
    }
    return jsonData;
}

From source file:edu.vt.vbi.patric.cache.DataLandingGenerator.java

License:Apache License

private JSONObject getPopularGenomesForPathways() {

    JSONObject jsonData = null;/*  w  w w. j  ava 2 s  .c o m*/
    JSONArray list = new JSONArray();

    for (String genomeId : REFERENCE_GENOME_IDS) {
        Genome genome = dataApi.getGenome(genomeId);

        // construct genome
        JSONObject popGenome = new JSONObject();

        popGenome.put("popularName", genome.getGenomeName());
        popGenome.put("link",
                URL_PATHWAY_TAB.replace("{cType}", "genome").replace("{cId}", genomeId).replace("{pId}", ""));

        JSONArray data = new JSONArray();

        LinkedList<Map<String, String>> pathwayList = new LinkedList<>();

        try {
            //{stat:{field:{field:pathway_id,sort:{ec_count:desc},facet:{ec_count:"unique(ec_number)",gene_count:"unique(feature_id)",field:{field:pathway_name}}}}}
            SolrQuery query = new SolrQuery("genome_id:" + genomeId).addFilterQuery("annotation:PATRIC");
            query.setRows(0).setFacet(true).set("json.facet",
                    "{stat:{field:{field:pathway_id,sort:{ec_count:desc},facet:{ec_count:\"unique(ec_number)\",gene_count:\"unique(feature_id)\",field:{field:pathway_name}}}}}");

            LOGGER.debug("getPopularGenomesForPathways: [{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query);

            String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query);

            Map resp = jsonReader.readValue(apiResponse);
            Map facets = (Map) resp.get("facets");
            List<Map> buckets = (List) ((Map) facets.get("stat")).get("buckets");

            for (Map bucket : buckets) {
                Map<String, String> pathway = new HashMap<>();
                pathway.put("id", bucket.get("val").toString());
                pathway.put("ec_count", bucket.get("ec_count").toString());
                pathway.put("gene_count", bucket.get("gene_count").toString());

                // getting name
                List<Map> subBuckets = (List) ((Map) bucket.get("field")).get("buckets");
                pathway.put("name", subBuckets.get(0).get("val").toString());

                pathwayList.add(pathway);
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }

        for (Map<String, String> pathway : pathwayList) {
            JSONObject pw = new JSONObject();

            pw.put("name", pathway.get("name"));
            pw.put("name_link", URL_PATHWAY_TAB.replace("{cType}", "genome").replace("{cId}", genomeId)
                    .replace("{pId}", pathway.get("id")));
            // pw.put("class", item.get("pathway_class"));
            pw.put("gene_count", pathway.get("gene_count"));
            pw.put("gene_link",
                    URL_PATHWAY_GENE_TAB.replace("{cId}", genomeId).replace("{pId}", pathway.get("id")));
            pw.put("ec_count", pathway.get("ec_count"));
            pw.put("ec_link",
                    URL_PATHWAY_EC_TAB.replace("{cId}", genomeId).replace("{pId}", pathway.get("id")));

            data.add(pw);
        }
        popGenome.put("popularData", data);
        list.add(popGenome);
    }
    if (list.size() > 0) {
        jsonData = new JSONObject();
        jsonData.put("popularList", list);
        jsonData.put("popularTitle", "Select Genomes");
    }
    return jsonData;
}

From source file:edu.vt.vbi.patric.cache.DataLandingGenerator.java

License:Apache License

private JSONObject getPopularGenomesForTranscriptomics() {
    JSONObject jsonData = null;// w  w w .jav  a 2s . co  m
    JSONArray list = new JSONArray();

    for (String genomeId : REFERENCE_GENOME_IDS_TRANSCRIPTOMICS) {

        // construct genome node
        JSONObject popGenome = new JSONObject();

        Genome genome = dataApi.getGenome(genomeId);

        popGenome.put("popularName", genome.getGenomeName());
        popGenome.put("link", URL_TRANSCRIPTOMICS_TAB.replace("{cType}", "genome").replace("{cId}", genomeId)
                .replace("{kw}", ""));

        // Retrieve eId associated a given genome
        List<String> eIds = new ArrayList<>();
        try {
            SolrQuery query = new SolrQuery("genome_ids:" + genomeId);
            query.setRows(1000).setFields("eid");

            LOGGER.trace("[{}] {}", SolrCore.TRANSCRIPTOMICS_EXPERIMENT.getSolrCoreName(), query);

            String apiResponse = dataApi.solrQuery(SolrCore.TRANSCRIPTOMICS_EXPERIMENT, query);

            Map resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");
            List<Map> sdl = (List) respBody.get("docs");

            for (Map doc : sdl) {
                eIds.add(doc.get("eid").toString());
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }

        JSONObject gm = new JSONObject();
        JSONObject ec = new JSONObject();
        gm.put("title", "Gene Modifications");
        ec.put("title", "Experiment Conditions");
        JSONArray data = new JSONArray();
        try {
            Map response = dataApi.getFieldFacets(SolrCore.TRANSCRIPTOMICS_EXPERIMENT, "*:*",
                    "eid:(" + StringUtils.join(eIds, " OR ") + ")", "mutant,condition");
            Map facets = (Map) response.get("facets");

            Map mutantsFacet = (Map) facets.get("mutant");

            int i = 0;
            for (Map.Entry<String, Integer> entry : (Iterable<Map.Entry>) mutantsFacet.entrySet()) {
                JSONObject mutant = new JSONObject();
                mutant.put("label", entry.getKey());
                mutant.put("value", entry.getValue());
                data.add(mutant);
                i++;
                if (i > 4) {
                    break;
                }
            }
            gm.put("data", data);

            // top 5 experiment conditoins
            data = new JSONArray();
            Map conditionsFacet = (Map) facets.get("condition");
            i = 0;
            for (Map.Entry<String, Integer> entry : (Iterable<Map.Entry>) conditionsFacet.entrySet()) {
                JSONObject condition = new JSONObject();
                condition.put("label", entry.getKey());
                condition.put("value", entry.getValue());
                data.add(condition);
                i++;
                if (i > 4) {
                    break;
                }
            }
            ec.put("data", data);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }

        popGenome.put("GeneModifications", gm);
        popGenome.put("ExperimentConditions", ec);

        list.add(popGenome);
    }
    if (list.size() > 0) {
        jsonData = new JSONObject();
        jsonData.put("popularList", list);
        jsonData.put("popularTitle", "Select Genomes");
    }
    return jsonData;
}