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

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

Introduction

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

Prototype

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

Source Link

Document

Replace any existing parameter with the given name.

Usage

From source file:org.dspace.app.webui.cris.components.statistics.ASolrStatsConfigurerComponent.java

protected void _addBasicConfiguration(SolrQuery solrQuery, Integer yearsQuery) {
    solrQuery.setRows(0);/*  ww w. j av a 2  s  .c o  m*/
    solrQuery.setFacet(true);
    solrQuery.set("facet.date", "time");
    solrQuery.set("facet.date.end", "NOW/MONTH+1MONTH");

    solrQuery.set("facet.date.start", "NOW/MONTH-" + yearsQuery + "YEARS");
    solrQuery.set("facet.date.include", "upper");
    solrQuery.set("facet.date.gap", "+1MONTHS");
    // solrQuery.set("facet.mincount", "1");
}

From source file:org.dspace.app.webui.cris.components.statistics.StatCrisDownloadSelectedObjectComponent.java

protected void _prepareBasicQuery(SolrQuery solrQuery, Integer yearsQuery) {
    _addBasicConfiguration(solrQuery, yearsQuery);
    solrQuery.addFacetField(_CONTINENT, _COUNTRY_CODE, _CITY, ID, _LOCATION, _FISCALYEAR, _SOLARYEAR);
    solrQuery.set("facet.missing", true);
    solrQuery.set("f." + _LOCATION + ".facet.missing", false);
    solrQuery.set("f." + ID + ".facet.missing", false);
    solrQuery.set("f." + _FISCALYEAR + ".facet.missing", false);
    solrQuery.set("f." + _SOLARYEAR + ".facet.missing", false);
    solrQuery.set("f." + _FISCALYEAR + ".facet.sort", false);
    solrQuery.set("f." + _SOLARYEAR + ".facet.sort", false);

    solrQuery.set("f." + FILE + ".facet.missing", false);
    solrQuery.set("f." + FILE + ".facet.mincount", 1);
    solrQuery.set("f." + ID + ".facet.mincount", 1);
    solrQuery.set("f." + _CONTINENT + ".facet.mincount", 1);
    solrQuery.set("f." + _COUNTRY_CODE + ".facet.mincount", 1);
    solrQuery.set("f." + _CITY + ".facet.mincount", 1);
    solrQuery.set("f." + _LOCATION + ".facet.mincount", 1);
    solrQuery.set("f." + _FISCALYEAR + ".facet.mincount", 1);
    solrQuery.set("f." + _SOLARYEAR + ".facet.mincount", 1);
    solrQuery.set("f." + _CONTINENT + ".facet.mincount", 1);
}

From source file:org.dspace.app.webui.cris.components.statistics.StatCrisViewSelectedObjectComponent.java

protected void _prepareBasicQuery(SolrQuery solrQuery, Integer yearsQuery) {
    _addBasicConfiguration(solrQuery, yearsQuery);
    solrQuery.addFacetField(_CONTINENT, _COUNTRY_CODE, _CITY, ID, _LOCATION, _FISCALYEAR, _SOLARYEAR);
    solrQuery.set("facet.missing", true);
    solrQuery.set("f." + _LOCATION + ".facet.missing", false);
    solrQuery.set("f." + ID + ".facet.missing", false);
    solrQuery.set("f." + _FISCALYEAR + ".facet.missing", false);
    solrQuery.set("f." + _SOLARYEAR + ".facet.missing", false);
    solrQuery.set("f." + _FISCALYEAR + ".facet.sort", false);
    solrQuery.set("f." + _SOLARYEAR + ".facet.sort", false);

    solrQuery.set("f." + _CONTINENT + ".facet.mincount", 1);
    solrQuery.set("f." + _COUNTRY_CODE + ".facet.mincount", 1);
    solrQuery.set("f." + _CITY + ".facet.mincount", 1);
    solrQuery.set("f." + _LOCATION + ".facet.mincount", 1);
    solrQuery.set("f." + _FISCALYEAR + ".facet.mincount", 1);
    solrQuery.set("f." + _SOLARYEAR + ".facet.mincount", 1);
}

From source file:org.dspace.authority.SolrAuthority.java

License:BSD License

public Choices getMatches(String field, String text, int collection, int start, int limit, String locale,
        boolean bestMatch) {
    if (limit == 0)
        limit = 10;//ww  w . j a  va  2s . c  om

    SolrQuery queryArgs = new SolrQuery();
    if (text == null || text.trim().equals("")) {
        queryArgs.setQuery("*:*");
    } else {
        String searchField = "value";
        String localSearchField = "";
        try {
            //A downside of the authors is that the locale is sometimes a number, make sure that this isn't one
            Integer.parseInt(locale);
            locale = null;
        } catch (NumberFormatException e) {
            //Everything is allright
        }
        if (locale != null && !"".equals(locale)) {
            localSearchField = searchField + "_" + locale;
        }

        String query = "(" + toQuery(searchField, text) + ") ";
        if (!localSearchField.equals("")) {
            query += " or (" + toQuery(localSearchField, text) + ")";
        }
        queryArgs.setQuery(query);
    }

    queryArgs.addFilterQuery("field:" + field);
    queryArgs.set(CommonParams.START, start);
    //We add one to our facet limit so that we know if there are more matches
    int maxNumberOfSolrResults = limit + 1;
    if (externalResults) {
        maxNumberOfSolrResults = ConfigurationManager.getIntProperty("xmlui.lookup.select.size", 12);
    }
    queryArgs.set(CommonParams.ROWS, maxNumberOfSolrResults);

    String sortField = "value";
    String localSortField = "";
    if (StringUtils.isNotBlank(locale)) {
        localSortField = sortField + "_" + locale;
        queryArgs.setSortField(localSortField, SolrQuery.ORDER.asc);
    } else {
        queryArgs.setSortField(sortField, SolrQuery.ORDER.asc);
    }

    Choices result;
    try {
        int max = 0;
        boolean hasMore = false;
        QueryResponse searchResponse = getSearchService().search(queryArgs);
        SolrDocumentList authDocs = searchResponse.getResults();
        ArrayList<Choice> choices = new ArrayList<Choice>();
        if (authDocs != null) {
            max = (int) searchResponse.getResults().getNumFound();
            int maxDocs = authDocs.size();
            if (limit < maxDocs)
                maxDocs = limit;
            List<AuthorityValue> alreadyPresent = new ArrayList<AuthorityValue>();
            for (int i = 0; i < maxDocs; i++) {
                SolrDocument solrDocument = authDocs.get(i);
                if (solrDocument != null) {
                    AuthorityValue val = AuthorityValue.fromSolr(solrDocument);

                    Map<String, String> extras = val.choiceSelectMap();
                    extras.put("insolr", val.getId());
                    choices.add(new Choice(val.getId(), val.getValue(), val.getValue(), extras));
                    alreadyPresent.add(val);
                }
            }

            if (externalResults && StringUtils.isNotBlank(text)) {
                int sizeFromSolr = alreadyPresent.size();
                int maxExternalResults = limit <= 10 ? Math.max(limit - sizeFromSolr, 2)
                        : Math.max(limit - 10 - sizeFromSolr, 2) + limit - 10;
                addExternalResults(text, choices, alreadyPresent, maxExternalResults);
            }

            // hasMore = (authDocs.size() == (limit + 1));
            hasMore = true;
        }

        int confidence;
        if (choices.size() == 0)
            confidence = Choices.CF_NOTFOUND;
        else if (choices.size() == 1)
            confidence = Choices.CF_UNCERTAIN;
        else
            confidence = Choices.CF_AMBIGUOUS;

        result = new Choices(choices.toArray(new Choice[choices.size()]), start,
                hasMore ? max : choices.size() + start, confidence, hasMore);
    } catch (Exception e) {
        log.error("Error while retrieving authority values {field: " + field + ", prefix:" + text + "}", e);
        result = new Choices(true);
    }

    return result; //To change body of implemented methods use File | Settings | File Templates.
}

From source file:org.dspace.discovery.SolrServiceImpl.java

License:BSD License

@Override
public void buildSpellCheck() throws SearchServiceException {
    try {/*from   w ww. jav  a2 s  .  co  m*/
        if (getSolr() == null) {
            return;
        }
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("spellcheck", true);
        solrQuery.set(SpellingParams.SPELLCHECK_BUILD, true);
        getSolr().query(solrQuery);
    } catch (SolrServerException e) {
        //Make sure to also log the exception since this command is usually run from a crontab.
        log.error(e, e);
        throw new SearchServiceException(e);
    }
}

From source file:org.dspace.util.SolrImportExport.java

License:BSD License

/**
 * Exports documents from the given index to the specified target directory in batches of #ROWS_PER_FILE, starting at fromWhen (or all documents).
 * See #makeExportFilename for the file names that are generated.
 *
 * @param indexName The index to export.
 * @param toDir The target directory for the export. Will be created if it doesn't exist yet. The directory must be writeable.
 * @param solrUrl The solr URL for the index to export. Must not be null.
 * @param timeField The time field to use for sorting the export. Must not be null.
 * @param fromWhen Optionally, from when to export. See options for allowed values. If null or empty, all documents will be exported.
 * @throws SolrServerException if there is a problem with exporting the index.
 * @throws IOException if there is a problem creating the files or communicating with Solr.
 * @throws SolrImportExportException if there is a problem in communicating with Solr.
 *//*from w w w  .j av  a 2 s . c  o  m*/
public static void exportIndex(String indexName, File toDir, String solrUrl, String timeField, String fromWhen)
        throws SolrServerException, IOException, SolrImportExportException {
    if (StringUtils.isBlank(solrUrl)) {
        throw new SolrImportExportException(
                "Could not construct solr URL for index" + indexName + ", aborting export.");
    }

    if (!toDir.exists() || !toDir.canWrite()) {
        throw new SolrImportExportException("Target directory " + toDir
                + " doesn't exist or is not writable, aborting export of index " + indexName);
    }

    HttpSolrServer solr = new HttpSolrServer(solrUrl);

    SolrQuery query = new SolrQuery("*:*");
    if (StringUtils.isNotBlank(fromWhen)) {
        String lastValueFilter = makeFilterQuery(timeField, fromWhen);
        if (StringUtils.isNotBlank(lastValueFilter)) {
            query.addFilterQuery(lastValueFilter);
        }
    }

    query.setRows(0);
    query.setGetFieldStatistics(timeField);
    Map<String, FieldStatsInfo> fieldInfo = solr.query(query).getFieldStatsInfo();
    if (fieldInfo == null || !fieldInfo.containsKey(timeField)) {
        log.warn("Cannot get earliest date, not exporting index " + indexName + ", time field " + timeField
                + ", from " + fromWhen);
        return;
    }
    FieldStatsInfo timeFieldInfo = fieldInfo.get(timeField);
    if (timeFieldInfo == null || timeFieldInfo.getMin() == null) {
        log.warn("Cannot get earliest date, not exporting index " + indexName + ", time field " + timeField
                + ", from " + fromWhen);
        return;
    }
    Date earliestTimestamp = (Date) timeFieldInfo.getMin();

    query.setGetFieldStatistics(false);
    query.clearSorts();
    query.setRows(0);
    query.setFacet(true);
    query.add(FacetParams.FACET_RANGE, timeField);
    query.add(FacetParams.FACET_RANGE_START, SOLR_DATE_FORMAT.format(earliestTimestamp) + "/MONTH");
    query.add(FacetParams.FACET_RANGE_END, "NOW/MONTH+1MONTH");
    query.add(FacetParams.FACET_RANGE_GAP, "+1MONTH");
    query.setFacetMinCount(1);

    List<RangeFacet.Count> monthFacets = solr.query(query).getFacetRanges().get(0).getCounts();

    for (RangeFacet.Count monthFacet : monthFacets) {
        Date monthStartDate;
        String monthStart = monthFacet.getValue();
        try {
            monthStartDate = SOLR_DATE_FORMAT_NO_MS.parse(monthStart);
        } catch (java.text.ParseException e) {
            throw new SolrImportExportException("Could not read start of month batch as date: " + monthStart,
                    e);
        }
        int docsThisMonth = monthFacet.getCount();

        SolrQuery monthQuery = new SolrQuery("*:*");
        monthQuery.setRows(ROWS_PER_FILE);
        monthQuery.set("wt", "csv");
        monthQuery.set("fl", "*");

        monthQuery.addFilterQuery(timeField + ":[" + monthStart + " TO " + monthStart + "+1MONTH]");

        for (int i = 0; i < docsThisMonth; i += ROWS_PER_FILE) {
            monthQuery.setStart(i);
            URL url = new URL(solrUrl + "/select?" + monthQuery.toString());

            File file = new File(toDir.getCanonicalPath(),
                    makeExportFilename(indexName, monthStartDate, docsThisMonth, i));
            if (file.createNewFile()) {
                FileUtils.copyURLToFile(url, file);
                log.info("Exported batch " + i + " to " + file.getCanonicalPath());
            } else {
                throw new SolrImportExportException("Could not create file " + file.getCanonicalPath()
                        + " while exporting index " + indexName + ", month" + monthStart + ", batch " + i);
            }
        }
    }
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

@Override
protected Iterable<? extends DocumentDistance> geoQuery(URI geoProperty, Point p, final URI units,
        double distance, String distanceVar, Var contextVar) throws MalformedQueryException, IOException {
    double kms = GeoUnits.toKilometres(distance, units);

    String qstr = "{!geofilt score=recipDistance}";
    if (contextVar != null) {
        Resource ctx = (Resource) contextVar.getValue();
        String tq = termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx));
        if (ctx != null) {
            qstr = tq + " AND " + qstr;
        } else {/*from w w w.  j  av a  2s.  c o m*/
            qstr = "-" + tq + " AND " + qstr;
        }
    }
    SolrQuery q = new SolrQuery(qstr);
    q.set(SpatialParams.FIELD, SearchFields.getPropertyField(geoProperty));
    q.set(SpatialParams.POINT, p.getY() + "," + p.getX());
    q.set(SpatialParams.DISTANCE, Double.toString(kms));
    q.addField(SearchFields.URI_FIELD_NAME);
    // ':' is part of the fl parameter syntax so we can't use the full
    // property field name
    // instead we use wildcard + local part of the property URI
    q.addField("*" + geoProperty.getLocalName());
    // always include the distance - needed for sanity checking
    q.addField(DISTANCE_FIELD + ":geodist()");
    boolean requireContext = (contextVar != null && !contextVar.hasValue());
    if (requireContext) {
        q.addField(SearchFields.CONTEXT_FIELD_NAME);
    }

    QueryResponse response;
    try {
        response = search(q);
    } catch (SolrServerException e) {
        throw new IOException(e);
    }

    SolrDocumentList results = response.getResults();
    return Iterables.transform(results, new Function<SolrDocument, DocumentDistance>() {

        @Override
        public DocumentDistance apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            return new SolrDocumentDistance(doc, units);
        }
    });
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

@Override
protected Iterable<? extends DocumentResult> geoRelationQuery(String relation, URI geoProperty, Shape shape,
        Var contextVar) throws MalformedQueryException, IOException {
    String spatialOp = toSpatialOp(relation);
    if (spatialOp == null) {
        return null;
    }// ww w  . ja  va2  s .  co  m
    String wkt = toWkt(shape);
    String qstr = "\"" + spatialOp + "(" + wkt + ")\"";
    if (contextVar != null) {
        Resource ctx = (Resource) contextVar.getValue();
        String tq = termQuery(SearchFields.CONTEXT_FIELD_NAME, SearchFields.getContextID(ctx));
        if (ctx != null) {
            qstr = tq + " AND " + qstr;
        } else {
            qstr = "-" + tq + " AND " + qstr;
        }
    }
    SolrQuery q = new SolrQuery(qstr);
    q.set(CommonParams.DF, SearchFields.getPropertyField(geoProperty));
    q.addField(SearchFields.URI_FIELD_NAME);
    // ':' is part of the fl parameter syntax so we can't use the full
    // property field name
    // instead we use wildcard + local part of the property URI
    q.addField("*" + geoProperty.getLocalName());
    boolean requireContext = (contextVar != null && !contextVar.hasValue());
    if (requireContext) {
        q.addField(SearchFields.CONTEXT_FIELD_NAME);
    }

    QueryResponse response;
    try {
        response = search(q);
    } catch (SolrServerException e) {
        throw new IOException(e);
    }

    SolrDocumentList results = response.getResults();
    return Iterables.transform(results, new Function<SolrDocument, DocumentResult>() {

        @Override
        public DocumentResult apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            return new SolrDocumentResult(doc);
        }
    });
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

private SolrQuery prepareQuery(URI propertyURI, SolrQuery query) {
    // check out which query parser to use, based on the given property URI
    if (propertyURI == null)
        // if we have no property given, we create a default query parser which
        // has the TEXT_FIELD_NAME as the default field
        query.set(CommonParams.DF, SearchFields.TEXT_FIELD_NAME);
    else/*from  www .j  a va 2s.c om*/
        // otherwise we create a query parser that has the given property as
        // the default field
        query.set(CommonParams.DF, SearchFields.getPropertyField(propertyURI));
    return query;
}

From source file:org.emonocot.persistence.dao.hibernate.SearchableDaoImpl.java

License:Open Source License

/**
 * @param query//from   ww  w.j  av a  2s  .co m
 *            A lucene query
 * @param spatialQuery
 *            A spatial query to filter the results by
 * @param pageSize
 *            The maximum number of results to return
 * @param pageNumber
 *            The offset (in pageSize chunks, 0-based) from the beginning of
 *            the recordset
 * @param facets
 *            The names of the facets you want to calculate
 * @param selectedFacets
 *            A map of facets which you would like to restrict the search by
 * @param sort
 *            A representation for the order results should be returned in
 * @param fetch
 *            Set the fetch profile
 * @return a Page from the resultset
 * @throws SolrServerException
 */
public final Page<T> search(final String query, final String spatialQuery, final Integer pageSize,
        final Integer pageNumber, final String[] facets, Map<String, String> facetPrefixes,
        final Map<String, String> selectedFacets, final String sort, final String fetch)
        throws SolrServerException {
    SolrQuery solrQuery = prepareQuery(query, sort, pageSize, pageNumber, selectedFacets);
    solrQuery.set("spellcheck", "true");
    solrQuery.set("spellcheck.collate", "true");
    solrQuery.set("spellcheck.count", "1");
    solrQuery.set("bq", "base.class_s:org.emonocot.model.Taxon^2.0");

    // Filter the searchable objects out
    solrQuery.addFilterQuery("base.class_searchable_b:" + isSearchableObject());

    if (spatialQuery != null && spatialQuery.trim().length() != 0) {
        solrQuery.addFilterQuery(spatialQuery);
    }

    if (facets != null && facets.length != 0) {
        solrQuery.setFacet(true);
        solrQuery.setFacetMinCount(1);
        solrQuery.setFacetSort(FacetParams.FACET_SORT_INDEX);

        for (String facet : facets) {
            if (facet.equals("base.class_s")) {
                solrQuery.setParam("f.base.class_s.facet.sort", FacetParams.FACET_SORT_COUNT);
            }
            if (facet.endsWith("_dt")) {
                /**
                 * Is a date facet. Once Solr 4.2 is released, we can implement variable length buckets, but for now
                 * stick with fixed buckets https://issues.apache.org/jira/browse/SOLR-2366
                 */

                solrQuery.add("facet.range", facet);
                solrQuery.add("f." + facet + ".facet.range.start", "NOW/DAY-1YEARS");
                solrQuery.add("f." + facet + ".facet.range.end", "NOW/DAY");
                solrQuery.add("f." + facet + ".facet.range.gap", "+1MONTH");
            } else {
                solrQuery.addFacetField(facet);
            }
            includeMissing(solrQuery, facet);
        }
        if (facetPrefixes != null) {
            for (String facet : facetPrefixes.keySet()) {
                solrQuery.add("f." + facet + ".facet.prefix", facetPrefixes.get(facet));
            }
        }
    }
    QueryResponse queryResponse = solrServer.query(solrQuery);

    List<T> results = new ArrayList<T>();
    for (SolrDocument solrDocument : queryResponse.getResults()) {
        T object = loadObjectForDocument(solrDocument);
        enableProfilePostQuery(object, fetch);
        results.add(object);
    }

    Long totalResults = new Long(queryResponse.getResults().getNumFound());
    Page<T> page = new DefaultPageImpl<T>(totalResults.intValue(), pageNumber, pageSize, results,
            queryResponse);
    if (selectedFacets != null) {
        page.setSelectedFacets(selectedFacets);
    }
    page.setSort(sort);

    return page;
}