SpringBoot Solr tutorials series of documents deleted

200114-SpringBoot Solr tutorials series of documents deleted

Search tutorial before a start there is no continued, and now re-retrieve, at least, the basic operating position CURD complemented; This part describes how to delete data

I. placement

Before introducing the demo, you need to install solr environment, build SpringBoot project, specific environmental building process does not go into detail, recommended reference documentation

In application.ymlthe configuration file of red, solr specified domain name

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

Then solr, a write some data for us to use delete, you can write by way of the console, can also 190526-SpringBoot advanced version of Solr search of new and modified documents using gestures case add this document

{
  "id":"1",
  "content_id":1,
  "title":"一灰灰blog",
  "content":"这是一灰灰blog的内容",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609540674060288},
{
  "id":"2",
  "content_id":2,
  "title":"一灰灰",
  "content":"这是一灰灰的内容",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609550229733376},
{
  "id":"3",
  "content_id":3,
  "title":"solrTemplate 修改之后!!!",
  "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":"新增一个测试文档",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655609304836734976},
{
  "id":"6",
  "content_id":6,
  "title":"addBatchByBean - 2",
  "content":"新增又一个测试文档",
  "type":1,
  "create_at":1578912072,
  "publish_at":1578912072,
  "_version_":1655684018701598720
}

II. Delete

We still use SolrTemplateto manipulate solr positive excision investigation, it integrates all the basic operations of solr

1. Delete Primary Key

Note that this case is to delete the primary key id, support the bulk deletion, you need solrTemplate.commit("yhh");this line to commit the changes

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

2. Delete Queries

The above remove the primary key for precise deletion, but limited applicability; delete query manner described below, will satisfy the query data are deleted

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

The above provides a simple query, delete content content 新增documents at the beginning, as to when the query using gestures next article on Solr query gesture details

3. Test

Next, test the above two case

First, we provide a method of output for all documents, before and after comparison of the data for 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 is the method call

@Autowired
private SolrTemplate solrTemplate;

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

Output results are as follows, id are removed as the 4,5,6

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

DocDO(id=1, contentId=1, title=一灰灰blog, content=这是一灰灰blog的内容, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=2, contentId=2, title=一灰灰, content=这是一灰灰的内容, 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=新增一个测试文档, type=1, createAt=1578988256, publishAt=1578988256)
DocDO(id=6, contentId=6, title=addBatchByBean - 2, content=新增又一个测试文档, type=1, createAt=1578988256, publishAt=1578988256)

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


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

DocDO(id=1, contentId=1, title=一灰灰blog, content=这是一灰灰blog的内容, type=1, createAt=1578912072, publishAt=1578912072)
DocDO(id=2, contentId=2, title=一灰灰, content=这是一灰灰的内容, 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. Other

0. series Bowen & Source Project

Bowen series

Source Project

1. A gray Blog

Believe everything the book is not as good, above, is purely one of the words, due to limited personal capacity, it is inevitable omissions and mistakes, such as find a bug or have better suggestions are welcome criticism and generous gratitude

Here a gray personal blog, recording all study and work in the blog, welcome to go around

A gray blog

Published 206 original articles · won praise 57 · views 160 000 +

Guess you like

Origin blog.csdn.net/liuyueyi25/article/details/103998951