Example usage for org.apache.solr.client.solrj.response FacetField.Count getName

List of usage examples for org.apache.solr.client.solrj.response FacetField.Count getName

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.response FacetField.Count getName.

Prototype

public String getName() 

Source Link

Usage

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * Writes the values for the first supplied facet to output stream
 *
 * @param includeCount true when the count should be included in the download
 * @param lookupName   true when a name lsid should be looked up in the bie
 *//*w  ww  . j a v  a 2  s  .  c o  m*/
public void writeFacetToStream(SpatialSearchRequestParams searchParams, boolean includeCount,
        boolean lookupName, boolean includeSynonyms, boolean includeLists, OutputStream out,
        DownloadDetailsDTO dd) throws Exception {
    //set to unlimited facets
    searchParams.setFlimit(-1);
    queryFormatUtils.formatSearchQuery(searchParams);
    String queryString = searchParams.getFormattedQuery();
    SolrQuery solrQuery = initSolrQuery(searchParams, false, null);
    solrQuery.setQuery(queryString);

    //don't want any results returned
    solrQuery.setRows(0);
    searchParams.setPageSize(0);
    solrQuery.setFacetLimit(FACET_PAGE_SIZE);
    int offset = 0;
    boolean shouldLookup = lookupName
            && (searchParams.getFacets()[0].contains("_guid") || searchParams.getFacets()[0].contains("_lsid"));

    if (dd != null) {
        dd.resetCounts();
    }

    QueryResponse qr = runSolrQuery(solrQuery, searchParams);
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieved facet results from server...");
    }
    if (qr.getResults().getNumFound() > 0) {
        FacetField ff = qr.getFacetField(searchParams.getFacets()[0]);

        //write the header line
        if (ff != null) {
            String[] header = new String[] { ff.getName() };
            // out.write(ff.getName().getBytes());
            if (shouldLookup) {
                header = speciesLookupService.getHeaderDetails(ff.getName(), includeCount, includeSynonyms);
            } else if (includeCount) {
                //out.write(",Count".getBytes());
                header = (String[]) ArrayUtils.add(header, "count");
            }
            if (includeLists) {
                header = (String[]) ArrayUtils.addAll(header, listsService.getTypes().toArray(new String[] {}));
            }

            CSVRecordWriter writer = new CSVRecordWriter(new CloseShieldOutputStream(out), header);
            try {
                boolean addedNullFacet = false;

                //out.write("\n".getBytes());
                //PAGE through the facets until we reach the end.
                //do not continue when null facet is already added and the next facet is only null
                while (ff.getValueCount() > 1 || !addedNullFacet
                        || (ff.getValueCount() == 1 && ff.getValues().get(0).getName() != null)) {
                    //process the "species_guid_ facet by looking up the list of guids
                    if (shouldLookup) {
                        List<String> guids = new ArrayList<String>();
                        List<Long> counts = new ArrayList<Long>();
                        List<String[]> speciesLists = new ArrayList<String[]>();
                        if (logger.isDebugEnabled()) {
                            logger.debug("Downloading " + ff.getValueCount() + " species guids");
                        }
                        for (FacetField.Count value : ff.getValues()) {
                            //only add null facet once
                            if (value.getName() == null)
                                addedNullFacet = true;
                            if (value.getCount() == 0 || (value.getName() == null && addedNullFacet))
                                continue;

                            guids.add(value.getName());
                            if (includeCount) {
                                counts.add(value.getCount());
                            }

                            //Only want to send a sub set of the list so that the URI is not too long for BIE
                            if (guids.size() == 30) {
                                //now get the list of species from the web service TODO may need to move this code
                                //handle null values being returned from the service...
                                writeTaxonDetailsToStream(guids, counts, includeCount, includeSynonyms,
                                        includeLists, writer);
                                guids.clear();
                                counts.clear();
                            }
                        }
                        //now write any guids that remain at the end of the looping
                        writeTaxonDetailsToStream(guids, counts, includeCount, includeSynonyms, includeLists,
                                writer);
                    } else {
                        //default processing of facets
                        for (FacetField.Count value : ff.getValues()) {
                            //only add null facet once
                            if (value.getName() == null)
                                addedNullFacet = true;
                            if (value.getCount() == 0 || (value.getName() == null && addedNullFacet))
                                continue;

                            String name = value.getName() != null ? value.getName() : "";
                            String[] row = includeCount ? new String[] { name, Long.toString(value.getCount()) }
                                    : new String[] { name };
                            writer.write(row);
                        }
                    }
                    offset += FACET_PAGE_SIZE;
                    if (dd != null) {
                        dd.updateCounts(FACET_PAGE_SIZE);
                    }

                    //get the next values
                    solrQuery.remove("facet.offset");
                    solrQuery.add("facet.offset", Integer.toString(offset));
                    qr = runSolrQuery(solrQuery, searchParams);
                    ff = qr.getFacetField(searchParams.getFacets()[0]);
                }
            } finally {
                writer.finalise();
            }
        }
    }
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * Writes all the distinct latitude and longitude in the index to the supplied
 * output stream.//from w  w  w.j  a  v  a 2 s  .c  o  m
 *
 * @param out
 * @throws Exception
 */
public void writeCoordinatesToStream(SearchRequestParams searchParams, OutputStream out) throws Exception {
    //generate the query to obtain the lat,long as a facet
    SearchRequestParams srp = new SearchRequestParams();
    SearchUtils.setDefaultParams(srp);
    srp.setFacets(searchParams.getFacets());

    SolrQuery solrQuery = initSolrQuery(srp, false, null);
    //We want all the facets so we can dump all the coordinates
    solrQuery.setFacetLimit(-1);
    solrQuery.setFacetSort("count");
    solrQuery.setRows(0);
    solrQuery.setQuery(searchParams.getQ());

    QueryResponse qr = runSolrQuery(solrQuery, srp);
    if (qr.getResults().size() > 0) {
        FacetField ff = qr.getFacetField(searchParams.getFacets()[0]);
        if (ff != null && ff.getValueCount() > 0) {
            out.write("latitude,longitude\n".getBytes());
            //write the facets to file
            for (FacetField.Count value : ff.getValues()) {
                //String[] slatlon = value.getName().split(",");
                if (value.getName() != null && value.getCount() > 0) {
                    out.write(value.getName().getBytes());
                    out.write("\n".getBytes());
                }
            }
        }
    }
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

private String getQAFromFacet(FacetField facet) {
    StringBuilder qasb = new StringBuilder();
    for (FacetField.Count facetEntry : facet.getValues()) {
        if (facetEntry.getCount() > 0) {
            if (qasb.length() > 0) {
                qasb.append(",");
            }//from  www  . j av  a2s .  co  m
            if (facetEntry.getName() != null) {
                qasb.append(facetEntry.getName());
            }
        }
    }
    return qasb.toString();
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * Initialises the download limit tracking
 *
 * @param map//  w w  w  .  j  av  a2 s .co m
 * @param facet
 */
private void initDownloadLimits(Map<String, Integer> map, FacetField facet) {
    //get the download limits from the cache
    Map<String, Integer> limits = collectionCache.getDownloadLimits();
    for (FacetField.Count facetEntry : facet.getValues()) {
        String name = facetEntry.getName() != null ? facetEntry.getName() : "";
        Integer limit = limits.get(name);
        if (limit != null && limit > 0) {
            //check to see if the number of records returned from the query execeeds the limit
            if (limit < facetEntry.getCount())
                map.put(name, limit);
        }
    }
    if (logger.isDebugEnabled() && map.size() > 0) {
        logger.debug("Downloading with the following limits: " + map);
    }
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

private List<OccurrencePoint> getPoints(SpatialSearchRequestParams searchParams, PointType pointType, int max)
        throws Exception {
    List<OccurrencePoint> points = new ArrayList<OccurrencePoint>(); // new OccurrencePoint(PointType.POINT);
    queryFormatUtils.formatSearchQuery(searchParams);
    if (logger.isInfoEnabled()) {
        logger.info("search query: " + searchParams.getFormattedQuery());
    }/*from   w  ww  . j a  va 2s. c  o  m*/
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType("standard");
    solrQuery.setQuery(searchParams.getFormattedQuery());
    solrQuery.setRows(0);
    solrQuery.setFacet(true);
    solrQuery.addFacetField(pointType.getLabel());
    solrQuery.setFacetMinCount(1);
    solrQuery.setFacetLimit(max); // unlimited = -1

    QueryResponse qr = runSolrQuery(solrQuery, searchParams.getFormattedFq(), 1, 0, "score", "asc");
    List<FacetField> facets = qr.getFacetFields();

    if (facets != null) {
        for (FacetField facet : facets) {
            List<FacetField.Count> facetEntries = facet.getValues();
            if (facet.getName().contains(pointType.getLabel()) && (facetEntries != null)
                    && (facetEntries.size() > 0)) {

                for (FacetField.Count fcount : facetEntries) {
                    if (StringUtils.isNotEmpty(fcount.getName()) && fcount.getCount() > 0) {
                        OccurrencePoint point = new OccurrencePoint(pointType);
                        point.setCount(fcount.getCount());
                        String[] pointsDelimited = StringUtils.split(fcount.getName(), ',');
                        List<Float> coords = new ArrayList<Float>();

                        for (String coord : pointsDelimited) {
                            try {
                                Float decimalCoord = Float.parseFloat(coord);
                                coords.add(decimalCoord);
                            } catch (NumberFormatException numberFormatException) {
                                logger.warn("Error parsing Float for Lat/Long: "
                                        + numberFormatException.getMessage(), numberFormatException);
                            }
                        }

                        if (!coords.isEmpty()) {
                            Collections.reverse(coords); // must be long, lat order
                            point.setCoordinates(coords);
                            points.add(point);
                        }
                    }
                }
            }
        }
    }
    return points;
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * http://ala-biocache1.vm.csiro.au:8080/solr/select?q=*:*&rows=0&facet=true&facet.field=data_provider_id&facet.field=data_provider&facet.sort=data_provider_id
 *
 * @see au.org.ala.biocache.dao.SearchDAO#getDataProviderCounts()
 *///from   w  ww  .  j av  a 2s .  co  m
//IS THIS BEING USED BY ANYTHING??
@Override
public List<DataProviderCountDTO> getDataProviderCounts() throws Exception {

    List<DataProviderCountDTO> dpDTOs = new ArrayList<DataProviderCountDTO>(); // new OccurrencePoint(PointType.POINT);
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType("standard");
    solrQuery.setQuery("*:*");
    solrQuery.setRows(0);
    solrQuery.setFacet(true);
    solrQuery.addFacetField("data_provider_uid");
    solrQuery.addFacetField("data_provider");
    solrQuery.setFacetMinCount(1);
    QueryResponse qr = runSolrQuery(solrQuery, null, 1, 0, "data_provider", "asc");
    List<FacetField> facets = qr.getFacetFields();

    if (facets != null && facets.size() == 2) {

        FacetField dataProviderIdFacet = facets.get(0);
        FacetField dataProviderNameFacet = facets.get(1);

        List<FacetField.Count> dpIdEntries = dataProviderIdFacet.getValues();
        List<FacetField.Count> dpNameEntries = dataProviderNameFacet.getValues();

        if (dpIdEntries != null) {
            for (int i = 0; i < dpIdEntries.size(); i++) {

                FacetField.Count dpIdEntry = dpIdEntries.get(i);
                FacetField.Count dpNameEntry = dpNameEntries.get(i);

                String dataProviderId = dpIdEntry.getName();
                String dataProviderName = dpNameEntry.getName();
                long count = dpIdEntry.getCount();

                if (count > 0) {
                    DataProviderCountDTO dto = new DataProviderCountDTO(dataProviderId, dataProviderName,
                            count);
                    dpDTOs.add(dto);
                }
            }
        }
    }
    if (logger.isInfoEnabled()) {
        logger.info("Find data providers = " + dpDTOs.size());
    }
    return dpDTOs;
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * Get a distinct list of species and their counts using a facet search
 *
 * @param queryString/*from   w ww.  j av  a 2 s.c  om*/
 * @param pageSize
 * @param sortField
 * @param sortDirection
 * @return
 * @throws SolrServerException
 */
protected List<TaxaCountDTO> getSpeciesCounts(String queryString, List<String> filterQueries,
        List<String> facetFields, Integer pageSize, Integer startIndex, String sortField, String sortDirection)
        throws SolrServerException {

    List<TaxaCountDTO> speciesCounts = new ArrayList<TaxaCountDTO>();
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType("standard");
    solrQuery.setQuery(queryString);

    if (filterQueries != null && filterQueries.size() > 0) {
        //solrQuery.addFilterQuery("(" + StringUtils.join(filterQueries, " OR ") + ")");
        for (String fq : filterQueries) {
            solrQuery.addFilterQuery(fq);
        }
    }
    solrQuery.setRows(0);
    solrQuery.setFacet(true);
    solrQuery.setFacetSort(sortField);
    for (String facet : facetFields) {
        solrQuery.addFacetField(facet);
        if (logger.isDebugEnabled()) {
            logger.debug("adding facetField: " + facet);
        }
    }
    //set the facet starting point based on the paging information
    solrQuery.setFacetMinCount(1);
    solrQuery.setFacetLimit(pageSize); // unlimited = -1 | pageSize
    solrQuery.add("facet.offset", Integer.toString(startIndex));
    if (logger.isDebugEnabled()) {
        logger.debug("getSpeciesCount query :" + solrQuery.getQuery());
    }
    QueryResponse qr = runSolrQuery(solrQuery, null, 1, 0, "score", sortDirection);
    if (logger.isInfoEnabled()) {
        logger.info("SOLR query: " + solrQuery.getQuery() + "; total hits: " + qr.getResults().getNumFound());
    }
    List<FacetField> facets = qr.getFacetFields();
    java.util.regex.Pattern p = java.util.regex.Pattern.compile("\\|");

    if (facets != null && facets.size() > 0) {
        if (logger.isDebugEnabled()) {
            logger.debug("Facets: " + facets.size() + "; facet #1: " + facets.get(0).getName());
        }
        for (FacetField facet : facets) {
            List<FacetField.Count> facetEntries = facet.getValues();
            if ((facetEntries != null) && (facetEntries.size() > 0)) {

                for (FacetField.Count fcount : facetEntries) {
                    TaxaCountDTO tcDTO = null;
                    String name = fcount.getName() != null ? fcount.getName() : "";
                    if (fcount.getFacetField().getName().equals(NAMES_AND_LSID)) {
                        String[] values = p.split(name, 5);

                        if (values.length >= 5) {
                            if (!"||||".equals(name)) {
                                tcDTO = new TaxaCountDTO(values[0], fcount.getCount());
                                tcDTO.setGuid(StringUtils.trimToNull(values[1]));
                                tcDTO.setCommonName(values[2]);
                                tcDTO.setKingdom(values[3]);
                                tcDTO.setFamily(values[4]);
                                if (StringUtils.isNotEmpty(tcDTO.getGuid()))
                                    tcDTO.setRank(searchUtils.getTaxonSearch(tcDTO.getGuid())[1].split(":")[0]);
                            }
                        } else {
                            if (logger.isDebugEnabled()) {
                                logger.debug("The values length: " + values.length + " :" + name);
                            }
                            tcDTO = new TaxaCountDTO(name, fcount.getCount());
                        }
                        //speciesCounts.add(i, tcDTO);
                        if (tcDTO != null && tcDTO.getCount() > 0)
                            speciesCounts.add(tcDTO);
                    } else if (fcount.getFacetField().getName().equals(COMMON_NAME_AND_LSID)) {
                        String[] values = p.split(name, 6);

                        if (values.length >= 5) {
                            if (!"|||||".equals(name)) {
                                tcDTO = new TaxaCountDTO(values[1], fcount.getCount());
                                tcDTO.setGuid(StringUtils.trimToNull(values[2]));
                                tcDTO.setCommonName(values[0]);
                                //cater for the bug of extra vernacular name in the result
                                tcDTO.setKingdom(values[values.length - 2]);
                                tcDTO.setFamily(values[values.length - 1]);
                                if (StringUtils.isNotEmpty(tcDTO.getGuid()))
                                    tcDTO.setRank(searchUtils.getTaxonSearch(tcDTO.getGuid())[1].split(":")[0]);
                            }
                        } else {
                            if (logger.isDebugEnabled()) {
                                logger.debug("The values length: " + values.length + " :" + name);
                            }
                            tcDTO = new TaxaCountDTO(name, fcount.getCount());
                        }
                        //speciesCounts.add(i, tcDTO);
                        if (tcDTO != null && tcDTO.getCount() > 0) {
                            speciesCounts.add(tcDTO);
                        }
                    }
                }
            }
        }
    }

    return speciesCounts;
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

/**
 * Obtains a list and facet count of the source uids for the supplied query.
 *
 * @param searchParams//from   w  w w . j a  v  a2 s  .  co  m
 * @return
 * @throws Exception
 */
public Map<String, Integer> getSourcesForQuery(SpatialSearchRequestParams searchParams) throws Exception {

    Map<String, Integer> uidStats = new HashMap<String, Integer>();
    SolrQuery solrQuery = new SolrQuery();
    queryFormatUtils.formatSearchQuery(searchParams);
    if (logger.isInfoEnabled()) {
        logger.info("The query : " + searchParams.getFormattedQuery());
    }
    solrQuery.setQuery(searchParams.getFormattedQuery());
    solrQuery.setQueryType("standard");
    solrQuery.setRows(0);
    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(1);
    solrQuery.addFacetField("data_provider_uid");
    solrQuery.addFacetField("data_resource_uid");
    solrQuery.addFacetField("collection_uid");
    solrQuery.addFacetField("institution_uid");
    QueryResponse qr = runSolrQuery(solrQuery, searchParams.getFormattedFq(), 1, 0, "score", "asc");
    //now cycle through and get all the facets
    List<FacetField> facets = qr.getFacetFields();
    for (FacetField facet : facets) {
        if (facet.getValues() != null) {
            for (FacetField.Count ffc : facet.getValues()) {
                if (ffc.getCount() > 0) {
                    uidStats.put(ffc.getName() != null ? ffc.getName() : "", new Integer((int) ffc.getCount()));
                }
            }
        }
    }
    return uidStats;
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

@Cacheable(cacheName = "legendCache")
public List<LegendItem> getLegend(SpatialSearchRequestParams searchParams, String facetField,
        String[] cutpoints) throws Exception {
    List<LegendItem> legend = new ArrayList<LegendItem>();

    queryFormatUtils.formatSearchQuery(searchParams);
    if (logger.isInfoEnabled()) {
        logger.info("search query: " + searchParams.getFormattedQuery());
    }/*from   www  .j  a v  a2 s  .com*/
    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQueryType("standard");
    solrQuery.setQuery(searchParams.getFormattedQuery());
    solrQuery.setRows(0);
    solrQuery.setFacet(true);

    //is facet query?
    if (cutpoints == null) {
        //special case for the decade
        if (DECADE_FACET_NAME.equals(facetField))
            initDecadeBasedFacet(solrQuery, "occurrence_year");
        else
            solrQuery.addFacetField(facetField);
    } else {
        solrQuery.addFacetQuery("-" + facetField + ":[* TO *]");

        for (int i = 0; i < cutpoints.length; i += 2) {
            solrQuery.addFacetQuery(facetField + ":[" + cutpoints[i] + " TO " + cutpoints[i + 1] + "]");
        }
    }

    solrQuery.setFacetMinCount(1);
    solrQuery.setFacetLimit(-1);//MAX_DOWNLOAD_SIZE);  // unlimited = -1

    solrQuery.setFacetMissing(true);

    QueryResponse qr = runSolrQuery(solrQuery, searchParams.getFormattedFq(), 1, 0, "score", "asc");
    List<FacetField> facets = qr.getFacetFields();
    if (facets != null) {
        for (FacetField facet : facets) {
            List<FacetField.Count> facetEntries = facet.getValues();
            if (facet.getName().contains(facetField) && (facetEntries != null) && (facetEntries.size() > 0)) {
                int i = 0;
                for (i = 0; i < facetEntries.size(); i++) {
                    FacetField.Count fcount = facetEntries.get(i);
                    if (fcount.getCount() > 0) {
                        String fq = facetField + ":\"" + fcount.getName() + "\"";
                        if (fcount.getName() == null) {
                            fq = "-" + facetField + ":[* TO *]";
                        }
                        legend.add(new LegendItem(fcount.getName(), fcount.getCount(), fq));
                    }
                }
                break;
            }
        }
    }
    //check if we have query based facets
    Map<String, Integer> facetq = qr.getFacetQuery();
    if (facetq != null && facetq.size() > 0) {
        for (Entry<String, Integer> es : facetq.entrySet()) {
            legend.add(new LegendItem(es.getKey(), es.getValue(), es.getKey()));
        }
    }

    //check to see if we have a date range facet
    List<FacetField> facetDates = qr.getFacetDates();
    if (facetDates != null && !facetDates.isEmpty()) {
        FacetField ff = facetDates.get(0);
        String firstDate = null;
        for (FacetField.Count facetEntry : ff.getValues()) {
            String startDate = facetEntry.getName();
            if (firstDate == null) {
                firstDate = startDate;
            }
            String finishDate;
            if (DECADE_PRE_1850_LABEL.equals(startDate)) {
                startDate = "*";
                finishDate = firstDate;
            } else {
                int startYear = Integer.parseInt(startDate.substring(0, 4));
                finishDate = (startYear - 1) + "-12-31T23:59:59Z";
            }
            legend.add(new LegendItem(facetEntry.getName(), facetEntry.getCount(),
                    "occurrence_year:[" + startDate + " TO " + finishDate + "]"));
        }
    }
    return legend;
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

License:Open Source License

public List<DataProviderCountDTO> getDataProviderList(SpatialSearchRequestParams requestParams)
        throws Exception {
    List<DataProviderCountDTO> dataProviderList = new ArrayList<DataProviderCountDTO>();
    FacetField facet = getFacet(requestParams, "data_provider_uid");
    String[] oldFq = requestParams.getFacets();
    if (facet != null) {
        String[] dp = new String[1];
        List<FacetField.Count> facetEntries = facet.getValues();
        if (facetEntries != null && facetEntries.size() > 0) {
            for (int i = 0; i < facetEntries.size(); i++) {
                FacetField.Count fcount = facetEntries.get(i);

                //get data_provider value
                if (fcount.getName() == null) {
                    dp[0] = "-data_provider:*";
                } else {
                    dp[0] = fcount.getAsFilterQuery();
                }//  w ww  .j a  va2 s.co  m
                requestParams.setFq(dp);
                String dataProviderName = getFacet(requestParams, "data_provider").getValues().get(0).getName();
                dataProviderList
                        .add(new DataProviderCountDTO(fcount.getName(), dataProviderName, fcount.getCount()));
            }
        }
    }
    requestParams.setFacets(oldFq);
    return dataProviderList;
}