solr transaction commit

Keywords: solr

In the process of using solr, most of the operations are query operations, few transactions are used, and only commit is used when adding or deleting.

solr's commit can be divided into three types:

  1. Manually call commit
  2. Automatic submission
  3. Delayed submission

After each manual operation, the efficiency of calling commit is the lowest, but it doesn't matter. After all, add and delete operations are few.

This article only studies manual call commit

Through testing, solrClient's commit() method can help us complete the transaction.

1. New data test:

In the following code, test(), add two new bean s, 1 and 2 respectively. Before commit, 1 and 2 are not added to solr index. After commit, 1 and 2 can be queried in index library

2. Delete data test:

In the following code, test1() executes the delete statement first. If there is no commit, 1 in the solr library still exists. After commit, 1 in the index library can be queried to become 4

@Service
public class TestService {
    @Autowired
    private SolrClient solrClient;

    public void test(){
        SearchObject searchObject = new SearchObject();
        searchObject.setId("1");
        searchObject.setSearch_id("1");
        searchObject.setSearch_name("1");
        try {
            solrClient.addBean(searchObject);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        SearchObject searchObject1 = new SearchObject();
        searchObject1.setId("2");
        searchObject1.setSearch_id("2");
        searchObject1.setSearch_name("2");
        try {
            solrClient.addBean(searchObject1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

        try {
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public void test2(){
        String query = "search_id:1";
        try {
            solrClient.deleteByQuery(query);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        SearchObject searchObject1 = new SearchObject();
        searchObject1.setId("1");
        searchObject1.setSearch_id("4");
        searchObject1.setSearch_name("4");
        try {
            solrClient.addBean(searchObject1);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

        try {
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Posted by vineld on Sat, 30 Nov 2019 22:39:52 -0800