tika-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From neerajshah84 <neerajsha...@gmail.com>
Subject How to display search result with highlighted search word using lucene2.9?
Date Thu, 05 Apr 2012 06:01:08 GMT
im using lucene2.9 and i need display search result like
filename,filepath,textContent. 
This text content will highlight the matched string and display in search
like google. 
When click on the search result ,it will go to the exact file and and the
exact line.

i tried to do it but my index size is very huge around 250MB, i think im
doing something wrong in indexing.
Here is my code :-
-----------------------
   InputStream  is = new BufferedInputStream(new FileInputStream(file));
   BufferedReader bufr = new BufferedReader(new InputStreamReader(is));
   String inputLine="",newLine="";
   while((inputLine=bufr.readLine())!=null ){        newLine =
newLine+"\n"+inputLine;     }
   Document doc = new Document();
 doc.add(new
Field("contents",s,Field.Store.COMPRESS,Field.Index.ANALYZED));
 doc.add(new
Field("filepath",file.getCanonicalPath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
 doc.add(new
Field("title",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
 writer.addDocument(doc);
--------------------------------------------------

IndexReader reader = IndexReader.open(FSDirectory.open(new File("file
location")), true);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
Searcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser("contents", analyzer);
Query query = parser.parse(searchString);
Hits hits = searcher.search(query, new Sort("filepath"));
QueryScorer scorer = new QueryScorer(query, "contents");
Highlighter highlighter = new Highlighter(scorer);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
for (int i = 0; i < hits.length(); i++) {
  Document doc = hits.doc(i);
  String storedField = doc.get("contents");
  TokenStream stream = TokenSources.getTokenStream("contents",storedField,
analyzer);
  highlighter.setTextFragmenter(fragmenter);
  String[] fragments = highlighter.getBestFragments(stream,
storedField,20);
  for(int j=0;j<fragments.length;j++){
    System.out.println( fragments[j]) ) ;
  }

}

--
View this message in context: http://lucene.472066.n3.nabble.com/How-to-display-search-result-with-highlighted-search-word-using-lucene2-9-tp3886392p3886392.html
Sent from the Apache Tika - Development mailing list archive at Nabble.com.

Mime
View raw message