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

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

Introduction

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

Prototype

public SolrQuery setRows(Integer rows) 

Source Link

Usage

From source file:cz.incad.vdkcommon.solr.Indexer.java

License:Open Source License

public void removeAllDemands() throws Exception {
    SolrQuery query = new SolrQuery("poptavka:[* TO *]");
    query.addField("code");
    query.setRows(1000);
    SolrDocumentList docs = IndexerQuery.query(query);
    long numFound = docs.getNumFound();
    Iterator<SolrDocument> iter = docs.iterator();
    while (iter.hasNext()) {
        if (jobData.isInterrupted()) {
            LOGGER.log(Level.INFO, "INDEXER INTERRUPTED");
            break;
        }/*from   ww  w  .  j av  a 2s.c o m*/
        StringBuilder sb = new StringBuilder();

        SolrDocument resultDoc = iter.next();
        String docCode = (String) resultDoc.getFieldValue("code");
        sb.append("<add><doc>");
        sb.append("<field name=\"code\">").append(docCode).append("</field>");
        sb.append("<field name=\"md5\">").append(docCode).append("</field>");

        sb.append("<field name=\"poptavka\" update=\"set\" null=\"true\" />");
        sb.append("<field name=\"poptavka_ext\" update=\"set\" null=\"true\" />");
        sb.append("</doc></add>");
        SolrIndexerCommiter.postData(sb.toString());
        SolrIndexerCommiter.postData("<commit/>");
        LOGGER.log(Level.INFO, "Demands for {0} removed.", docCode);
    }
    if (numFound > 0 && !jobData.isInterrupted()) {
        removeAllDemands();
    }
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

License:Open Source License

public void reindexDocByIdentifier(String identifier) throws Exception {
    LOGGER.log(Level.INFO, "----- Reindexing doc {0} ...", identifier);

    SolrQuery query = new SolrQuery("id:\"" + identifier + "\"");
    query.addField("id,code");
    query.setRows(1000);
    SolrDocumentList docs = IndexerQuery.query(opts.getString("solrIdCore", "vdk_id"), query);
    Iterator<SolrDocument> iter = docs.iterator();
    while (iter.hasNext()) {
        SolrDocument resultDoc = iter.next();
        String uniqueCode = (String) resultDoc.getFieldValue("code");
        reindexDoc(uniqueCode, identifier);
    }/*ww  w .  ja v a 2  s  . c  o m*/
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

License:Open Source License

public void reindexDoc(String uniqueCode, String identifier) throws Exception {

    SolrQuery query = new SolrQuery("id:\"" + identifier + "\"");
    query.addField("id,code");
    query.setRows(1000);
    SolrDocumentList docs = IndexerQuery.query(query);
    Iterator<SolrDocument> iter = docs.iterator();
    if (iter.hasNext()) {
        SolrDocument resultDoc = iter.next();
        String oldUniqueCode = (String) resultDoc.getFieldValue("code");

        if (oldUniqueCode != null && !oldUniqueCode.equals(uniqueCode)) {
            LOGGER.log(Level.INFO, "Cleaning doc {0} from index...", oldUniqueCode);
            String s = "<delete><query>code:" + oldUniqueCode + "</query></delete>";
            SolrIndexerCommiter.postData(s);
            indexDoc(oldUniqueCode);//from   w w  w  . ja va  2 s. c om
        }
    }

    LOGGER.log(Level.INFO, "Cleaning doc {0} from index...", uniqueCode);
    String s = "<delete><query>code:" + uniqueCode + "</query></delete>";
    SolrIndexerCommiter.postData(s);

    indexDoc(uniqueCode);
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

License:Open Source License

public void indexDoc(String uniqueCode) throws Exception {

    try {/*from   ww w. ja v a  2  s  .co  m*/
        LOGGER.log(Level.FINE, "Indexace doc {0}...", uniqueCode);
        StringBuilder sb = new StringBuilder();
        sb.append("<add><doc>");
        sb.append("<field name=\"code\">").append(uniqueCode).append("</field>");
        sb.append("");
        SolrQuery query = new SolrQuery("code:\"" + uniqueCode + "\"");
        query.addField("id,code,code_type,xml,bohemika,zdroj");
        query.setRows(1000);
        SolrDocumentList docs = IndexerQuery.query(opts.getString("solrIdCore", "vdk_id"), query);
        Iterator<SolrDocument> iter = docs.iterator();
        String codeType = "";
        String md5 = "";
        boolean bohemika = false;
        int docsNum = 0;
        while (iter.hasNext()) {
            if (jobData.isInterrupted()) {
                LOGGER.log(Level.INFO, "INDEXER INTERRUPTED");
                break;
            }
            SolrDocument resultDoc = iter.next();

            if (resultDoc.getFieldValue("bohemika") != null) {
                bohemika = (Boolean) resultDoc.getFieldValue("bohemika");
            } else {
                bohemika = Bohemika.isBohemika((String) resultDoc.getFieldValue("xml"));
            }
            codeType = (String) resultDoc.getFieldValue("code_type");

            sb.append(transformXML((String) resultDoc.getFieldValue("xml"), uniqueCode,
                    (String) resultDoc.getFieldValue("code_type"), (String) resultDoc.getFieldValue("id"),
                    bohemika, (String) resultDoc.getFieldValue("zdroj")));

            sb.append(getDocOffers(uniqueCode));
            docsNum++;
            total++;

        }
        sb.append("<field name=\"code_type\">").append(codeType).append("</field>");
        sb.append("<field name=\"md5\">").append(md5).append("</field>");
        sb.append("<field name=\"bohemika\">").append(bohemika).append("</field>");
        sb.append("<field name=\"pocet_doc\">").append(docsNum).append("</field>");

        sb.append("</doc></add>");
        SolrIndexerCommiter.postData(sb.toString());
        //removeDocOffers(uniqueCode);
        //indexDocOffers(uniqueCode);
        //SolrIndexerCommiter.postData("<commit/>");
        //logger.log(Level.INFO, "Doc indexed. Total docs: {0}", total);
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Error in reindex", ex);
    }
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

License:Open Source License

private boolean isInDemandsCache(String code) {
    if (demandsCache == null) {
        try {/*from   w ww  .j  a  va2  s .c o  m*/
            SolrQuery query = new SolrQuery("poptavka_ext:[* TO *]");
            query.addField("id,code,poptavka_ext,title");
            query.setRows(1000);
            demandsCache = new HashMap<>();
            SolrDocumentList docs = IndexerQuery.query(query);
            Iterator<SolrDocument> iter = docs.iterator();
            while (iter.hasNext()) {
                SolrDocument doc = iter.next();
                demandsCache.put((String) doc.getFieldValue("code"), doc);

            }
        } catch (SolrServerException | IOException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }
    }
    return demandsCache.containsKey(code);
}

From source file:cz.incad.vdkcommon.solr.Indexer.java

License:Open Source License

public void run() throws Exception {

    readStatus();/*from  w  w  w. j  a v  a 2s .  co m*/
    if (jobData.getBoolean("full_index", false)) {
        reindex();
    } else {
        if (statusJson.has(LAST_UPDATE)) {
            update(statusJson.getString(LAST_UPDATE));
        } else {
            update(null);
        }
        if (jobData.getBoolean("reindex_offers", false)) {
            removeAllOffers();
            indexAllOffers();
        }
        if (jobData.getBoolean("reindex_demands", false)) {
            removeAllDemands();
            indexAllDemands();
        }
        if (jobData.getBoolean("reindex_wanted", false)) {
            removeAllWanted();
            indexAllWanted();
        }
        if (!"".equals(jobData.getString("identifier", ""))) {
            LOGGER.log(Level.INFO, "----- Reindexing doc {0} ...", jobData.getString("identifier"));

            SolrQuery query = new SolrQuery("id:\"" + jobData.getString("identifier") + "\"");
            query.addField("id,code");
            query.setRows(1000);
            SolrDocumentList docs = IndexerQuery.query(opts.getString("solrIdCore", "vdk_id"), query);
            Iterator<SolrDocument> iter = docs.iterator();
            while (iter.hasNext()) {
                SolrDocument resultDoc = iter.next();
                String uniqueCode = (String) resultDoc.getFieldValue("code");
                reindexDoc(uniqueCode, jobData.getString("identifier"));
            }

        }
    }
    writeStatus();

}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Configures the query to be processed.
 * @param inputQuery The query string.//from ww  w  .  j a v a  2s  . c  om
 * @param start Index of the first returned result.
 * @param count Number of results we wish to display.
 * @return Configured query.
 */
private SolrQuery configureQuery(String inputQuery, ResultCategory category, int start, int count) {
    SolrQuery query = new SolrQuery();
    query.setQuery(inputQuery);
    if (category != null && !category.equals(ResultCategory.ALL)) {
        query.addFilterQuery(IndexField.CLASS.getValue() + ":\"" + category.getValue() + "\"");
    }
    query.setStart(start);
    query.setRows(count);
    query.setHighlightSimplePre(FullTextSearchUtils.HIGHLIGHTED_TEXT_BEGIN);
    query.setHighlightSimplePost(FullTextSearchUtils.HIGHLIGHTED_TEXT_END);
    return query;
}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Gets values to autocomplete for the input string.
 * The autocomplete feature works for multivalued fields and is based on a highlighting trick.
 * See http://solr.pl/en/2013/02/25/autocomplete-on-multivalued-fields-using-highlighting/
 * @param keywordStart/*  ww  w  . jav a2 s.co m*/
 * @return Autocomplete values.
 * @throws SolrServerException
 */
public Set<String> getTextToAutocomplete(String keywordStart) throws SolrServerException {
    SolrQuery query = new SolrQuery();
    query.setQuery(IndexField.AUTOCOMPLETE.getValue() + ":" + keywordStart);
    query.setFields(IndexField.AUTOCOMPLETE.getValue());
    query.setHighlight(true);
    query.setParam("hl.fl", IndexField.AUTOCOMPLETE.getValue());
    query.setHighlightSimplePre("");
    query.setHighlightSimplePost("");
    query.setRows(FullTextSearchUtils.AUTOCOMPLETE_ROWS);

    Map<String, Integer> map = new TreeMap<String, Integer>();
    QueryResponse response = solrServer.query(query);

    Set<String> foundIds = response.getHighlighting().keySet();
    for (String id : foundIds) {

        List<String> resultsPerDocument = response.getHighlighting().get(id)
                .get(IndexField.AUTOCOMPLETE.getValue());
        if (resultsPerDocument != null) {
            for (String result : resultsPerDocument) {
                String resultValue;
                int resultFrequency;
                int delimiterPosition = result.lastIndexOf('#');
                if (delimiterPosition == -1) { // autocomplete phrase was copied from title
                    resultValue = result;
                    resultFrequency = 0;
                } else {
                    resultValue = result.substring(0, delimiterPosition);
                    try {
                        resultFrequency = Integer
                                .valueOf(result.substring(delimiterPosition + 1, result.length()));
                    } catch (NumberFormatException e) {
                        resultFrequency = 0;
                    }
                }

                map.put(resultValue.toLowerCase(), resultFrequency);
            }
        }

        if (map.size() == FullTextSearchUtils.AUTOCOMPLETE_ROWS) {
            break;
        }
    }

    map = sortByValue(map);
    return map.keySet();
}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Gets the number of all documents matching the query.
 * @param queryString The query string./* w w w . j a v a2s .c  o  m*/
 * @return The number of all documents matching the query.
 */
public int getTotalNumberOfDocumentsForQuery(String queryString, ResultCategory category) {
    SolrQuery q = new SolrQuery();
    q.setQuery(queryString);
    if (category != null && !category.equals(ResultCategory.ALL)) {
        q.setFilterQueries(IndexField.CLASS.getValue() + ":\"" + category.getValue() + "\"");
    }
    q.setRows(0); // don't actually request any data
    try {
        return (int) solrServer.query(q).getResults().getNumFound();
    } catch (SolrServerException e) {
        log.error(e);
        e.getCause().printStackTrace();
    }
    return 0;
}

From source file:datacite.oai.provider.service.MDSSearchServiceSolrImpl.java

License:Open Source License

static SolrQuery constructSolrQuery(Date updateDateFrom, Date updateDateTo, String setspec, int offset,
        int length) throws ServiceException {
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.setRows(length);
    query.setStart(offset);//from   w  w w .  j  a v a 2s.com
    query.setSortField("updated", ORDER.asc);

    setspec = StringUtils.trimToEmpty(setspec);
    if (setspec.contains(Constants.Set.BASE64_PART_DELIMITER)) {
        String split[] = setspec.split(Constants.Set.BASE64_PART_DELIMITER, 2);
        setspec = split[0];
        String base64 = split[1];
        String solrfilter = new String(Base64.decodeBase64(base64));
        logger.info("decoded base64 setspec: " + solrfilter);
        solrfilter = solrfilter.replaceAll("^[?&]+", "");

        List<NameValuePair> params = URLEncodedUtils.parse(solrfilter, Charset.defaultCharset());
        for (NameValuePair param : params) {
            String name = param.getName();
            String value = param.getValue();
            if (name.equals("q"))
                query.setQuery(value);
            else if (name.equals("fq"))
                query.addFilterQuery(value);
            else
                throw new ServiceException("parameter '" + name + "' is not supported");
        }
    }

    if (setspec != null && setspec.trim().length() > 0) {
        setspec = setspec.trim().toUpperCase();

        String field = setspec.contains(".") ? "datacentre_symbol" : "allocator_symbol";
        query.addFilterQuery(field + ":" + setspec);
    }

    String from = dateFormat.format(updateDateFrom);
    String to = dateFormat.format(updateDateTo);

    query.addFilterQuery("updated:[" + from + " TO " + to + "]");

    query.setParam(CommonParams.QT, "/api");

    return query;
}