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

The following examples show how to use org.apache.solr.client.solrj.impl.HttpSolrClient#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: SolrProductSearch.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
private static Map<String, Object> updateToSolrCoreMultiRemove(DispatchContext dctx, Map<String, Object> context, Collection<String> productIdList, HttpSolrClient client) {
    Map<String, Object> result;
    // NOTE: log this as info because it's the only log line
    try {
        StringBuilder query = new StringBuilder();
        for(String productId : productIdList) {
            query.append(' ');
            query.append(SolrExprUtil.escapeTermFull(productId));
        }
        if (Debug.infoOn()) {
            Debug.logInfo("Solr: updateToSolr: Removing " + productIdList.size() + " products from index: " + makeLogIdList(productIdList), module);
        }
        client.deleteByQuery("productId:(" + StringUtils.join(productIdList, ' ') + ")");
        client.commit();
        result = ServiceUtil.returnSuccess();
        result.put("numRemoved", productIdList.size());
        result.put("numFailures", 0);
    } catch (Exception e) {
        Debug.logError(e, "Solr: updateToSolr: Error removing " + productIdList.size() + " products (" + makeLogIdList(productIdList) + ") from solr index: " + e.getMessage(), module);
        result = ServiceUtil.returnError("Error removing " + productIdList.size() + " products (" + makeLogIdList(productIdList) + ") from solr index: " + e.toString());
    }
    return result;
}
 
Example 2
Source File: SolrExampleJettyTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testUtf8PerfDegradation() throws Exception {
  SolrInputDocument doc = new SolrInputDocument();

  doc.addField("id", "1");
  doc.addField("b_is", IntStream.range(0, 30000).boxed().collect(Collectors.toList()));

  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.add(doc);
  client.commit();
  long start = System.nanoTime();
  QueryResponse rsp = client.query(new SolrQuery("*:*"));
  System.out.println("time taken : " + ((System.nanoTime() - start)) / (1000 * 1000));
  assertEquals(1, rsp.getResults().getNumFound());

}
 
Example 3
Source File: SolrSchemalessExampleTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testArbitraryJsonIndexing() throws Exception  {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  assertNumFound("*:*", 0); // make sure it got in

  // two docs, one with uniqueKey, another without it
  String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
  HttpClient httpClient = client.getHttpClient();
  HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
  post.setHeader("Content-Type", "application/json");
  post.setEntity(new InputStreamEntity(
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), -1));
  HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
  Utils.consumeFully(response.getEntity());
  assertEquals(200, response.getStatusLine().getStatusCode());
  client.commit();
  assertNumFound("*:*", 2);
}
 
Example 4
Source File: SolrProductSearch.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> removeFromSolrCore(DispatchContext dctx, Map<String, Object> context, String productId) {
    Map<String, Object> result;
    // NOTE: log this as info because it's the only log line
    Debug.logInfo("Solr: removeFromSolr: Removing productId '" + productId + "' from index", module);
    try {
        HttpSolrClient client = SolrUtil.getUpdateHttpSolrClient((String) context.get("core"));
        client.deleteByQuery("productId:" + SolrExprUtil.escapeTermFull(productId));
        client.commit();
        result = ServiceUtil.returnSuccess();
    } catch (Exception e) {
        Debug.logError(e, "Solr: removeFromSolr: Error removing product '" + productId + "' from solr index: " + e.getMessage(), module);
        result = ServiceUtil.returnError("Error removing product '" + productId + "' from solr index: " + e.toString());
    }
    return result;
}
 
Example 5
Source File: TestBatchUpdate.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBinaryBean()throws Exception{
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.setRequestWriter(new BinaryRequestWriter());
  client.deleteByQuery("*:*"); // delete everything!
  final int[] counter = new int[1];
  counter[0] = 0;
  client.addBeans(new Iterator<Bean>() {

    @Override
    public boolean hasNext() {
      return counter[0] < numdocs;
    }

    @Override
    public Bean next() {
      Bean bean = new Bean();
      bean.id = "" + (++counter[0]);
      bean.cat = "foocat";
      return bean;
    }

    @Override
    public void remove() {
      //do nothing
    }
  });
  client.commit();
  SolrQuery query = new SolrQuery("*:*");
  QueryResponse response = client.query(query);
  assertEquals(0, response.getStatus());
  assertEquals(numdocs, response.getResults().getNumFound());
}
 
Example 6
Source File: TestBatchUpdate.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void doIt(HttpSolrClient client) throws SolrServerException, IOException {
  final int[] counter = new int[1];
  counter[0] = 0;
  client.add(new Iterator<SolrInputDocument>() {

    @Override
    public boolean hasNext() {
      return counter[0] < numdocs;
    }

    @Override
    public SolrInputDocument next() {
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("id", "" + (++counter[0]));
      doc.addField("cat", "foocat");
      return doc;
    }

    @Override
    public void remove() {
      //do nothing

    }
  });
  client.commit();
  SolrQuery query = new SolrQuery("*:*");
  QueryResponse response = client.query(query);
  assertEquals(0, response.getStatus());
  assertEquals(numdocs, response.getResults().getNumFound());
}
 
Example 7
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@Monster("Only useful to verify the performance of serialization+ deserialization")
// ant -Dtestcase=SolrExampleBinaryTest -Dtests.method=testQueryPerf -Dtests.monster=true test
public void testQueryPerf() throws Exception {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  ArrayList<SolrInputDocument> docs = new ArrayList<>();
  int id = 0;
  docs.add(makeTestDoc("id", id++, "features", "aaa", "manu", "apple", "cat", "a", "inStock", true, "popularity", 12, "price", .017));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "manu", "lg", "cat", "a", "inStock", false, "popularity", 13, "price", 16.04));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "manu", "samsung", "cat", "a", "inStock", true, "popularity", 14, "price", 12.34));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "manu", "lg", "cat", "b", "inStock", false, "popularity", 24, "price", 51.39));
  docs.add(makeTestDoc("id", id++, "features", "aaa", "manu", "nokia", "cat", "b", "inStock", true, "popularity", 28, "price", 131.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "manu", "ztc", "cat", "a", "inStock", false, "popularity", 32));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "manu", "htc", "cat", "a", "inStock", true, "popularity", 31, "price", 131.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "manu", "apple", "cat", "b", "inStock", false, "popularity", 36));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "manu", "lg", "cat", "b", "inStock", true, "popularity", 37, "price", 1.39));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "manu", "ztc", "cat", "b", "inStock", false, "popularity", 38, "price", 47.98));
  docs.add(makeTestDoc("id", id++, "features", "bbb", "manu", "ztc", "cat", "b", "inStock", true, "popularity", -38));
  docs.add(makeTestDoc("id", id++, "cat", "b")); // something not matching all fields
  client.add(docs);
  client.commit();
  //this sets the cache
  QueryResponse rsp = getSolrClient().query(new SolrQuery("*:*").setRows(20));

  RTimer timer = new RTimer();
  int count = 10000;
  log.info("Started perf test....");
  for(int i=0;i< count; i++){
    rsp = getSolrClient().query(new SolrQuery("*:*").setRows(20));
  }

  if (log.isInfoEnabled()) {
    log.info("time taken to execute {} queries is {} ms", count, timer.getTime());
  }

}
 
Example 8
Source File: ContentEditor.java    From jease with GNU General Public License v3.0 5 votes vote down vote up
public void deleteToSolr(String docid) {
	String solrurl = jease.Registry.getParameter(jease.Names.JEASE_SOLR_URL, "");

	HttpSolrClient solr = new HttpSolrClient.Builder(solrurl).build();
	try {
		solr.deleteById(docid);
		solr.commit();
	}catch(Exception d){
		d.printStackTrace();
	}
}
 
Example 9
Source File: ContentEditor.java    From jease with GNU General Public License v3.0 5 votes vote down vote up
public void updateToSolr(String docid) {
	String solrurl = jease.Registry.getParameter(jease.Names.JEASE_SOLR_URL, "");

	HttpSolrClient solr = new HttpSolrClient.Builder(solrurl).build();
	try {
		SolrInputDocument document = new SolrInputDocument();
		document.addField("id",docid);
		Map<String, Object> fieldModifier = new HashMap<>(1);
		fieldModifier.put("set", this.getNode().getFulltext());
		document.addField("text", fieldModifier);

		Map<String, Object> fieldModifier1 = new HashMap<>(1);
		fieldModifier1.put("set", this.getNode().getTitle());
		document.addField("title", fieldModifier1);

		Map<String, Object> fieldModifier2 = new HashMap<>(1);
		fieldModifier2.put("set", this.getNode().getTages());
		document.addField("tags", fieldModifier2);

		Map<String, Object> fieldModifier3 = new HashMap<>(1);
		fieldModifier3.put("set", new Date());
		document.addField("last_modified", fieldModifier3);

		Map<String, Object> fieldModifier4 = new HashMap<>(1);
		fieldModifier4.put("set", month_date.format(new Date()));
		document.addField("date", fieldModifier4);

		Map<String, Object> fieldModifier5 = new HashMap<>(1);
		fieldModifier5.put("set", id.getValue());
		document.addField("jeaseid", fieldModifier5);



		solr.add(document);
		solr.commit();
	} catch (Exception s) {
		s.printStackTrace();
	}
}
 
Example 10
Source File: SolrExampleJettyTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Ignore
public void testUtf8QueryPerf() throws Exception {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  List<SolrInputDocument> docs = new ArrayList<>();
  for (int i = 0; i < 10; i++) {
    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField("id", "" + i);
    doc2.addField("fld1_s", "1 value 1 value 1 value 1 value 1 value 1 value 1 value ");
    doc2.addField("fld2_s", "2 value 2 value 2 value 2 value 2 value 2 value 2 value 2 value 2 value 2 value ");
    doc2.addField("fld3_s", "3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value 3 value ");
    doc2.addField("fld4_s", "4 value 4 value 4 value 4 value 4 value 4 value 4 value 4 value 4 value ");
    doc2.addField("fld5_s", "5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value 5 value ");
    docs.add(doc2);
  }
  client.add(docs);
  client.commit();
  QueryResponse rsp = client.query(new SolrQuery("*:*"));
  assertEquals(10, rsp.getResults().getNumFound());


  client.setParser(new BinaryResponseParser() {
    @Override
    public NamedList<Object> processResponse(InputStream body, String encoding) {
      try {
        IOUtils.skip(body, 1024 * 1000);
      } catch (IOException e) {
        e.printStackTrace();
      }
      return rsp.getResponse();
    }
  });


  runQueries(client, 1000, true);
  /*BinaryResponseWriter.useUtf8CharSeq = false;
  System.out.println("BinaryResponseWriter.useUtf8CharSeq = " + BinaryResponseWriter.useUtf8CharSeq);
  runQueries(client, 10000, false);
  BinaryResponseWriter.useUtf8CharSeq = true;
  System.out.println("BinaryResponseWriter.useUtf8CharSeq = " + BinaryResponseWriter.useUtf8CharSeq);*/
  runQueries(client, 10000, false);
}
 
Example 11
Source File: SolrSchemalessExampleTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testFieldMutating() throws Exception {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  assertNumFound("*:*", 0); // make sure it got in
  // two docs, one with uniqueKey, another without it
  String json = "{\"name one\": \"name\"} " +
      "{\"name  two\" : \"name\"}" +
      "{\"first-second\" : \"name\"}" +
      "{\"x+y\" : \"name\"}" +
      "{\"p%q\" : \"name\"}" +
      "{\"p.q\" : \"name\"}" +
      "{\"a&b\" : \"name\"}"
      ;
  HttpClient httpClient = client.getHttpClient();
  HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
  post.setHeader("Content-Type", "application/json");
  post.setEntity(new InputStreamEntity(
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), -1));
  HttpResponse response = httpClient.execute(post);
  assertEquals(200, response.getStatusLine().getStatusCode());
  client.commit();
  List<String> expected = Arrays.asList(
      "name_one",
      "name__two",
      "first-second",
      "a_b",
      "p_q",
      "p.q",
      "x_y");
  @SuppressWarnings({"rawtypes"})
  HashSet set = new HashSet();
  QueryResponse rsp = assertNumFound("*:*", expected.size());
  for (SolrDocument doc : rsp.getResults()) set.addAll(doc.getFieldNames());
  for (String s : expected) {
    assertTrue(s+" not created "+ rsp ,set.contains(s) );
  }

}
 
Example 12
Source File: TestSolrJErrorHandling.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
int getCount(HttpSolrClient client) throws IOException, SolrServerException {
  client.commit();
  QueryResponse rsp = client.query(params("q", "id:test", "fl", "count_i", "wt", "json"));
  int count = ((Number)rsp.getResults().get(0).get("count_i")).intValue();
  return count;
}
 
Example 13
Source File: CaseController.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public String commit(HttpSolrClient client) throws SolrServerException, IOException {
    client.commit(collection);
    return "Success";
}