Java Code Examples for org.apache.solr.client.solrj.SolrClient#commit()

The following examples show how to use org.apache.solr.client.solrj.SolrClient#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestReplicationHandlerDiskOverFlow.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private long indexDocs(SolrClient client, int totalDocs, int start) throws Exception {
  for (int i = 0; i < totalDocs; i++)
    TestReplicationHandler.index(client, "id", i + start, "name", TestUtil.randomSimpleString(random(), 1000, 5000));
  client.commit(true, true);
  QueryResponse response = client.query(new SolrQuery()
      .add("qt", "/replication")
      .add("command", "filelist")
      .add("generation", "-1"));

  long totalSize = 0;
  for (Map map : (List<Map>) response.getResponse().get(CMD_GET_FILE_LIST)) {
    Long sz = (Long) map.get(ReplicationHandler.SIZE);
    totalSize += sz;
  }
  return totalSize;
}
 
Example 2
Source File: SolrLocatorTest.java    From kite with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectsEmbeddedSolrServerAndAddDocument() throws Exception {
  //Solr locator should select EmbeddedSolrServer only solrHome is specified
  SolrLocator solrLocator = new SolrLocator(new SolrMorphlineContext.Builder().build());
  solrLocator.setSolrHomeDir(RESOURCES_DIR + "/solr");
  solrLocator.setCollectionName("collection1");

  SolrServerDocumentLoader documentLoader = (SolrServerDocumentLoader)solrLocator.getLoader();
  SolrClient solrServer = documentLoader.getSolrServer();
  
  assertTrue(solrServer instanceof EmbeddedSolrServer);
  
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "myId");
  doc.addField("text", "myValue");
  solrServer.add(doc);
  solrServer.commit();
  
  SolrDocument resultDoc = solrServer.getById("myId");
  assertTrue(resultDoc.getFieldValues("text").contains("myValue"));
  
  UpdateResponse deleteResponse = solrServer.deleteById("myId");
  assertEquals(0, deleteResponse.getStatus());
  solrServer.commit();
  solrServer.close();
}
 
Example 3
Source File: UsingSolrJRefGuideExamplesTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void indexWithSolrInputDocumentExample() throws Exception {
  // tag::solrj-index-with-raw-solrinputdoc[]
  final SolrClient client = getSolrClient();

  final SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", UUID.randomUUID().toString());
  doc.addField("name", "Amazon Kindle Paperwhite");

  final UpdateResponse updateResponse = client.add("techproducts", doc);
  // Indexed documents must be committed
  client.commit("techproducts");
  // end::solrj-index-with-raw-solrinputdoc[]

  assertNumDocuments(NUM_INDEXED_DOCUMENTS + 1);
}
 
Example 4
Source File: BackupRestoreUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static int indexDocs(SolrClient masterClient, String collectionName, long docsSeed) throws IOException, SolrServerException {
  masterClient.deleteByQuery(collectionName, "*:*");

  Random random = new Random(docsSeed);// use a constant seed for the whole test run so that we can easily re-index.
  int nDocs = TestUtil.nextInt(random, 1, 100);
  log.info("Indexing {} test docs", nDocs);

  List<SolrInputDocument> docs = new ArrayList<>(nDocs);
  for (int i = 0; i < nDocs; i++) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", i);
    doc.addField("name", "name = " + i);
    docs.add(doc);
  }
  masterClient.add(collectionName, docs);
  masterClient.commit(collectionName);
  return nDocs;
}
 
Example 5
Source File: ConcurrentUpdateHttp2SolrClientMultiCollectionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void splitDocumentsAcrossCollections(SolrClient client, int numTotalDocs) throws IOException, SolrServerException {
  for (int docNum = 0; docNum < numTotalDocs; docNum++) {
    final SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "value" + docNum);

    if (docNum %2 == 0) {
      client.add(COLLECTION_ONE_NAME, doc);
    } else {
      client.add(COLLECTION_TWO_NAME, doc);
    }
  }

  client.commit(COLLECTION_ONE_NAME);
  client.commit(COLLECTION_TWO_NAME);
}
 
Example 6
Source File: TestRemoteStreaming.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Before
public void doBefore() throws IOException, SolrServerException {
  //add document and commit, and ensure it's there
  SolrClient client = getSolrClient();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField( "id", "1234" );
  client.add(doc);
  client.commit();
  assertTrue(searchFindsIt());
}
 
Example 7
Source File: SolrTestCaseHS.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void commit() throws IOException, SolrServerException {
  if (local()) {
    assertU(SolrTestCaseJ4.commit());
    return;
  }

  for (SolrClient client : provider.all()) {
    client.commit();
  }
}
 
Example 8
Source File: NestedShardedAtomicUpdateTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void indexDocAndRandomlyCommit(SolrClient client, SolrParams params, SolrInputDocument sdoc, boolean compareToControlCollection) throws IOException, SolrServerException {
  if (compareToControlCollection) {
    indexDoc(client, params, sdoc);
  } else {
    add(client, params, sdoc);
  }
  // randomly commit docs
  if (random().nextBoolean()) {
    client.commit();
  }
}
 
Example 9
Source File: SolrPageListener.java    From aem-solr-search with Apache License 2.0 5 votes vote down vote up
protected void addOrUpdatePage(Resource pageRes, SolrClient solr) {
		if (pageRes == null) {
				LOG.error("Page does not exist to add/update in solr");
				return;
		}
		GeometrixxMediaContentType dataPage = pageRes.adaptTo(GeometrixxMediaPage.class);
		try {
				LOG.info("Adding/updating page " + pageRes.getPath());
				solr.add(dataPage.getSolrDoc());
				solr.commit();
		} catch (Exception e) {
				LOG.error("Failure to add/update page " + pageRes.getPath(), e);
		}
}
 
Example 10
Source File: MoveReplicaTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void addDocs(String collection, int numDocs) throws Exception {
  SolrClient solrClient = cluster.getSolrClient();
  for (int docId = 1; docId <= numDocs; docId++) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", docId);
    solrClient.add(collection, doc);
  }
  solrClient.commit(collection);
  Thread.sleep(5000);
}
 
Example 11
Source File: HBaseMapReduceIndexerTool.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
private void commitSolr(Map<String, String> indexConnectionParams) throws SolrServerException, IOException {
     Set<SolrClient> servers = createSolrClients(indexConnectionParams);
     for (SolrClient server : servers) {
         server.commit(false, false);
try {
	server.close();
} catch (java.io.IOException e) {
   throw new RuntimeException(e);
}
     }
 }
 
Example 12
Source File: NoOpResponseParserTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Before
public void doBefore() throws IOException, SolrServerException {
  //add document and commit, and ensure it's there
  SolrClient client = getSolrClient();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "1234");
  client.add(doc);
  client.commit();
}
 
Example 13
Source File: ContentEditor.java    From jease with GNU General Public License v3.0 5 votes vote down vote up
public void insertToSolr() {

		String solrurl = jease.Registry.getParameter(jease.Names.JEASE_SOLR_URL, "");

		if (solrurl.equals("")) {
			return;
		}

		String oid = checkDuplication();
		if (oid.length() > 0) {
			updateToSolr(oid);
			return;
		}
		try {
			ArrayList<String> tagslist = new ArrayList<String>(Arrays.asList(tags.getValue().split(",")));
			SolrClient client = new HttpSolrClient.Builder(solrurl).build();
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("id", UUID.randomUUID().toString());
			doc.addField("tags", tagslist);
			doc.addField("jeaseid", id.getValue());
			doc.addField("jeasepath", getNode().getPath());
			doc.addField("title", title.getValue());
			doc.addField("author", getNode().getEditor().getName());
			doc.addField("type", getNode().getType());
			doc.addField("text", getNode().getFulltext().toString());
			doc.addField("last_modified", new Date());
			doc.addField("date", month_date.format(new Date()));
			doc.addField("category", getNode().getParent().getId());
			client.add(doc);
			client.commit();
		} catch (Exception s) {
			s.printStackTrace();
		}
	}
 
Example 14
Source File: TestPutSolrRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that given SolrServer contains the expected SolrDocuments.
 */
private static void verifySolrDocuments(SolrClient solrServer, Collection<SolrDocument> expectedDocuments)
        throws IOException, SolrServerException {

    solrServer.commit();

    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrServer.query(query);
    Assert.assertEquals(expectedDocuments.size(), qResponse.getResults().getNumFound());

    // verify documents have expected fields and values
    for (SolrDocument expectedDoc : expectedDocuments) {
        boolean found = false;
        for (SolrDocument solrDocument : qResponse.getResults()) {
            boolean foundAllFields = true;
            for (String expectedField : expectedDoc.getFieldNames()) {
                Object expectedVal = expectedDoc.getFirstValue(expectedField);
                Object actualVal = solrDocument.getFirstValue(expectedField);
                foundAllFields = expectedVal.equals(actualVal);
            }

            if (foundAllFields) {
                found = true;
                break;
            }
        }
        Assert.assertTrue("Could not find " + expectedDoc, found);
    }
}
 
Example 15
Source File: PeerSyncWithBufferUpdatesTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void validateDocs(Set<Integer> docsAdded, SolrClient client0, SolrClient client1) throws SolrServerException, IOException {
  client0.commit();
  client1.commit();
  QueryResponse qacResponse;
  qacResponse = queryAndCompare(params("q", "*:*", "rows", "10000", "sort","_version_ desc"), client0, client1);
  validateQACResponse(docsAdded, qacResponse);
}
 
Example 16
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * * Commits to the specified client, and optionally all shards
 */
protected static void commit(SolrClient client, boolean andShards) throws Exception
{
    client.commit();
    if (andShards)
    {
        for (SolrClient cshards : clientShards)
        {
            cshards.commit();
        }
    }
}
 
Example 17
Source File: AbstractAlfrescoDistributedIT.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void explicitCommitOnAllClients() throws Exception
{
    List<SolrClient> clients = getStandaloneAndShardedClients();

    for (SolrClient client : clients) {
        client.commit();
    }
}
 
Example 18
Source File: TestPutSolrContentStream.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that given SolrServer contains the expected SolrDocuments.
 */
private static void verifySolrDocuments(SolrClient solrServer, Collection<SolrDocument> expectedDocuments)
        throws IOException, SolrServerException {

    solrServer.commit();

    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrServer.query(query);
    Assert.assertEquals(expectedDocuments.size(), qResponse.getResults().getNumFound());

    // verify documents have expected fields and values
    for (SolrDocument expectedDoc : expectedDocuments) {
        boolean found = false;
        for (SolrDocument solrDocument : qResponse.getResults()) {
            boolean foundAllFields = true;
            for (String expectedField : expectedDoc.getFieldNames()) {
                Object expectedVal = expectedDoc.getFirstValue(expectedField);
                Object actualVal = solrDocument.getFirstValue(expectedField);
                foundAllFields = expectedVal.equals(actualVal);
            }

            if (foundAllFields) {
                found = true;
                break;
            }
        }
        Assert.assertTrue("Could not find " + expectedDoc, found);
    }
}
 
Example 19
Source File: AbstractSolrMorphlineTest.java    From kite with Apache License 2.0 4 votes vote down vote up
private void deleteAllDocuments() throws SolrServerException, IOException {
  collector.reset();
  SolrClient s = solrClient;
  s.deleteByQuery("*:*"); // delete everything!
  s.commit();
}
 
Example 20
Source File: BasicDistributedZkTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void testANewCollectionInOneInstance() throws Exception {
    log.info("### STARTING testANewCollectionInOneInstance");
    CollectionAdminResponse response = CollectionAdminRequest.createCollection(oneInstanceCollection, "conf1", 2, 2)
        .setCreateNodeSet(jettys.get(0).getNodeName())
        .setMaxShardsPerNode(4)
        .process(cloudClient);
    assertEquals(0, response.getStatus());
    List<SolrClient> collectionClients = new ArrayList<>();
    for (String coreName : response.getCollectionCoresStatus().keySet()) {
      collectionClients.add(createNewSolrClient(coreName, jettys.get(0).getBaseUrl().toString()));
    }
   
    SolrClient client1 = collectionClients.get(0);
    SolrClient client2 = collectionClients.get(1);
    SolrClient client3 = collectionClients.get(2);
    SolrClient client4 = collectionClients.get(3);
 
    waitForRecoveriesToFinish(oneInstanceCollection, getCommonCloudSolrClient().getZkStateReader(), false);
    assertAllActive(oneInstanceCollection, getCommonCloudSolrClient().getZkStateReader());
    
    client2.add(getDoc(id, "1")); 
    client3.add(getDoc(id, "2")); 
    client4.add(getDoc(id, "3")); 
    
    client1.commit();
    SolrQuery query = new SolrQuery("*:*");
    query.set("distrib", false);
    long oneDocs = client1.query(query).getResults().getNumFound();
    long twoDocs = client2.query(query).getResults().getNumFound();
    long threeDocs = client3.query(query).getResults().getNumFound();
    long fourDocs = client4.query(query).getResults().getNumFound();
    
    query.set("collection", oneInstanceCollection);
    query.set("distrib", true);
    long allDocs = getCommonCloudSolrClient().query(query).getResults().getNumFound();
    
//    System.out.println("1:" + oneDocs);
//    System.out.println("2:" + twoDocs);
//    System.out.println("3:" + threeDocs);
//    System.out.println("4:" + fourDocs);
//    System.out.println("All Docs:" + allDocs);
    
    assertEquals(3, allDocs);
    IOUtils.close(collectionClients);

  }