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

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

Introduction

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

Prototype

public SolrQuery setFields(String... fields) 

Source Link

Usage

From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java

License:Open Source License

public static SolrQuery getSolrQuery(final String querystring, final String sort, final int offset,
        final int count, final String... fields) {
    // construct query
    final SolrQuery params = new SolrQuery();
    //if (count < 2 && querystring.startsWith("{!raw f=")) {
    //    params.setQuery("*:*");
    //    params.addFilterQuery(querystring);
    //} else {//from   w  ww.  jav a 2s .c o  m
    params.setQuery(querystring);
    //}
    params.clearSorts();
    if (sort != null) {
        params.set(CommonParams.SORT, sort);
    }
    params.setRows(count);
    params.setStart(offset);
    params.setFacet(false);
    if (fields != null && fields.length > 0)
        params.setFields(fields);
    params.setIncludeScore(false);
    params.setParam("defType", "edismax");
    params.setParam(DisMaxParams.QF, CollectionSchema.text_t.getSolrFieldName() + "^1.0");
    return params;
}

From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java

License:Open Source License

/**
 * get the number of results when this query is done.
 * This should only be called if the actual result is never used, and only the count is interesting
 * @param querystring//from w  w  w  .  j  av  a  2 s . c om
 * @return the number of results for this query
 */
@Override
public long getCountByQuery(String querystring) throws IOException {
    // construct query
    final SolrQuery params = new SolrQuery();
    params.setQuery(querystring);
    params.setRows(0); // essential to just get count
    params.setStart(0);
    params.setFacet(false);
    params.clearSorts();
    params.setFields(CollectionSchema.id.getSolrFieldName());
    params.setIncludeScore(false);

    // query the server
    final SolrDocumentList sdl = getDocumentListByParams(params);
    return sdl == null ? 0 : sdl.getNumFound();
}

From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java

License:Open Source License

/**
 * get facets of the index: a list of lists with values that are most common in a specific field
 * @param query a query which is performed to get the facets
 * @param fields the field names which are selected as facet
 * @param maxresults the maximum size of the resulting maps
 * @return a map with key = facet field name, value = an ordered map of field values for that field
 * @throws IOException// w ww .  ja  va  2 s. com
 */
@Override
public LinkedHashMap<String, ReversibleScoreMap<String>> getFacets(String query, int maxresults,
        final String... fields) throws IOException {
    // construct query
    assert fields.length > 0;
    final SolrQuery params = new SolrQuery();
    params.setQuery(query);
    params.setRows(0);
    params.setStart(0);
    params.setFacet(true);
    params.setFacetMinCount(1); // there are many 0-count facets in the uninverted index cache
    params.setFacetLimit(maxresults);
    params.setFacetSort(FacetParams.FACET_SORT_COUNT);
    params.setParam(FacetParams.FACET_METHOD, FacetParams.FACET_METHOD_fc /*FACET_METHOD_fcs*/);
    params.setFields(fields);
    params.clearSorts();
    params.setIncludeScore(false);
    for (String field : fields)
        params.addFacetField(field);

    // query the server
    QueryResponse rsp = getResponseByParams(params);
    LinkedHashMap<String, ReversibleScoreMap<String>> facets = new LinkedHashMap<String, ReversibleScoreMap<String>>(
            fields.length);
    for (String field : fields) {
        FacetField facet = rsp.getFacetField(field);
        ReversibleScoreMap<String> result = new ClusteredScoreMap<String>(UTF8.insensitiveUTF8Comparator);
        List<Count> values = facet.getValues();
        if (values == null)
            continue;
        for (Count ff : values)
            if (ff.getCount() > 0)
                result.set(ff.getName(), (int) ff.getCount());
        facets.put(field, result);
    }
    return facets;
}

From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java

License:Open Source License

@Override
public SolrDocument getDocumentById(final String id, final String... fields) throws IOException {
    assert id.length() == Word.commonHashLength : "wrong id: " + id;
    final SolrQuery query = new SolrQuery();
    // construct query
    StringBuilder sb = new StringBuilder(23);
    sb.append("{!cache=false raw f=").append(CollectionSchema.id.getSolrFieldName()).append('}').append(id);
    query.setQuery(sb.toString());//from ww  w  .j ava2  s . c  o m
    //query.setQuery("*:*");
    //query.addFilterQuery(sb.toString());
    query.clearSorts();
    query.setRows(1);
    query.setStart(0);
    if (fields != null && fields.length > 0)
        query.setFields(fields);
    query.setIncludeScore(false);

    // query the server
    try {
        final SolrDocumentList docs = getDocumentListByParams(query);
        if (docs == null || docs.isEmpty())
            return null;
        SolrDocument doc = docs.get(0);
        return doc;
    } catch (final Throwable e) {
        clearCaches(); // we clear the in case that this is caused by OOM
        throw new IOException(e.getMessage(), e);
    }
}

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. ja v a  2 s . co 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:net.yacy.search.index.ErrorCache.java

License:Open Source License

public ErrorCache(final Fulltext fulltext) {
    this.fulltext = fulltext;
    this.cache = new LinkedHashMap<String, CollectionConfiguration.FailDoc>();
    // concurrently fill stack with latest values
    new Thread() {
        @Override//from   www.j a  v a2s. c o m
        public void run() {
            final SolrQuery params = new SolrQuery();
            params.setParam("defType", "edismax");
            params.setStart(0);
            params.setRows(1000);
            params.setFacet(false);
            params.setSort(
                    new SortClause(CollectionSchema.last_modified.getSolrFieldName(), SolrQuery.ORDER.desc));
            params.setFields(CollectionSchema.id.getSolrFieldName());
            params.setQuery(
                    CollectionSchema.failreason_s.getSolrFieldName() + AbstractSolrConnector.CATCHALL_DTERM);
            params.set(CommonParams.DF, CollectionSchema.id.getSolrFieldName()); // DisMaxParams.QF or CommonParams.DF must be given
            SolrDocumentList docList;
            try {
                docList = fulltext.getDefaultConnector().getDocumentListByParams(params);
                if (docList != null)
                    for (int i = docList.size() - 1; i >= 0; i--) {
                        SolrDocument doc = docList.get(i);
                        String hash = (String) doc.getFieldValue(CollectionSchema.id.getSolrFieldName());
                        cache.put(hash, null);
                    }
            } catch (IOException e) {
                ConcurrentLog.logException(e);
            }
        }
    }.start();
}

From source file:net.yacy.search.index.ErrorCacheFiller.java

License:Open Source License

/**
 * Fills the error cache with recently failed document hashes found in the index
 *//* w  w  w.  j a  v a 2 s .  c o  m*/
@Override
public void run() {
    final SolrQuery params = new SolrQuery();
    params.setParam("defType", "edismax");
    params.setStart(0);
    params.setRows(1000);
    params.setFacet(false);
    params.setSort(new SortClause(CollectionSchema.load_date_dt.getSolrFieldName(), SolrQuery.ORDER.desc)); // load_date_dt = faildate
    params.setFields(CollectionSchema.id.getSolrFieldName());
    params.setQuery(CollectionSchema.failreason_s.getSolrFieldName() + AbstractSolrConnector.CATCHALL_DTERM);
    params.set(CommonParams.DF, CollectionSchema.id.getSolrFieldName()); // DisMaxParams.QF or CommonParams.DF must be given
    SolrDocumentList docList;
    try {
        docList = this.sb.index.fulltext().getDefaultConnector().getDocumentListByParams(params);
        if (docList != null)
            for (int i = docList.size() - 1; i >= 0; i--) {
                SolrDocument doc = docList.get(i);
                String hash = (String) doc.getFieldValue(CollectionSchema.id.getSolrFieldName());
                cache.putHashOnly(hash);
            }
    } catch (IOException e) {
        ConcurrentLog.logException(e);
    }
}

From source file:no.sesat.search.mode.command.SolrSearchCommand.java

License:Open Source License

/** Override this to set additional parameters in the SolrQuery.
 * Crucial for any override to call super.modifyQuery(query)
 **//*from   ww w. j  ava2  s  . co  m*/
protected void modifyQuery(final SolrQuery query) {

    // @XXX does this ruin solr caching
    query.set("uniqueId", context.getDataModel().getParameters().getUniqueId());

    // add any filtering query
    if (0 < getSearchConfiguration().getFilteringQuery().length()) {
        query.setFilterQueries(getSearchConfiguration().getFilteringQuery());
    }

    // also add any filter
    if (0 < getFilter().length()) {
        query.addFilterQuery(getFilter());
    }

    // custom query type
    if (null != getSearchConfiguration().getQueryType()
            && 0 < getSearchConfiguration().getQueryType().length()) {
        query.setQueryType(getSearchConfiguration().getQueryType());
    }

    // The request handler may be configured in the index which fields to return in the results
    if (0 < getSearchConfiguration().getResultFieldMap().size()) {
        query.setFields(getSearchConfiguration().getResultFieldMap().keySet().toArray(new String[] {}));
    }

    createFacets(query);

    // when the root logger is set to DEBUG do not limit connection times
    if (Logger.getRootLogger().getLevel().isGreaterOrEqual(Level.INFO)) {
        query.setTimeAllowed(getSearchConfiguration().getTimeout());
    }

    // sorting
    if (isUserSortable()) {
        final String sort = getUserSortBy();
        if (null != sort) {
            final String[] sortSplit = sort.split(" ");
            query.addSortField(sortSplit[0], SolrQuery.ORDER.valueOf(sortSplit[1]));
        }
    }
    final Map<String, String> sortMap = getSearchConfiguration().getSortMap();
    for (Map.Entry<String, String> entry : sortMap.entrySet()) {
        final SolrQuery.ORDER order = SolrQuery.ORDER.valueOf(entry.getValue());
        query.addSortField(entry.getKey(), order);
    }
}

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

License:Open Source License

/**
 * Re-usable method for performing a SOLR search that returns the RAW query response
 * @param queryString//from   www. j  av a 2 s  .c om
 * @param filterQuery
 * @param pageSize
 * @param startIndex
 * @param sortField
 * @param sortDirection
 * @return
 * @throws Exception
 */
private QueryResponse doSolrSearchForQueryResponse(String queryString, String filterQuery[], String[] fields,
        Integer pageSize, Integer startIndex, String sortField, String sortDirection) throws Exception {
    SolrQuery solrQuery = initSolrQuery(null); // general search settings
    solrQuery.setFields(fields);
    solrQuery.setQuery(queryString);
    return getSolrQueryResponse(solrQuery, filterQuery, pageSize, startIndex, sortField, sortDirection);
}

From source file:org.apache.blur.slur.BlurQueryHelperTest.java

License:Apache License

@Test(expected = IllegalArgumentException.class)
public void fieldValuesMustFollowBlursFamilyColumnFormat() {
    SolrQuery p = new SolrQuery();

    p.setFields("foo");

    BlurQuery query = BlurQueryHelper.from(p);
}