terms = fileds.terms(...);
termsEnum = terms.iterator(null);
termsEnum.seekExat(...);
docsAndPositionsEnum docsPosEnum = termsEnum.docsAndPositions(...);

You can get the information in "docsPosEnum".


On Thu, Dec 6, 2012 at 2:28 AM, <vitaly_arte...@mcafee.com> wrote:

> Hi all,
> I am new with Lucene.
> I try to understand how can I find the term position.
>
> I use following code to index documents:
> ...
>                 IndexWriter writer = new IndexWriter(mIndexDir, mIwc);
>                 FileInputStream fis;
>                 fis = new FileInputStream(file);
> Document doc = new Document();
> Field pathField = new StringField("path", file.getPath(), Field.Store.YES);
>                 doc.add(pathField);
> doc.add(new TextField("contents", new BufferedReader(new
> InputStreamReader(fis, "UTF-16LE"))));
> writer.updateDocument(new Term("path", file.getPath()), doc);
> fis.close();
> writer.close();
> ...
>
> To search I use following code:
> ...
> IndexReader reader = DirectoryReader.open( mIndexDir );
> IndexSearcher searcher = new IndexSearcher(reader);
> QueryParser parser = new QueryParser(Version.LUCENE_40, "contents",
> mAnalyzer);
> Query query = parser.parse(aQuery);
>
> TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);
> searcher.search(query, collector);
> ScoreDoc[] hits = collector.topDocs().scoreDocs;
>
> for(int i=0;i<hits.length;++i) {
>                 int docId = hits[i].doc;
>                 Document d = searcher.doc(docId);
> }
> ...
> How can I find positions of my query string in the indexed documents.
>
> Thanks, Vitaly
>
>
>

Reply via email to