Example usage for org.apache.solr.common SolrDocumentList getNumFound

List of usage examples for org.apache.solr.common SolrDocumentList getNumFound

Introduction

In this page you can find the example usage for org.apache.solr.common SolrDocumentList getNumFound.

Prototype

public long getNumFound() 

Source Link

Usage

From source file:com.lyncode.oai.proxy.data.ProxyItemRepository.java

License:Apache License

@Override
public ListItemsResults getItems(List<Filter> filters, int offset, int length) {
    try {// www .  j  av a  2  s.c o m
        List<String> whereCond = new ArrayList<String>();
        for (Filter filter : filters) {
            if (filter.getFilter() instanceof ProxyFilter) {
                ProxyFilter proxyFilter = (ProxyFilter) filter.getFilter();
                whereCond.add("(" + proxyFilter.query() + ")");
            }
        }
        if (whereCond.isEmpty())
            whereCond.add("*:*");
        SolrQuery query = new SolrQuery("(" + StringUtils.join(whereCond.iterator(), ") AND (") + ")");
        query.setStart(offset);
        query.setRows(length);
        SolrDocumentList list = SolrServerManager.getServer().query(query).getResults();
        List<AbstractItem> results = new ArrayList<AbstractItem>();
        for (SolrDocument doc : list)
            results.add(new ProxyItem(doc));
        return new ListItemsResults(list.getNumFound() > offset + length, results, (int) list.getNumFound());
    } catch (SolrServerException e) {
        return new ListItemsResults(false, new ArrayList<AbstractItem>());
    }
}

From source file:com.mustardgrain.solr.SolrClient.java

License:Apache License

/**
 * Tries to query a live server. A SolrServerException is thrown if all
 * servers are dead. If the request failed due to IOException then the live
 * server is moved to dead pool and the request is retried on another live
 * server. After live servers are exhausted, any servers previously marked
 * as dead will be tried before failing the request.
 * /*from   w ww .  j ava 2  s .com*/
 * @param request the SolrRequest.
 * @return response
 * @throws IOException If there is a low-level I/O error.
 */
@Override
public NamedList<Object> request(final SolrRequest request) throws SolrServerException, IOException {
    Exception ex = null;
    ServerWrapper[] serverList = aliveServerList;

    int maxTries = serverList.length;
    Map<String, ServerWrapper> justFailed = null;

    for (int attempts = 0; attempts < maxTries; attempts++) {
        int count = counter.incrementAndGet();
        ServerWrapper wrapper = serverList[count % serverList.length];
        wrapper.lastUsed = System.currentTimeMillis();

        try {
            long start = System.currentTimeMillis();
            NamedList<Object> response = wrapper.solrServer.request(request);
            long end = System.currentTimeMillis();
            updateStatsSuccess(wrapper.solrServer, end - start);

            SolrDocumentList results = (SolrDocumentList) response.get("response");

            if (results != null) {
                if (results.getNumFound() <= 0)
                    updateStatsEmptyResults(wrapper.solrServer);
            }

            return response;
        } catch (SolrException e) {
            // Server is alive but the request was malformed or invalid
            updateStatsException(wrapper.solrServer, e);
            throw e;
        } catch (SolrServerException e) {
            Throwable rootCause = e.getRootCause();
            updateStatsException(wrapper.solrServer, rootCause);

            if (rootCause instanceof IOException) {
                ex = e;
                moveAliveToDead(wrapper, e);
                if (justFailed == null)
                    justFailed = new HashMap<String, ServerWrapper>();
                justFailed.put(wrapper.getKey(), wrapper);
            } else {
                throw e;
            }
        } catch (Exception e) {
            updateStatsException(wrapper.solrServer, e);
            throw new SolrServerException(e);
        }
    }

    // try other standard servers that we didn't try just now
    for (ServerWrapper wrapper : zombieServers.values()) {
        if (wrapper.standard == false || justFailed != null && justFailed.containsKey(wrapper.getKey()))
            continue;
        try {
            NamedList<Object> rsp = wrapper.solrServer.request(request);
            // remove from zombie list *before* adding to alive to avoid a
            // race that could lose a server
            zombieServers.remove(wrapper.getKey());
            addToAlive(wrapper);

            if (LOG.isInfoEnabled())
                LOG.info("Marking server " + wrapper.solrServer.getBaseURL()
                        + " active after exhausting attempts to contact known active servers failed");

            return rsp;
        } catch (SolrException e) {
            // Server is alive but the request was malformed or invalid
            updateStatsException(wrapper.solrServer, e);
            throw e;
        } catch (SolrServerException e) {
            Throwable rootCause = e.getRootCause();
            updateStatsException(wrapper.solrServer, rootCause);
            if (rootCause instanceof IOException) {
                ex = e;
                // still dead
            } else {
                throw e;
            }
        } catch (Exception e) {
            updateStatsException(wrapper.solrServer, e);
            throw new SolrServerException(e);
        }
    }

    if (ex == null) {
        throw new SolrServerException("No live SolrServers available to handle this request");
    } else {
        throw new SolrServerException("No live SolrServers available to handle this request", ex);
    }
}

From source file:com.norconex.committer.solr.SolrCommitterSolrIntegrationTest.java

License:Apache License

@Test
public void testCommitAdd() throws Exception {

    String content = "hello world!";
    InputStream is = IOUtils.toInputStream(content);

    String id = "1";
    Properties metadata = new Properties();
    metadata.addString("id", id);

    // Add new doc to Solr
    committer.add(id, is, metadata);//from   w ww . j  a v  a2 s .c  o m

    committer.commit();

    IOUtils.closeQuietly(is);

    // Check that it's in Solr
    SolrDocumentList results = queryId(id);
    assertEquals(1, results.getNumFound());
}

From source file:com.norconex.committer.solr.SolrCommitterSolrIntegrationTest.java

License:Apache License

public void testSolrJWith3AddCommandAnd1DeleteCommand() throws Exception {
    SolrInputDocument doc1 = new SolrInputDocument();
    SolrInputDocument doc2 = new SolrInputDocument();
    SolrInputDocument doc3 = new SolrInputDocument();
    doc1.addField("id", "1");
    doc2.addField("id", "2");
    doc3.addField("id", "3");
    server.add(doc1);//from  w w  w  .  j  ava 2  s.c om
    server.add(doc2);
    server.deleteById("1");
    server.add(doc3);
    server.commit();
    SolrDocumentList results = getAllDocs();
    assertEquals(2, results.getNumFound());

}

From source file:com.norconex.committer.solr.SolrCommitterSolrIntegrationTest.java

License:Apache License

@Test
public void testAddWithQueueContaining2documents() throws Exception {
    String content = "Document 1";
    InputStream is = IOUtils.toInputStream(content);

    String content2 = "Document 2";
    InputStream is2 = IOUtils.toInputStream(content2);

    String id = "1";
    String id2 = "2";

    Properties metadata = new Properties();
    metadata.addString("id", id);

    Properties metadata2 = new Properties();
    metadata.addString("id", id2);

    committer.add(id, is, metadata);//from w  w  w.j av  a 2  s . co  m
    committer.add(id2, is2, metadata2);
    committer.commit();
    IOUtils.closeQuietly(is);

    //Check that there is 2 documents in Solr
    SolrDocumentList results = getAllDocs();
    assertEquals(2, results.getNumFound());
}

From source file:com.norconex.committer.solr.SolrCommitterSolrIntegrationTest.java

License:Apache License

@Test
public void testCommitQueueWith3AddCommandAnd1DeleteCommand() throws Exception {
    UpdateResponse worked = server.deleteByQuery("*:*");
    committer.commit();/*from   w  ww .ja v  a 2s . co  m*/

    System.out.println("deleted " + worked.toString());
    String content1 = "Document 1";
    InputStream doc1Content = IOUtils.toInputStream(content1);
    String id1 = "1";
    Properties doc1Metadata = new Properties();
    doc1Metadata.addString("id", id1);

    String content2 = "Document 2";
    String id2 = "2";
    InputStream doc2Content = IOUtils.toInputStream(content2);
    Properties doc2Metadata = new Properties();
    doc2Metadata.addString("id", "2");

    String content3 = "Document 3";
    String id3 = "3";
    InputStream doc3Content = IOUtils.toInputStream(content3);
    Properties doc3Metadata = new Properties();
    doc2Metadata.addString("id", "3");

    committer.add(id1, doc1Content, doc1Metadata);
    committer.add(id2, doc2Content, doc2Metadata);

    //TODO hacking this part of the test until a more solid fix is found in 
    //SolrCommitter
    committer.commit();

    committer.remove(id1, doc1Metadata);
    committer.add(id3, doc3Content, doc3Metadata);

    committer.commit();

    IOUtils.closeQuietly(doc1Content);
    IOUtils.closeQuietly(doc2Content);
    IOUtils.closeQuietly(doc3Content);

    //Check that there is 2 documents in Solr
    SolrDocumentList results = getAllDocs();
    System.out.println("results " + results.toString());
    assertEquals(2, results.getNumFound());
    System.out.println("Writing/Reading this => " + committer);
}

From source file:com.norconex.committer.solr.SolrCommitterSolrIntegrationTest.java

License:Apache License

@Test
public void testCommitQueueWith3AddCommandAnd2DeleteCommand() throws Exception {

    UpdateResponse worked = server.deleteByQuery("*:*");
    committer.commit();//  w  ww  .j  a v a  2  s.  co  m
    System.out.println("deleted " + worked.toString());
    String content = "Document 1";
    InputStream doc1Content = IOUtils.toInputStream(content);
    String id1 = "1";
    Properties doc1Metadata = new Properties();
    doc1Metadata.addString("id", id1);

    String content2 = "Document 2";
    String id2 = "2";
    InputStream doc2Content = IOUtils.toInputStream(content2);
    Properties doc2Metadata = new Properties();
    doc2Metadata.addString("id", "2");

    String content3 = "Document 3";
    String id3 = "3";
    InputStream doc3Content = IOUtils.toInputStream(content3);
    Properties doc3Metadata = new Properties();
    doc2Metadata.addString("id", "3");

    committer.add(id1, doc1Content, doc1Metadata);
    committer.add(id2, doc2Content, doc2Metadata);

    //TODO hacking this part of the test until a more solid fix is found in 
    //SolrCommitter
    committer.commit();

    committer.remove(id1, doc1Metadata);
    committer.remove(id2, doc1Metadata);
    committer.add(id3, doc3Content, doc3Metadata);
    committer.commit();

    IOUtils.closeQuietly(doc1Content);
    IOUtils.closeQuietly(doc2Content);
    IOUtils.closeQuietly(doc3Content);

    //Check that there is 2 documents in Solr
    SolrDocumentList results = getAllDocs();
    System.out.println("results " + results.toString());
    assertEquals(1, results.getNumFound());
    System.out.println("Writing/Reading this => " + committer);
}

From source file:com.norconex.committer.solr.SolrCommitterSolrIntegrationTest.java

License:Apache License

@Test
public void testCommitDelete() throws Exception {

    // Add a document directly to Solr
    SolrInputDocument doc = new SolrInputDocument();
    String id = "1";
    doc.addField(SolrCommitter.DEFAULT_SOLR_ID_FIELD, id);
    String content = "hello world!";
    doc.addField(SolrCommitter.DEFAULT_SOLR_CONTENT_FIELD, content);

    server.add(doc);/*w w w .  j a  v a 2 s. c  o  m*/
    server.commit();

    // Queue it to be deleted
    Properties metadata = new Properties();
    metadata.addString("id", id);
    committer.remove(id, metadata);

    committer.commit();

    // Check that it's remove from Solr
    SolrDocumentList results = queryId(id);
    assertEquals(0, results.getNumFound());
}

From source file:com.norconex.committer.solr.SolrCommitterTest.java

License:Apache License

@Test
public void testCommitAdd() throws Exception {

    String content = "hello world!";
    InputStream is = IOUtils.toInputStream(content);

    String id = "1";
    Properties metadata = new Properties();
    metadata.addString("id", id);

    // Add new doc to Solr
    committer.add(id, is, metadata);//  w  w  w . j a  v a 2 s. co m

    committer.commit();

    IOUtils.closeQuietly(is);

    // Check that it's in Solr
    SolrDocumentList results = queryId(id);
    assertEquals(1, results.getNumFound());
    assertEquals(id, results.get(0).get(SolrCommitter.DEFAULT_SOLR_ID_FIELD));
    // TODO we need to trim because of the extra white spaces returned by
    // Solr. Why is that?
    assertEquals(content, results.get(0).get(SolrCommitter.DEFAULT_SOLR_CONTENT_FIELD).toString().trim());
}

From source file:com.norconex.committer.solr.SolrCommitterTest.java

License:Apache License

@Test
public void testCommitDelete() throws Exception {

    // Add a document directly to Solr
    SolrInputDocument doc = new SolrInputDocument();
    String id = "1";
    doc.addField(SolrCommitter.DEFAULT_SOLR_ID_FIELD, id);
    String content = "hello world!";
    doc.addField(SolrCommitter.DEFAULT_SOLR_CONTENT_FIELD, content);
    server.add(doc);//from   w  ww  .j a  v  a 2s  . co  m
    server.commit();

    // Queue it to be deleted
    Properties metadata = new Properties();
    metadata.addString("id", id);
    committer.remove(id, metadata);

    committer.commit();

    // Check that it's remove from Solr
    SolrDocumentList results = queryId(id);
    assertEquals(0, results.getNumFound());
}