官术网_书友最值得收藏!

  • Lucene 4 Cookbook
  • Edwood Ng Vineeth Mohan
  • 231字
  • 2021-07-16 14:07:48

Deleting documents

We have learned how documents are added to an index. Now, we will see how to delete Documents. Suppose you want to keep your index up to date by deleting documents that are a week old. All of a sudden, the ability to remove documents becomes a very important feature. Let's see how can we do that.

How to do it...

IndexWriter provides the interface to delete documents from an index. It takes either term or query as argument, and will delete all the documents matching these arguments:

  • deleteDocuments(Term)
  • deleteDocuments(Term… terms)
  • deleteDocuments(Query)
  • deleteDocuments(Query… queries)
  • deleteAll( )

Here is a code snippet on how deleteDocuments is called:

  indexWriter.deleteDocuments(new Term("id", "1"));"));
  indexWriter.close();

How it works…

Assuming IndexWriter is already instantiated, this code will trigger IndexWriter to delete all the documents that contain the term id where the value equals 1. Then, we call close to commit changes and close the IndexWriting. Note that this is a match to a Field called id; it's not the same as DocId.

In fact, deletions do not happen at once. They are kept in the memory buffer and later flushed to the directory. The documents are initially marked as deleted on disk so subsequent searches will simply skip the deleted documents; however, to free the memory, you still need to wait. We will see the underlying process in detail in due course.

主站蜘蛛池模板: 中方县| 玉门市| 宁晋县| 漳平市| 济南市| 石阡县| 凭祥市| 宁德市| 肃北| 易门县| 新河县| 毕节市| 新乡县| 宁城县| 泾川县| 太仆寺旗| 贵州省| 神池县| 贵州省| 巴林右旗| 朝阳区| 南漳县| 枞阳县| 璧山县| 南投市| 台前县| 会理县| 阿荣旗| 东海县| 开平市| 庆阳市| 道真| 利辛县| 封丘县| 赫章县| 旬阳县| 高清| 崇义县| 错那县| 旌德县| 双辽市|