Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Lucene 4 Cookbook
  • Toc
  • feedback
Lucene 4 Cookbook

Lucene 4 Cookbook

By : Edwood Ng, Vineeth Mohan
3.2 (5)
close
Lucene 4 Cookbook

Lucene 4 Cookbook

3.2 (5)
By: Edwood Ng, Vineeth Mohan

Overview of this book

This book is for software developers who are new to Lucene and who want to explore the more advanced topics to build a search engine. Knowledge of Java is necessary to follow the code samples. You will learn core concepts, best practices, and also advanced features, in order to build an effective search application.
Table of Contents (11 chapters)
close
10
Index

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.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete