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

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.

主站蜘蛛池模板: 英吉沙县| 宁乡县| 孝昌县| 东乌| 盐源县| 岳池县| 潜山县| 桦南县| 巴青县| 宁阳县| 鄂托克前旗| 昔阳县| 马龙县| 崇信县| 金坛市| 平武县| 屏山县| 岢岚县| 黄浦区| 焉耆| 垣曲县| 监利县| 潼南县| 安顺市| 永宁县| 鄂伦春自治旗| 息烽县| 康马县| 托克逊县| 精河县| 阿克| 沙田区| 朝阳县| 安义县| 神农架林区| 财经| 哈巴河县| 临高县| 张家港市| 大化| 广水市|