Document deletion for the SpringBoot series tutorial Solr

Keywords: Programming solr SpringBoot Spring github

Document deletion for Solr, 200114-SpringBoot series tutorial

The previous Search tutorial did not continue at the beginning, and now it is picked up again, at least to complete the basic operation posture of CURD; this article focuses on how to delete data

<!-- more -->

I. Configuration

Before introducing demo, you need to install solr environment and build SpringBoot project. The specific process of setting up environment is not detailed. Reference documentation is recommended.

Red in application.yml configuration file, specify solr's domain name

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

Then in solr, write some data for us to delete, either by console or by 190526-SpringBoot Advanced Search Solr Documents Add and Modify Postures case additions to this document

{
  "id":"1",
  "content_id":1,
  "title":"One Gray blog",
  "content":"This is gray blog Contents",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609540674060288},
{
  "id":"2",
  "content_id":2,
  "title":"One Gray",
  "content":"This is gray content",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609550229733376},
{
  "id":"3",
  "content_id":3,
  "title":"solrTemplate After modification!!!",
  "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":"Add a new test document",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609304836734976},
{
  "id":"6",
  "content_id":6,
  "title":"addBatchByBean - 2",
  "content":"Add another test document",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655684018701598720
}

II. Delete

We still use SolrTemplate to manipulate the deletion checks of solrs, which integrates the basic operations of solrs

1. Delete by Primary Key

Note that this case is deleted based on the primary key id, supports bulk deletion and requires solrTemplate.commit("yhh"); this line submits changes

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

2. Query Delete

The above deletion is based on the primary key for precise deletion, but has limited applicability; the following describes how query deletion removes all data that meets the query criteria

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

The above provides a simple query condition, deleting content to start with a new document, and specifying the usage posture of the query statement in the next article when introducing Solr's query posture

3. Testing

Next, test the two case s above

First, we provide a way to output all documents to compare 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 the method call

@Autowired
private SolrTemplate solrTemplate;

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

The output is as follows, with IDS 4,5,6 deleted

---------> query all init start <------------

DocDO(id=1, contentId=1, title=One Gray blog, content=This is gray blog Contents, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=2, contentId=2, title=One Gray, content=This is gray content, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=3, contentId=3, title=solrTemplate After modification!!!, 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=Add a new test document, type=1, createAt=1578988256, publishAt=1578988256)
DocDO(id=6, contentId=6, title=addBatchByBean - 2, content=Add another test document, type=1, createAt=1578988256, publishAt=1578988256)

---------> query all init over <------------


---------> query all afterDelete start <------------

DocDO(id=1, contentId=1, title=One Gray blog, content=This is gray blog Contents, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=2, contentId=2, title=One Gray, content=This is gray content, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=3, contentId=3, title=solrTemplate After modification!!!, content=null, type=0, createAt=1578988256, publishAt=1578988256)

---------> query all afterDelete over <------------

II. Other

0.Series Blog & Project Source

Series Blog

Project Source

1.A grey Blog

Unlike letters, the above are purely family statements. Due to limited personal abilities, there are unavoidable omissions and errors. If bug s are found or there are better suggestions, you are welcome to criticize and correct them with gratitude.

Below is a grey personal blog, which records all the blogs in study and work. Welcome to visit it

Posted by rougue on Wed, 15 Jan 2020 18:09:21 -0800