On line 195 of IndexFiles.java you will see:
doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))));
This line allows for the user to search on file contents. If somebody wishes to display a summary along with the name of the matching file (kind of like Google search results) you need to add a some more lines of code after line 195 of IndexFiles.java as shown below:
FileReader fr = new FileReader("/home/user1/largefile.txt");
Bufferedreader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
String line;
while ( (line = br.readLine()) != null){
sb.append(line);
}
Field contentField = new StringField("content", sb.toString(), Field.Store.YES, Field.Index.ANALYZED);
doc.add(contentField);
But I'm not done yet, I need to use Lucene's Highlighter class and add code after line 184 inSearchFiles.java. More specifically something like:
Document doc = searcher.doc(hits[i].doc);
String text = doc.getField("content");
highlighter = new Highlighter(new QueryScorer());
String summary = highlighter.getBestFragment(analyzer, "content", text);
This code works perfectly and gives me the summary of search results. However, if the files are too big the IndexFiles.java class spits out an OutOfMemeory error while appending to the StringBuilder(). How do I get around this?