Example usage for org.apache.solr.client.solrj SolrClient commit

List of usage examples for org.apache.solr.client.solrj SolrClient commit

Introduction

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

Prototype

public UpdateResponse commit(boolean waitFlush, boolean waitSearcher) throws SolrServerException, IOException 

Source Link

Document

Performs an explicit commit, causing pending documents to be committed for indexing Be very careful when triggering commits from the client side.

Usage

From source file:net.yacy.cora.federate.solr.instance.ServerShard.java

License:Open Source License

/** 
 * Performs an explicit commit, causing pending documents to be committed for indexing
 * @param waitFlush  block until index changes are flushed to disk
 * @param waitSearcher  block until a new searcher is opened and registered as the main query searcher, making the changes visible 
 * @throws IOException If there is a low-level I/O error.
 */// ww  w .j  ava 2s  .c o m
@Override
public UpdateResponse commit(boolean waitFlush, boolean waitSearcher) throws SolrServerException, IOException {
    if (!this.writeEnabled)
        return _dummyOKResponse;
    UpdateResponse ur = null;
    for (SolrClient s : this.shards)
        ur = s.commit(waitFlush, waitSearcher);
    return ur;
}

From source file:org.apache.nutch.indexwriter.solr.TestSolrJ.java

License:Apache License

/**
 * query the example/*w  w  w. j  a  va 2 s  . c o  m*/
 */
@Test
public void testQuery() throws Exception {
    SolrClient client = new HttpSolrClient.Builder(serverUrl).build();

    // Empty the database...
    client.deleteByQuery("*:*");// delete everything!

    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    String docID = "1112211111";
    doc.addField("id", docID, 1.0f);
    doc.addField("name", "my name!", 1.0f);

    assertEquals(null, doc.getField("foo"));
    assertTrue(doc.getField("name").getValue() != null);

    UpdateResponse upres = client.add(doc);
    // System.out.println( "ADD:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    SolrQuery query = new SolrQuery();
    query.setQuery("id:" + docID);
    QueryResponse response = client.query(query);

    assertEquals(docID, response.getResults().get(0).getFieldValue("id"));

    // Now add a few docs for facet testing...
    List<SolrInputDocument> docs = new ArrayList<>();
    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField("id", "2", 1.0f);
    doc2.addField("inStock", true, 1.0f);
    doc2.addField("price", 2, 1.0f);
    doc2.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc2);
    SolrInputDocument doc3 = new SolrInputDocument();
    doc3.addField("id", "3", 1.0f);
    doc3.addField("inStock", false, 1.0f);
    doc3.addField("price", 3, 1.0f);
    doc3.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc3);
    SolrInputDocument doc4 = new SolrInputDocument();
    doc4.addField("id", "4", 1.0f);
    doc4.addField("inStock", true, 1.0f);
    doc4.addField("price", 4, 1.0f);
    doc4.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc4);
    SolrInputDocument doc5 = new SolrInputDocument();
    doc5.addField("id", "5", 1.0f);
    doc5.addField("inStock", false, 1.0f);
    doc5.addField("price", 5, 1.0f);
    doc5.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc5);

    upres = client.add(docs);
    // System.out.println( "ADD:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    query = new SolrQuery("*:*");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");
    query.addFacetQuery("price:[5 TO *]");
    query.addFacetField("inStock");
    query.addFacetField("price");
    query.addFacetField("timestamp_dt");
    query.removeFilterQuery("inStock:true");

    response = client.query(query);
    assertEquals(0, response.getStatus());
    assertEquals(5, response.getResults().getNumFound());
    assertEquals(3, response.getFacetQuery().size());
    assertEquals(2, response.getFacetField("inStock").getValueCount());
    assertEquals(4, response.getFacetField("price").getValueCount());

    // test a second query, test making a copy of the main query
    SolrQuery query2 = query.getCopy();
    query2.addFilterQuery("inStock:true");
    response = client.query(query2);
    assertEquals(1, query2.getFilterQueries().length);
    assertEquals(0, response.getStatus());
    assertEquals(2, response.getResults().getNumFound());
    assertFalse(query.getFilterQueries() == query2.getFilterQueries());

    // sanity check round tripping of params...
    query = new SolrQuery("foo");
    query.addFilterQuery("{!field f=inStock}true");
    query.addFilterQuery("{!term f=name}hoss");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");

    response = client.query(query);
    assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(),
            response.getResponseHeader().get("params") instanceof NamedList);
    NamedList echo = (NamedList) response.getResponseHeader().get("params");
    List values = null;
    assertEquals("foo", echo.get("q"));
    assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List);
    values = (List) echo.get("fq");
    assertEquals(2, values.size());
    assertEquals("{!field f=inStock}true", values.get(0));
    assertEquals("{!term f=name}hoss", values.get(1));
    assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(),
            echo.get("facet.query") instanceof List);
    values = (List) echo.get("facet.query");
    assertEquals(2, values.size());
    assertEquals("price:[* TO 2]", values.get(0));
    assertEquals("price:[2 TO 4]", values.get(1));
}

From source file:uk.bl.wa.annotation.CollectionsUpdateTest.java

License:Open Source License

private static void doUpdate(SolrClient ss, String id, String collection, String collections)
        throws SolrServerException, IOException {

    ss.add(createUpdateDocument(id, collection, collections));

    ss.commit(true, true);

    System.out.println("Updated.");
}