Searching the Local Index Files
Now we can search for data in the index files that we just created. Basically, search is done on the "field" data. You can search using any various search semantics supported by the Lucene search engine, and you can perform searches on one particular field or a combination of fields. The following Java code searches the index:
// Creating Searcher object and specifying the path where Indexed files are stored.
Searcher searcher = new IndexSearcher("E://DataFile/IndexFiles");
Analyzer analyzer = new StandardAnalyzer();
// Printing the total number of documents or entries present in the index file.
System.out.println("Total Documents = "+searcher.maxDoc()) ;
// Creating the QueryParser object and specifying the field name on
//which search has to be done.
QueryParser parser = new QueryParser("cs-uri", analyzer);
// Creating the Query object and specifying the text for which search has to be done.
Query query = parser.parse("/blank");
// Below line performs the search on the index file and
Hits hits = searcher.search(query);
// Printing the number of documents or entries that match the search query.
System.out.println("Number of matching documents = "+ hits.length());
// Printing documents (or rows of file) that matched the search criteria.
for (int i = 0; i < hits.length(); i++)
{
Document doc = hits.doc(i);
System.out.println(doc.get("date")+" "+ doc.get("time")+ " "+
doc.get("cs-method")+ " "+ doc.get("cs-uri")+ " "+ doc.get("sc-status")+ " "+ doc.get("time-taken"));
}
In this example, the search is done on the field cs-uri and the text that is searched inside the cs-uri field is /blank. So when the search code is run, all the documents (or rows) for which cs-uri field contains /blank, are shown in the output. The output is as follows:
Total Documents = 11 Number of matching documents = 7 2010-04-21 02:24:01 GET /blank 200 120 2010-04-21 02:24:02 POST /blank 304 56 2010-04-21 02:24:04 GET /blank 304 233 2010-04-21 02:24:04 GET /blank 500 567 2010-04-21 02:24:04 GET /blank 200 897 2010-04-21 02:24:04 POST /blank 200 567 2010-04-21 02:24:05 GET /blank 200 347


