You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by neerajshah84 <ne...@gmail.com> on 2012/03/19 11:27:53 UTC

How to display result like google like display text with search result?

im using lucene2.9 and i need display search result like
filename,filepath,textContent.. this text content highlight the matched
string. 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.
can you guide me.  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-result-like-google-like-display-text-with-search-result-tp3838657p3838657.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org