200114-SpringBoot series Solr document deletion

The previous search tutorial did not continue at the beginning, and now it is back, at least has completed the basic operation posture; How to delete data

<! — more –>

I. the configuration

Before introducing the demo, we need to install the Solr environment first and build the SpringBoot project project. The specific environment construction process will not be detailed, but the documentation is recommended

  • 190510-SpringBoot Advanced Part Solr Environment Building and Simple Testing

In the application.yml configuration file, red, specifies the domain name of Solr

Spring: data: solr: host: http://127.0.0.1:8983/solr

Then in Solr, write some data, for us to delete the use, can be written through the console way, can also be through 190526-SpringBoot advanced article search Solr document to add and modify the use of posture this document case add

{" id ":" 1 ", "content_id" : 1, "title" : "a dusty blog", "content" : "this is a gray blog content", "type" : 1, "create_at" : 1578912072, "Publish_at ":1578912072, "_version_":1655609540674060288}, {"id":" 1 ", "content_id":2, "title":" 1 ", "Content ":" This is a ash content", "type":1, "create_at":1578912072, "publish_at":1578912072, "_version_":1655609550229733376}, {" id ", "3", "content_id" : 3, "title" : "solrTemplate modification after!!!!!!" , "create_at":1578912072, "publish_at":1578912072, "type":0, "_version_":1655609304941592576}, { "id":"4", "content_id":4, "type":1, "create_at":0, "publish_at":0, "_version_":1655609305022332928}, { "id":"5", "content_id":5, "Title ":" addBatchByBean-1 ", "content":" new test document ", "type":1, "create_at":1578912072, "publish_at":1578912072, "_version_":1655609304836734976}, {"id":"6", "content_id":6, "title":" addBatchByBean2 ", "content":" Add another test document ", "type":1, "create_at":1578912072, "publish_at":1578912072, "_version_":1655684018701598720 }

II. Delete

We still use the SolrTemplate for Solr’s forward delete search, which consolidates Solr’s basic operations

1. Delete by primary key

Note that this case is deleted by primary key ID and supports bulk deletion, requiring solrtemplate.mit (“yhh”); This line to commit the changes

private void deleteById() {
    solrTemplate.deleteByIds("yhh", Arrays.asList("4"));
    solrTemplate.commit("yhh");
}

2. Query delete

The above delete by primary key is suitable for precise delete operation, but the applicability is limited; The following introduces the way of query deletion, will meet the query conditions of the data are deleted

private void deleteByQuery() { SolrDataQuery query = new SimpleQuery(); Query. AddCriteria (Criteria. The where (" content "). The startsWith (" new ")); solrTemplate.delete("yhh", query); solrTemplate.commit("yhh"); }

This provides a simple query condition that deletes a document whose content begins with a new one. The posture of the query statement will be explained in detail in the next article that introduces Solr’s query posture

3. The test

Next, test the two cases above

First, we provide a way to output all documents to compare the data changes before and after deletion

private void printAll(String tag) {
    System.out.println("\n---------> query all " + tag + " start <------------\n");
    List<DocDO> list = solrTemplate.query("yhh", new SimpleQuery("*:*").addSort(Sort.by("content_id").ascending()), DocDO.class)
                    .getContent();
    list.forEach(System.out::println);
    System.out.println("\n---------> query all " + tag + " over <------------\n");
}

Next comes method calls

@Autowired
private SolrTemplate solrTemplate;

public void delete() {
    printAll("init");
    this.deleteById();
    this.deleteByQuery();
    printAll("afterDelete");
}

The output is as follows: the id 4,5,6 has been deleted

---------> query all init start < p DocDO(id=1, contentId=1, title= 1, content= 1, type=1, CreatEat =1578912072, publishAt=1578912072) DocDO(id=2, contentId=2, title= 1, content= 1, type=1) CreatEat =1578912072, publishAt=1578912072) docDo (id=3, contentId=3, title=solrTemplate) , content=null, type=0, createAt=1578988256, publishAt=1578988256) DocDO(id=4, contentId=4, title=null, content=null, Type =1, creatEat =0, publishAt=0) DocDO(id=5, contentId=5, title= addBatchByBean-1, content= new test document, type=1, CreatEat =1578988256, publishAt=1578988256) DocDO(id=6, contentId=6, title= addBatchByBean2, content= new test document, type=1, createAt=1578988256, publishAt=1578988256) ---------> query all init over <------------ ---------> query all afterDelete start <------------ DocDO(id=1, contentId=1, title= 1, content= 1, type=1, createAt=1578912072, type=1, createAt=1578912072, PublishAt =1578912072) DocDO(id=2, contentId=2, title= 1, content= 1, type=1, createAt=1578912072, PublishAt =1578912072) docDo (id=3, contentId=3, title=solrTemplate) , content=null, type=0, createAt=1578988256, publishAt=1578988256) ---------> query all afterDelete over <------------

II. The other

0 series of blog & project source code

Series of blog posts

  • 190526-SpringBoot Advanced Search Solr for new and modified postures
  • 190510-SpringBoot Advanced Part Solr Environment Building and Simple Testing

Program source code

  • Project: https://github.com/liuyueyi/spring-boot-demo
  • Source: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/140-search-solr

1. Grey Blog

Do not trust the book, the above content, purely a family of words, due to personal ability is limited, inevitably there are omissions and mistakes, such as bugs found or have better suggestions, welcome criticism, gratefully

The following a gray personal blog, record all study and work in the blog, welcome everyone to visit

  • A Grey Blog personal Blog https://blog.hhui.top
  • A Grey Blog-Spring feature Blog http://spring.hhui.top