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 neeraj shah <ne...@gmail.com> on 2012/03/30 06:42:48 UTC

Can anyone guide me for this...!!!

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, 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]) ) ;
   }

}

On Thu, Mar 29, 2012 at 6:14 PM, Michael McCandless <
lucene@mikemccandless.com> wrote:

> On Wed, Mar 28, 2012 at 2:30 PM, Tim Eck <ti...@gmail.com> wrote:
> > Excuse my ignorance of lucene internals, but is the problem any easier if
> > the requirement is just to allow the addition/removal of stored only
> fields
> > (as opposed to indexed)?
>
> It would substantially simplify the problem... but even this
> simplification is quite a bit of work.
>
> Lucene's segments are write-once, so when some number of documents in
> a given segment are updated, we'd need to write new files against that
> segment (we call this a "stacked segment"), sort of like a patch over
> the original segment.
>
> But Lucene's stored fields writer/reader impls are set up to handle
> this sort of "patching", so some work must be done to correlate docIDs
> in the patched segment against docIDs in the base segment...
>
> > I suspect this wasn't intent of the original question, but a document can
> > certainly be deleted and re-added to the index with a new set of fields.
>
> Ahh, right, I forgot to mention that: fully removing the original
> document, and then fully adding the new document (with all original
> fields, plus the new fields) is always possible... just not "ideal" if
> your update is tiny and your original fields are huge.
>
> Mike McCandless
>
> http://blog.mikemccandless.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>


-- 
With Regards,
Neeraj Kumar Shah
+919819474146