title: index and search log files using Lucene - to function as grep?
body: <p>There are plain txt log files within directory C:\logs. Now we want to index those log files, and then search the index using regex. My code is below. It searched using keyword "Unknown error". But the following code returns "contents: <strong><em>null</em></strong>".</p>
  
  <pre><code> System.out.println("contents: "+ dochit.get("content"));
  </code></pre>
  
  <p>Any comments? </p>
  
  <p>The complete code:</p>
  
  <pre><code> import java.io.BufferedReader;
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileReader;
   import java.io.InputStreamReader;
   import java.io.Reader;
   import java.io.IOException;
   import java.util.Date;
  
   import org.apache.lucene.index.IndexReader;
   import org.apache.lucene.index.IndexWriter;
   import org.apache.lucene.index.IndexWriterConfig;
   import org.apache.lucene.document.Field;
   import org.apache.lucene.document.Document;
   import org.apache.lucene.document.StoredField;
   import org.apache.lucene.document.StringField;
   import org.apache.lucene.document.TextField;
   import org.apache.lucene.analysis.standard.StandardAnalyzer;
   import org.apache.lucene.analysis.Analyzer;
   import org.apache.lucene.search.IndexSearcher;
   import org.apache.lucene.store.Directory;
   import org.apache.lucene.store.FSDirectory;
   import org.apache.lucene.util.Version;
   import org.apache.lucene.search.TopScoreDocCollector;
   import org.apache.lucene.index.DirectoryReader;
   import org.apache.lucene.queryparser.classic.QueryParser;
   import org.apache.lucene.search.Query;
   import org.apache.lucene.search.ScoreDoc;
  
   public class indexOOO {
  
    public static void main(String[] args) throws Exception{
      //fileDir is the directory that contains the text files to be indexed
      File fileDir  = new File("C:\\logs");
  
      Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_45);
      IndexWriterConfig indexwriterconfig = new IndexWriterConfig(Version.LUCENE_45, luceneAnalyzer);
      //indexDir is the directory that hosts Lucene's index files
      Directory indexDir = FSDirectory.open(new File("C:\\logs\\index"));
  
      IndexWriter indexwriter = new IndexWriter(indexDir,indexwriterconfig);
      File[] textFiles  = fileDir.listFiles();
      long startTime = new Date().getTime();
  
      //Add documents to the index
      for(int i = 0; i &lt; textFiles.length; i++){
        if(textFiles[i].isFile() &amp;&amp; (textFiles[i].getName().endsWith(".log") || textFiles[i].getName().endsWith(".txt"))) {
          System.out.println("File " + textFiles[i].getCanonicalPath() 
                 + " is being indexed");
  
          FileReader textReader = new FileReader(textFiles[i]);
          Document document = new Document();
          document.add(new TextField("content",textReader));
          document.add(new TextField("path",textFiles[i].getPath(),Field.Store.YES));
          indexwriter.addDocument(document);
        }
      }
  
      indexwriter.close();
      long endTime = new Date().getTime();
  
      System.out.println("It took " + (endTime - startTime) 
                 + " milliseconds to create an index for the files in the directory "
                 + fileDir.getPath());
  
      //2. search...
      int hitsPerPage=10;
      IndexReader reader = null;
      try{
         reader = DirectoryReader.open(indexDir);
      } catch (IOException e) {
          e.printStackTrace();
      }
      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
  
      //if argument is inputed, use it, otherwise search with the keyword "Unknown error" below 
      String queryString = args.length &gt; 0 ? args[0] : "Unknown error";
      Query query = new QueryParser(Version.LUCENE_45, "content", analyzer).parse(queryString);
      System.out.println("Searching for:" + "content" + "-&gt;" + queryString);
  
      IndexSearcher searcher = new IndexSearcher(reader);
      TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
      searcher.search(query,collector);
      ScoreDoc[] hits = collector.topDocs().scoreDocs; 
  
      // 3. display results
          if (hits.length&gt; 0) {   
              System.out.println("Found: " + hits.length + " results!");  
              for (int i = 0; i &lt; hits.length; i++) {//output
                  int docId = hits[i].doc;
                  Document dochit = searcher.doc(docId);
                   System.out.println("contents: "+ dochit.get("content"));
              }
          } else{
              System.out.println("0 result!"); 
          }  
  
          reader.close();
      }  
   }
  </code></pre>