tags:

views:

135

answers:

2

I don't understand , what are the segments in Lucene ? What are the benefits of Lucene's segments?

Thanks .

+2  A: 

A segment is very simply a section of the index. The idea is that you can add documents to the index that's currently being served by creating a new segment with only new documents in it. This way, you don't have to go to the expensive trouble of rebuilding your entire index frequently in order to add new documents to the index.

sblom
+1  A: 

The Lucene index is splitted in smaller chunks called segments. Each segment is an index in itself. Lucene search in all of them in sequence.

A new segment is created when a new writer is opened and when a writer commits or is closed.

The advantages of using this system is that you never have to modify the files of a segment once it is created. When you are adding new documents in your index, they are added to the next segment. Previous segments are never modified. Deleting a document is done by simply indicating in a file which document of a segment is deleted, but physically, the document always stays in the segment. Updating a document in Lucene don't really exists. What happens is that the previous version of the document is marked as deleted in its original segment and the new version of the document is added to the current segment. This minimized the chances of corrupting an index by constantly having to modify it's content when there is changes. It also allows for easy backup and synchronization of the index across different machines.

However, at some point, Lucene may decide to merge some segments. This operation can also be triggered with an optimize.

Pascal Dimassimo