concept index in category lucene

This is an excerpt from Manning's book Lucene in Action, Second Edition.
% ant Indexer Index *.txt files in a directory into a Lucene index. Use the Searcher target to search this index. Indexer is covered in the "Meet Lucene" chapter. Press return to continue... Directory for new Lucene index: [indexes/MeetLucene] Directory with .txt files to index: [src/lia/meetlucene/data] Overwrite indexes/MeetLucene? (y, n) y Running lia.meetlucene.Indexer... Indexing /Users/mike/lia2e/src/lia/meetlucene/data/apache1.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/apache1.1.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/apache2.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/cpl1.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/epl1.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/freebsd.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/gpl1.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/gpl2.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/gpl3.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/lgpl2.1.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/lgpl3.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/lpgl2.0.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/mit.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/mozilla1.1.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/ mozilla_eula_firefox3.txt Indexing /Users/mike/lia2e/src/lia/meetlucene/data/ mozilla_eula_thunderbird2.txt Indexing 16 files took 757 milliseconds BUILD SUCCESSFUL
Searching in Lucene is as fast and simple as indexing; the power of this functionality is astonishing, as chapters 3, 5, and 6 will show you. For now, let’s look at Searcher, a command-line program that we’ll use to search the index created by Indexer. Keep in mind that our Searcher serves the purpose of demonstrating the use of Lucene’s search API. Your search application could also take the form of a web or desktop application with a GUI, a web application, and so on.
In the previous section, we indexed a directory of text files. The index in this example resides in a directory of its own on the file system. We instructed Indexer to create a Lucene index in the indexes/MeetLucene directory, relative to the directory from which we invoked Indexer. As you saw in listing 1.1, this index contains the indexed contents of each file, along with the absolute path. Now we need to use Lucene to search that index in order to find files that contain a specific piece of text. For instance, we may want to find all files that contain the keyword patent or redistribute, or we may want to find files that include the phrase modified version. Let’s do some searching now.
Unlike a database, Lucene has no notion of a fixed global schema. In other words, each document you add to the index is a blank slate and can be completely different from the document before it: it can have whatever fields you want, with any indexing and storing and term vector options. It need not have the same fields as the previous document you added. It can even have the same fields, with different options, than in other documents.
This feature is quite powerful: it allows you to take an iterative approach to building your index. You can jump right in and index documents without having to predesign the schema. If you change your mind about your fields, start adding additional fields later on and then go back and reindex previously added documents, or rebuild the index.
Lucene’s flexible schema also means a single index can hold documents that represent different entities. For instance, you could have documents that represent retail products with fields such as name and price, and documents that represent people with fields such as name, age, and gender. You could also include unsearchable “meta” documents, which simply hold metadata about the index or your application (such as what time the index was last updated or which product catalog was indexed) but are never included in search results.
Figure 2.1. Indexing with Lucene breaks down into three main operations: extracting text from source documents, analyzing it, and saving it to the index.
![]()
During indexing, the text is first extracted from the original content and used to create an instance of Document, containing Field instances to hold the content. The text in the fields is then analyzed to produce a stream of tokens. Finally, those tokens are added to the index in a segmented architecture. Let’s talk about text extraction first.
A new index commit is created whenever you invoke one of IndexWriter’s commit methods. There are two such methods: commit() creates a new index commit, and commit(Map<String,String> commitUserData) records the provided string map as opaque metadata into the commit for later retrieval. Closing the writer also calls commit(). Note that a newly opened or reopened IndexReader or IndexSearcher will only see the index as of the last commit, and all changes made by IndexWriter in between calls to commit are invisible to readers. The one exception to this is the near-real-time search functionality, covered in section 2.8, which is able to search changes made with the IndexWriter without first committing those changes to disk.
Note that commit can be a costly operation, and doing so frequently will slow down your indexing throughput. If for some reason you decide that you want to discard all changes, you can call rollback() to remove all changes in the current IndexWriter session since the last commit to the index. Here are the steps IndexWriter takes during commit: