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:edu.vt.vbi.patric.portlets.PathwayFinder.java

License:Apache License

@SuppressWarnings("unchecked")
private JSONObject processPathwayTab(DataApiHandler dataApi, String pathwayId, String ecNumber,
        String annotation, String taxonId, String genomeId, String keyword)
        throws PortletException, IOException {

    JSONObject jsonResult = new JSONObject();
    SolrQuery query = new SolrQuery("*:*");

    if (pathwayId != null && !pathwayId.equals("")) {
        query.addFilterQuery("pathway_id:" + pathwayId);
    }//from w  w w.  j a v a  2s  .  co m

    if (ecNumber != null && !ecNumber.equals("")) {
        query.addFilterQuery("ec_number:" + ecNumber);
    }

    if (annotation != null && !annotation.equals("") && !annotation.equalsIgnoreCase("ALL")) {
        query.addFilterQuery("annotation:" + annotation);
    }

    if (taxonId != null && !taxonId.equals("")) {
        query.addFilterQuery(
                SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + taxonId));
    }

    if (genomeId != null && !genomeId.equals("")) {
        query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id",
                "genome_id:(" + genomeId.replaceAll(",", " OR ") + ")"));
    }

    if (keyword != null && !keyword.equals("")) {
        query.setQuery(keyword);
    }

    JSONArray items = new JSONArray();
    int count_total = 0;
    int count_unique = 0;

    try {
        Set<String> listPathwayIds = new HashSet<>();
        Map<String, JSONObject> uniquePathways = new HashMap<>();

        // get pathway stat
        query.setRows(0).setFacet(true);
        query.add("json.facet",
                "{stat:{field:{field:pathway_id,limit:-1,facet:{genome_count:\"unique(genome_id)\",gene_count:\"unique(feature_id)\",ec_count:\"unique(ec_number)\",genome_ec:\"unique(genome_ec)\"}}}}");

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

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

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

        Map<String, Map> mapStat = new HashMap<>();
        for (Map value : buckets) {
            mapStat.put(value.get("val").toString(), value);
            listPathwayIds.add(value.get("val").toString());
        }

        if (!listPathwayIds.isEmpty()) {
            // get pathway list
            SolrQuery pathwayQuery = new SolrQuery(
                    "pathway_id:(" + StringUtils.join(listPathwayIds, " OR ") + ")");
            pathwayQuery.setFields("pathway_id,pathway_name,pathway_class");
            pathwayQuery.setRows(Math.max(dataApi.MAX_ROWS, listPathwayIds.size()));

            LOGGER.trace("processPathwayTab: [{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), pathwayQuery);

            apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, pathwayQuery);
            resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");

            List<Map> sdl = (List<Map>) respBody.get("docs");

            for (Map doc : sdl) {
                String aPathwayId = doc.get("pathway_id").toString();
                Map stat = mapStat.get(aPathwayId);

                if (!uniquePathways.containsKey(aPathwayId)
                        && !stat.get("genome_count").toString().equals("0")) {
                    JSONObject item = new JSONObject();
                    item.put("pathway_id", aPathwayId);
                    item.put("pathway_name", doc.get("pathway_name"));
                    item.put("pathway_class", doc.get("pathway_class"));

                    float genome_ec = Float.parseFloat(stat.get("genome_ec").toString());
                    float genome_count = Float.parseFloat(stat.get("genome_count").toString());
                    float ec_count = Float.parseFloat(stat.get("ec_count").toString());
                    float gene_count = Float.parseFloat(stat.get("gene_count").toString());

                    float ec_cons = 0;
                    float gene_cons = 0;
                    if (genome_count > 0 && ec_count > 0) {
                        ec_cons = genome_ec / genome_count / ec_count * 100;
                        gene_cons = gene_count / genome_count / ec_count;
                    }

                    item.put("ec_cons", ec_cons);
                    item.put("ec_count", ec_count);
                    item.put("gene_cons", gene_cons);
                    item.put("gene_count", gene_count);
                    item.put("genome_count", genome_count);
                    item.put("algorithm", annotation);

                    uniquePathways.put(aPathwayId, item);
                }
            }

            for (Map.Entry<String, JSONObject> pathway : uniquePathways.entrySet()) {
                items.add(pathway.getValue());
            }
            count_total = uniquePathways.entrySet().size();
            count_unique = count_total;
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    // Wrapping jsonResult
    try {
        jsonResult.put("total", count_total);
        jsonResult.put("results", items);
        jsonResult.put("unique", count_unique);
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage(), ex);
    }

    return jsonResult;
}

From source file:edu.vt.vbi.patric.portlets.PathwayFinder.java

License:Apache License

@SuppressWarnings("unchecked")
private JSONObject processEcNumberTab(DataApiHandler dataApi, String pathwayId, String ecNumber,
        String annotation, String taxonId, String genomeId, String keyword)
        throws PortletException, IOException {

    JSONObject jsonResult = new JSONObject();
    SolrQuery query = new SolrQuery("*:*");

    if (pathwayId != null && !pathwayId.equals("")) {
        query.addFilterQuery("pathway_id:" + pathwayId);
    }/*from   www.  j  ava  2  s  .  c  o m*/

    if (ecNumber != null && !ecNumber.equals("")) {
        query.addFilterQuery("ec_number:" + ecNumber);
    }

    if (annotation != null && !annotation.equals("")) {
        query.addFilterQuery("annotation:" + annotation);
    }

    if (taxonId != null && !taxonId.equals("")) {
        query.addFilterQuery(
                SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + taxonId));
    }

    if (genomeId != null && !genomeId.equals("")) {
        query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id",
                "genome_id:(" + genomeId.replaceAll(",", " OR ") + ")"));
    }

    if (keyword != null && !keyword.equals("")) {
        query.setQuery(keyword);
    }

    JSONArray items = new JSONArray();
    int count_total = 0;
    int count_unique = 0;

    try {
        Set<String> listPathwayIds = new HashSet<>();
        Set<String> listEcNumbers = new HashSet<>();

        // get pathway stat
        query.setRows(0).setFacet(true);
        query.add("json.facet",
                "{stat:{field:{field:pathway_ec,limit:-1,facet:{genome_count:\"unique(genome_id)\",gene_count:\"unique(feature_id)\",ec_count:\"unique(ec_number)\"}}}}");

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

        String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query);
        Map resp = jsonReader.readValue(apiResponse);
        List<Map> buckets = (List<Map>) ((Map) ((Map) resp.get("facets")).get("stat")).get("buckets");

        Map<String, Map> mapStat = new HashMap<>();
        for (Map value : buckets) {

            if (!value.get("genome_count").toString().equals("0")) {
                mapStat.put(value.get("val").toString(), value);

                String[] pathway_ec = value.get("val").toString().split("_");
                listPathwayIds.add(pathway_ec[0]);
                listEcNumbers.add(pathway_ec[1]);
            }
        }

        // get pathway list
        SolrQuery pathwayQuery = new SolrQuery("*:*");
        if (!listPathwayIds.isEmpty()) {
            pathwayQuery.setQuery("pathway_id:(" + StringUtils.join(listPathwayIds, " OR ") + ")");

            pathwayQuery.setFields("pathway_id,pathway_name,pathway_class,ec_number,ec_description");
            pathwayQuery.setRows(Math.max(1000000, listPathwayIds.size()));

            LOGGER.trace("processEcNumberTab: [{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), pathwayQuery);

            apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, pathwayQuery);
            resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");

            List<Map> sdl = (List<Map>) respBody.get("docs");

            for (Map doc : sdl) {
                String aPathwayId = doc.get("pathway_id").toString();
                String aEcNumber = doc.get("ec_number").toString();
                Map stat = mapStat.get(aPathwayId + "_" + aEcNumber);

                if (stat != null && !stat.get("genome_count").toString().equals("0")) {
                    JSONObject item = new JSONObject();
                    item.put("pathway_id", aPathwayId);
                    item.put("pathway_name", doc.get("pathway_name"));
                    item.put("pathway_class", doc.get("pathway_class"));

                    float genome_count = Float.parseFloat(stat.get("genome_count").toString());
                    float gene_count = Float.parseFloat(stat.get("gene_count").toString());

                    item.put("ec_name", doc.get("ec_description"));
                    item.put("ec_number", doc.get("ec_number"));
                    item.put("gene_count", gene_count);
                    item.put("genome_count", genome_count);
                    item.put("algorithm", annotation);

                    items.add(item);
                }
            }
            count_total = items.size();
            count_unique = listEcNumbers.size();
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    // Wrapping jsonResult
    try {
        jsonResult.put("total", count_total);
        jsonResult.put("results", items);
        jsonResult.put("unique", count_unique);
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage(), ex);
    }

    return jsonResult;
}

From source file:edu.vt.vbi.patric.portlets.TranscriptomicsEnrichment.java

License:Apache License

private JSONObject getEnrichmentPathway(ResourceRequest request, List<String> featureIDs) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);

    // 1. get Pathway ID, Pathway Name & genomeID
    //solr/pathway/select?q=feature_id:(PATRIC.83332.12.NC_000962.CDS.34.1524.fwd)&fl=pathway_name,pathway_id,gid

    Map<String, JSONObject> pathwayMap = new LinkedHashMap<>();
    Set<String> listFeatureID = new HashSet<>();
    Set<String> listGenomeID = new HashSet<>();
    Set<String> listPathwayID = new HashSet<>();

    SolrQuery query = new SolrQuery("feature_id:(" + StringUtils.join(featureIDs, " OR ") + ")");
    int queryRows = Math.max(dataApi.MAX_ROWS, (featureIDs.size() * 2));
    query.addField("pathway_name,pathway_id,genome_id,feature_id").setRows(queryRows);

    LOGGER.trace("Enrichment 1/3: [{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query);

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

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

    for (Map doc : pathwayList) {
        JSONObject pw = new JSONObject();
        pw.put("pathway_id", doc.get("pathway_id"));
        pw.put("pathway_name", doc.get("pathway_name"));
        pathwayMap.put(doc.get("pathway_id").toString(), pw);

        // LOGGER.debug("{}", pw.toJSONString());
        listFeatureID.add(doc.get("feature_id").toString());
        listGenomeID.add(doc.get("genome_id").toString());
        listPathwayID.add(doc.get("pathway_id").toString());
    }/*from   w  w w  . j a  v  a  2  s  . co  m*/

    // 2. get pathway ID & Ocnt
    //solr/pathway/select?q=feature_id:(PATRIC.83332.12.NC_000962.CDS.34.1524.fwd)&rows=0&facet=true
    // &json.facet={stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:"unique(feature_id)"}}}}
    query = new SolrQuery("feature_id:(" + StringUtils.join(featureIDs, " OR ") + ")");
    query.setRows(0).setFacet(true);
    query.add("json.facet",
            "{stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:\"unique(feature_id)\"}}}}");

    LOGGER.trace("Enrichment 2/3: [{}] {}", SolrCore.PATHWAY.getSolrCoreName(),
            URLDecoder.decode(query.toString(), "UTF-8"));

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

    resp = jsonReader.readValue(apiResponse);
    Map facets = (Map) resp.get("facets");
    if ((Integer) facets.get("count") > 0) {
        Map stat = (Map) facets.get("stat");
        List<Map> buckets = (List) stat.get("buckets");

        for (Map value : buckets) {
            String aPathwayId = value.get("val").toString();

            if (pathwayMap.containsKey(aPathwayId)) {
                pathwayMap.get(aPathwayId).put("ocnt", value.get("gene_count"));
            }
        }
    }

    // 3. with genomeID, get pathway ID & Ecnt
    //solr/pathway/select?q=genome_id:83332.12 AND pathway_id:(00230 OR 00240)&fq=annotation:PATRIC&rows=0&facet=true //&facet.mincount=1&facet.limit=-1
    // &json.facet={stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:"unique(feature_id)"}}}}
    if (!listGenomeID.isEmpty() && !listPathwayID.isEmpty()) {
        query = new SolrQuery("genome_id:(" + StringUtils.join(listGenomeID, " OR ") + ") AND pathway_id:("
                + StringUtils.join(listPathwayID, " OR ") + ")");
        query.setRows(0).setFacet(true).addFilterQuery("annotation:PATRIC");
        query.add("json.facet",
                "{stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:\"unique(feature_id)\"}}}}");

        LOGGER.trace("Enrichment 3/3: {}", query.toString());

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

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

        for (Map value : buckets) {
            pathwayMap.get(value.get("val").toString()).put("ecnt", value.get("gene_count"));
        }
    }

    // 4. Merge hash and calculate percentage on the fly
    JSONObject jsonResult = new JSONObject();
    JSONArray results = new JSONArray();
    for (JSONObject item : pathwayMap.values()) {
        if (item.get("ecnt") != null && item.get("ocnt") != null) {
            float ecnt = Float.parseFloat(item.get("ecnt").toString());
            float ocnt = Float.parseFloat(item.get("ocnt").toString());
            float percentage = ocnt / ecnt * 100;
            item.put("percentage", (int) percentage);
            results.add(item);
        }
    }
    jsonResult.put("results", results);
    jsonResult.put("total", results.size());
    jsonResult.put("featureRequested", featureIDs.size());
    jsonResult.put("featureFound", listFeatureID.size());

    return jsonResult;
}

From source file:edu.vt.vbi.patric.proteinfamily.FIGfamData.java

License:Apache License

@SuppressWarnings("unchecked")
public void getGroupStats(ResourceRequest request, PrintWriter writer) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);

    JSONObject figfams = new JSONObject();
    Set<String> figfamIdList = new HashSet<>();
    List<String> genomeIdList = new LinkedList<>();
    // get family Type
    final String familyType = request.getParameter("familyType");
    final String familyId = familyType + "_id";

    // get genome list in order
    String genomeIds = request.getParameter("genomeIds");
    try {/*from  w w w . ja v  a 2  s .  c  om*/
        SolrQuery query = new SolrQuery("genome_id:(" + genomeIds.replaceAll(",", " OR ") + ")");
        query.addSort("genome_name", SolrQuery.ORDER.asc).addField("genome_id")
                .setRows(DataApiHandler.MAX_ROWS);

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

        String apiResponse = dataApi.solrQuery(SolrCore.GENOME, query);
        Map resp = jsonReader.readValue(apiResponse);
        Map respBody = (Map) resp.get("response");

        List<Genome> genomes = dataApi.bindDocuments((List<Map>) respBody.get("docs"), Genome.class);

        for (final Genome genome : genomes) {
            genomeIdList.add(genome.getId());
        }

        if (genomeIdList.size() == 25000) {
            query.setStart(25000);

            apiResponse = dataApi.solrQuery(SolrCore.GENOME, query);
            resp = jsonReader.readValue(apiResponse);
            respBody = (Map) resp.get("response");

            genomes = dataApi.bindDocuments((List<Map>) respBody.get("docs"), Genome.class);

            for (final Genome genome : genomes) {
                genomeIdList.add(genome.getId());
            }
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    //      LOGGER.debug("genomeIdList: {}", genomeIdList);

    // getting genome counts per figfamID (figfam)
    // {stat:{field:{field:figfam_id,limit:-1,facet:{min:"min(aa_length)",max:"max(aa_length)",mean:"avg(aa_length)",ss:"sumsq(aa_length)",sum:"sum(aa_length)",dist:"percentile(aa_length,50,75,99,99.9)",field:{field:genome_id}}}}}

    try {
        long start = System.currentTimeMillis();
        SolrQuery query = new SolrQuery("annotation:PATRIC AND feature_type:CDS");
        //         query.addFilterQuery("end:[3200 TO 4300] OR end:[4400 TO 4490] OR end:[4990 TO 4999]");
        query.addFilterQuery(getSolrQuery(request));
        query.addFilterQuery("!" + familyId + ":\"\"");
        query.setRows(0).setFacet(true).set("facet.threads", 15);
        query.add("json.facet", "{stat:{type:field,field:genome_id,limit:-1,facet:{figfams:{type:field,field:"
                + familyId + ",limit:-1,sort:{index:asc}}}}}");

        LOGGER.trace("getGroupStats() 1/3: [{}] {}", SolrCore.FEATURE.getSolrCoreName(), query);
        String apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query);

        long point = System.currentTimeMillis();
        LOGGER.debug("1st query: {} ms", (point - start));
        start = point;

        Map resp = jsonReader.readValue(apiResponse);
        Map facets = (Map) resp.get("facets");
        Map stat = (Map) facets.get("stat");

        final Map<String, String> figfamGenomeIdStr = new LinkedHashMap<>();
        final Map<String, Integer> figfamGenomeCount = new LinkedHashMap<>();

        final int genomeTotal = genomeIdList.size();
        final Map<String, Integer> genomePosMap = new LinkedHashMap<>();
        for (String genomeId : genomeIdList) {
            genomePosMap.put(genomeId, genomeIdList.indexOf(genomeId));
        }

        final Map<String, List> figfamGenomeIdCountMap = new ConcurrentHashMap<>();
        final Map<String, Set> figfamGenomeIdSet = new ConcurrentHashMap<>();

        List<Map> genomeBuckets = (List<Map>) stat.get("buckets");

        for (final Map bucket : genomeBuckets) {

            final String genomeId = (String) bucket.get("val");
            final List<Map> figfamBucket = (List<Map>) ((Map) bucket.get("figfams")).get("buckets");

            for (final Map figfam : figfamBucket) {
                final String figfamId = (String) figfam.get("val");
                final String genomeCount = String.format("%02x", (Integer) figfam.get("count"));

                if (figfamGenomeIdCountMap.containsKey(figfamId)) {
                    figfamGenomeIdCountMap.get(figfamId).set(genomePosMap.get(genomeId), genomeCount);
                } else {
                    final List<String> genomeIdCount = new LinkedList<>(Collections.nCopies(genomeTotal, "00"));
                    genomeIdCount.set(genomePosMap.get(genomeId), genomeCount);
                    figfamGenomeIdCountMap.put(figfamId, genomeIdCount);
                }

                if (figfamGenomeIdSet.containsKey(figfamId)) {
                    figfamGenomeIdSet.get(figfamId).add(genomeId);
                } else {
                    final Set<String> genomeIdSet = new HashSet<>();
                    genomeIdSet.add(genomeId);
                    figfamGenomeIdSet.put(figfamId, genomeIdSet);
                }
            }
        }

        for (String figfamId : figfamGenomeIdCountMap.keySet()) {
            final List genomeIdStr = figfamGenomeIdCountMap.get(figfamId);
            figfamGenomeIdStr.put(figfamId, StringUtils.join(genomeIdStr, ""));
            figfamGenomeCount.put(figfamId, figfamGenomeIdSet.get(figfamId).size());
        }

        point = System.currentTimeMillis();
        LOGGER.debug("1st query process : {} ms, figfamGenomeIdStr:{}, figfamGenomeCount:{}", (point - start),
                figfamGenomeIdStr.size(), figfamGenomeCount.size());

        long start2nd = System.currentTimeMillis();
        // 2nd query

        query.set("json.facet", "{stat:{type:field,field:" + familyId
                + ",limit:-1,facet:{min:\"min(aa_length)\",max:\"max(aa_length)\",mean:\"avg(aa_length)\",ss:\"sumsq(aa_length)\",sum:\"sum(aa_length)\"}}}");

        LOGGER.trace("getGroupStats() 2/3: [{}] {}", SolrCore.FEATURE.getSolrCoreName(), query);
        apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query);

        point = System.currentTimeMillis();
        LOGGER.debug("2st query: {} ms", (point - start2nd));
        start2nd = point;

        resp = jsonReader.readValue(apiResponse);
        facets = (Map) resp.get("facets");
        stat = (Map) facets.get("stat");

        List<Map> buckets = (List<Map>) stat.get("buckets");

        for (Map bucket : buckets) {
            final String figfamId = (String) bucket.get("val");
            final int count = (Integer) bucket.get("count");

            double min, max, mean, sumsq, sum;
            if (bucket.get("min") instanceof Double) {
                min = (Double) bucket.get("min");
            } else if (bucket.get("min") instanceof Integer) {
                min = ((Integer) bucket.get("min")).doubleValue();
            } else {
                min = 0;
            }
            if (bucket.get("max") instanceof Double) {
                max = (Double) bucket.get("max");
            } else if (bucket.get("max") instanceof Integer) {
                max = ((Integer) bucket.get("max")).doubleValue();
            } else {
                max = 0;
            }
            if (bucket.get("mean") instanceof Double) {
                mean = (Double) bucket.get("mean");
            } else if (bucket.get("mean") instanceof Integer) {
                mean = ((Integer) bucket.get("mean")).doubleValue();
            } else {
                mean = 0;
            }
            if (bucket.get("ss") instanceof Double) {
                sumsq = (Double) bucket.get("ss");
            } else if (bucket.get("ss") instanceof Integer) {
                sumsq = ((Integer) bucket.get("ss")).doubleValue();
            } else {
                sumsq = 0;
            }
            if (bucket.get("sum") instanceof Double) {
                sum = (Double) bucket.get("sum");
            } else if (bucket.get("sum") instanceof Integer) {
                sum = ((Integer) bucket.get("sum")).doubleValue();
            } else {
                sum = 0;
            }

            //            LOGGER.debug("bucket:{}, sumsq:{}, count: {}", bucket, sumsq, count);
            double std;
            if (count > 1) {
                // std = Math.sqrt(sumsq / (count - 1));
                final double realSq = sumsq - (sum * sum) / count;
                std = Math.sqrt(realSq / (count - 1));
            } else {
                std = 0;
            }
            final JSONObject aaLength = new JSONObject();
            aaLength.put("min", min);
            aaLength.put("max", max);
            aaLength.put("mean", mean);
            aaLength.put("stddev", std);

            figfamIdList.add(figfamId);

            final JSONObject figfam = new JSONObject();
            figfam.put("genomes", figfamGenomeIdStr.get(figfamId));
            figfam.put("genome_count", figfamGenomeCount.get(figfamId));
            figfam.put("feature_count", count);
            figfam.put("stats", aaLength);

            figfams.put(figfamId, figfam);
        }

        point = System.currentTimeMillis();
        LOGGER.debug("2st query process: {} ms", (point - start2nd));
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }

    // getting distinct figfam_product
    if (!figfamIdList.isEmpty()) {

        figfamIdList.remove("");

        try {
            SolrQuery query = new SolrQuery("family_id:(" + StringUtils.join(figfamIdList, " OR ") + ")");
            query.addFilterQuery("family_type:" + familyType);
            query.addField("family_id,family_product").setRows(figfamIdList.size());

            LOGGER.debug("getGroupStats() 3/3: [{}] {}", SolrCore.FIGFAM_DIC.getSolrCoreName(), query);

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

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

            List<Map> sdl = (List<Map>) respBody.get("docs");

            for (final Map doc : sdl) {
                final JSONObject figfam = (JSONObject) figfams.get(doc.get("family_id"));
                figfam.put("description", doc.get("family_product"));
                figfams.put(doc.get("family_id").toString(), figfam);
            }

            int i = 1;
            while (sdl.size() == 25000) {
                query.setStart(25000 * i);

                apiResponse = dataApi.solrQuery(SolrCore.FIGFAM_DIC, query);
                resp = jsonReader.readValue(apiResponse);
                respBody = (Map) resp.get("response");

                sdl = (List<Map>) respBody.get("docs");

                for (final Map doc : sdl) {
                    final JSONObject figfam = (JSONObject) figfams.get(doc.get("family_id"));
                    figfam.put("description", doc.get("family_product"));
                    figfams.put(doc.get("family_id").toString(), figfam);
                }
                i++;
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            LOGGER.debug("::getGroupStats() 3/3, params: {}", request.getParameterMap().toString());
        }
        figfams.writeJSONString(writer);
    }
}

From source file:fr.cnes.sitools.metacatalogue.resources.opensearch.AbstractOpensearchSearchResource.java

License:Open Source License

protected Representation opensearchQuery(Representation repr, Form query, SolrServer server,
        ThesaurusSearcher searcher) {/*from ww w .ja  v a2s .c  om*/
    SolrQuery solrQuery = new SolrQuery();

    Integer count = getIntegerParameter(query, OpenSearchQuery.COUNT);
    Integer startIndex = getIntegerParameter(query, OpenSearchQuery.START_INDEX);
    Integer startPage = getIntegerParameter(query, OpenSearchQuery.START_PAGE);

    int start = count * (startPage - 1) + startIndex - 1; // index in solr start at 0, not a 1
    if (start < 0) {
        start = 0;
    }
    int rows = count;

    solrQuery.setStart(start);
    solrQuery.setRows(rows);
    solrQuery.add("df", "searchTerms");

    // addGeometryCriteria(solrQuery, query);
    try {
        setQuery(solrQuery, query);
        setFacet(solrQuery);

        getLogger().log(Level.INFO, "SOLR query : " + solrQuery.toString());

        QueryResponse rsp = server.query(solrQuery);

        boolean isAuthenticated = getClientInfo().isAuthenticated();
        SitoolsSettings settings = getSettings();
        String applicationBaseUrl = settings.getPublicHostDomain() + application.getAttachementRef();

        // Reference ref = new Reference(getRequest().getResourceRef().getBaseRef());
        Reference ref = new Reference(applicationBaseUrl + "/" + getReference().getLastSegment());

        ref.setQuery(query.getQueryString());

        repr = new GeoJsonMDEORepresentation(rsp, isAuthenticated, applicationBaseUrl, ref, 1,
                OpenSearchQuery.START_INDEX.getParamName(), OpenSearchQuery.COUNT.getParamName(),
                searcher.getAllConceptsAsMap(getLanguage()), thesaurusFacetFields);
    } catch (SolrServerException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Error while querying solr index", e);
    } catch (Exception e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Error while querying solr index", e);
    }
    return repr;
}

From source file:fr.cnes.sitools.metacatalogue.resources.opensearch.AbstractOpensearchSearchResource.java

License:Open Source License

private void setFacet(SolrQuery solrQuery) {

    solrQuery.addFacetField(MetacatalogField._RESOLUTION_DOMAIN.getField());
    // Date dateStart;
    // try {//from   w  ww .j av a 2  s. com
    // dateStart = DateUtils.parse("1980-01-01T00:00:00.0");
    // solrQuery.addDateRangeFacet("characterisationAxis.temporalAxis.min", dateStart, new Date(), "+1YEAR");
    // }
    // catch (ParseException e) {
    // e.printStackTrace();
    // }

    List<String> plateformIntrument = new ArrayList<String>();
    plateformIntrument.add(MetacatalogField.PLATFORM.getField());
    plateformIntrument.add(MetacatalogField.INSTRUMENT.getField());

    List<String> location = new ArrayList<String>();
    location.add(MetacatalogField.COUNTRY.getField());
    location.add(MetacatalogField.REGION.getField());
    location.add(MetacatalogField.DEPARTMENT.getField());
    location.add(MetacatalogField.CITY.getField());

    solrQuery.add("facet.pivot", Joiner.on(",").join(plateformIntrument));
    solrQuery.add("facet.pivot", Joiner.on(",").join(location));
    solrQuery.addFacetField(MetacatalogField.PROCESSING_LEVEL.getField());
    solrQuery.addFacetField(MetacatalogField.PRODUCT.getField());
    solrQuery.add("facet.pivot.mincount", "0");
    solrQuery.setFacetLimit(10);
    solrQuery.setFacetMinCount(1);

}

From source file:fr.gael.dhus.search.SearchResult.java

License:Open Source License

void initQuery() {
    if (query == null) {
        SolrQuery query = new SolrQuery();
        query.setQuery(BOOST_FUNCTION_SECTION + squery);
        query.setRows(fetchSize);// www  .  ja v  a2 s .co m
        if (this.filterQuery != null) {
            query.addFilterQuery(this.filterQuery);
        }

        if (Boolean.parseBoolean(System.getProperty("solr.filter.user", "false"))) {
            SecurityService secuService = ApplicationContextProvider.getBean(SecurityService.class);

            User user = secuService.getCurrentUser();
            if (user == null) {
                user = ApplicationContextProvider.getBean(UserDao.class).getRootUser();
            }
            query.add(SolrUtils.CURRENT_USER_ID, user.getId().toString());
        }

        QueryResponse rsp;
        try {
            rsp = server.query(query, SolrRequest.METHOD.POST);
        } catch (SolrServerException e) {
            logger.error("Error in query \"" + query + "\" : " + e.getMessage());
            totalResults = 0;
            this.query = query;
            return;
            // throw new DHusSearchException ("Cannot execute query", e);
        } catch (SolrException e) {
            logger.error("Error in query \"" + query + "\" : " + e.getMessage());
            totalResults = 0;
            this.query = query;
            return;
            // throw new DHusSearchException ("Cannot execute query", e);
        }
        totalResults = rsp.getResults().getNumFound();
        this.query = query;
    }
}

From source file:net.yacy.crawler.data.CrawlQueues.java

License:Open Source License

public boolean autocrawlJob() {
    if (!this.sb.getConfigBool(SwitchboardConstants.AUTOCRAWL, false)) {
        return false;
    }//from  ww w. j  av  a  2 s  .  c o  m

    if (isPaused(SwitchboardConstants.CRAWLJOB_LOCAL_CRAWL)) {
        return false;
    }

    if (coreCrawlJobSize() > 200) {
        return false;
    }

    String rows = this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_ROWS, "100");

    String dateQuery = String.format("load_date_dt:[* TO NOW-%sDAY]",
            this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_DAYS, "1"));

    final SolrQuery query = new SolrQuery();
    query.add("group", "true");
    query.add("group.field", "host_s");
    query.add("group.limit", "1");
    query.add("group.main", "true");
    query.add("rows", rows);
    query.setQuery(this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_QUERY, "*:*"));
    query.setFields("host_s,url_protocol_s");
    query.addSort("load_date_dt", SolrQuery.ORDER.asc);
    query.addFilterQuery(dateQuery);

    try {
        QueryResponse resp = sb.index.fulltext().getDefaultConnector().getResponseByParams(query);

        int i = 0;
        int deepRatio = Integer.parseInt(this.sb.getConfig(SwitchboardConstants.AUTOCRAWL_RATIO, "50"));
        for (SolrDocument doc : resp.getResults()) {
            boolean deep = false;
            i++;
            if (i % deepRatio == 0) {
                deep = true;
            }
            DigestURL url;
            final String u = doc.getFieldValue("url_protocol_s").toString() + "://"
                    + doc.getFieldValue("host_s").toString();
            try {
                url = new DigestURL(u);
            } catch (final MalformedURLException e) {
                continue;
            }
            final String urlRejectReason = this.sb.crawlStacker.urlInAcceptedDomain(url);
            if (urlRejectReason == null) {
                this.sb.crawlStacker.enqueueEntry(new Request(ASCII.getBytes(this.sb.peers.mySeed().hash), url,
                        null, "CRAWLING-ROOT", new Date(),
                        deep ? this.sb.crawler.defaultAutocrawlDeepProfile.handle()
                                : this.sb.crawler.defaultAutocrawlShallowProfile.handle(),
                        0, deep ? this.sb.crawler.defaultAutocrawlDeepProfile.timezoneOffset()
                                : this.sb.crawler.defaultAutocrawlShallowProfile.timezoneOffset()));
            } else {
                CrawlQueues.log.warn("autocrawl: Rejected URL '" + urlToString(url) + "': " + urlRejectReason);
            }
        }

    } catch (SolrException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return true;
}

From source file:org.ala.dao.FulltextSearchDaoImplSolr.java

License:Open Source License

/**
 * Helper method to create SolrQuery object and add facet settings
 *
 * @return solrQuery the SolrQuery//from  ww  w . j a  v  a 2  s.  c o  m
 */
protected SolrQuery initSolrQuery(String[] facets) {
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType("standard");
    if (facets == null) {
        //use the default set
        solrQuery.setFacet(true);
        solrQuery.addFacetField("idxtype");
        solrQuery.addFacetField("australian_s");
        solrQuery.addFacetField("speciesGroup");
        solrQuery.addFacetField("speciesSubgroup");
        //solrQuery.addFacetField("kingdom");
        solrQuery.addFacetField("rank");
        //solrQuery.addFacetField("rankId");
        //solrQuery.addFacetField("pestStatus");
        //        solrQuery.addFacetField("conservationStatus");
        solrQuery.addFacetField("conservationStatusAUS");
        solrQuery.addFacetField("conservationStatusACT");
        solrQuery.addFacetField("conservationStatusNSW");
        solrQuery.addFacetField("conservationStatusNT");
        solrQuery.addFacetField("conservationStatusQLD");
        solrQuery.addFacetField("conservationStatusSA");
        solrQuery.addFacetField("conservationStatusTAS");
        solrQuery.addFacetField("conservationStatusVIC");
        solrQuery.addFacetField("conservationStatusWA");
        solrQuery.addFacetField("category_m_s");
        solrQuery.addFacetField("category_NSW_m_s");
        solrQuery.addFacetField("category_ACT_m_s");
        solrQuery.addFacetField("category_QLD_m_s");
        solrQuery.addFacetField("category_SA_m_s");
        solrQuery.addFacetField("category_NT_m_s");
        solrQuery.addFacetField("category_TAS_m_s");
        solrQuery.addFacetField("category_WA_m_s");
        solrQuery.addFacetField("category_VIC_m_s");
    } else {
        solrQuery.addFacetField(facets);
    }

    solrQuery.setFacetMinCount(1);
    solrQuery.setRows(10);
    solrQuery.setStart(0);

    //add highlights
    solrQuery.setHighlight(true);
    solrQuery.setHighlightFragsize(80);
    solrQuery.setHighlightSnippets(2);
    solrQuery.setHighlightSimplePre("<strong>");
    solrQuery.setHighlightSimplePost("</strong>");
    solrQuery.add("hl.usePhraseHighlighter", "true");
    solrQuery.addHighlightField("commonName");
    solrQuery.addHighlightField("scientificName");
    solrQuery.addHighlightField("pestStatus");
    solrQuery.addHighlightField("conservationStatus");
    solrQuery.addHighlightField("simpleText");
    solrQuery.addHighlightField("content");

    return solrQuery;
}

From source file:org.apache.jena.query.text.TextIndexSolr.java

License:Apache License

private SolrDocumentList solrQuery(String qs, int limit) {
    SolrQuery sq = new SolrQuery(qs);
    sq.setIncludeScore(true);/*from w  ww.  java  2 s  . c  om*/
    if (limit > 0)
        sq.setRows(limit);
    else
        sq.setRows(MAX_N); // The Solr default is 10.
    try {
        // Set default field.
        sq.add(CommonParams.DF, docDef.getPrimaryField());
        QueryResponse rsp = solrServer.query(sq);
        SolrDocumentList docs = rsp.getResults();
        return docs;
    } catch (SolrServerException e) {
        exception(e);
        return null;
    }
}