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 Vikas <vi...@unnat-e.com> on 2007/07/09 09:11:57 UTC

UnExpected result for: indexReader.termDocs()

Hi Friends,

Before indexing somthing I want to check whether this term is already exist in index / NOT.[i.e. primary key kind of thing].

When I am trying to get docs with indexReader.termDocs(term); it is not returning expected results.
I put "TermDocs" object in while loop to print document number where I found this term; but it is always printing "0" and going in infinite loop.
Where the term, I am searching is exists in index and can get it though "indexReader.terms(term);"

Here is the code -->

Term term = new Term( SomeDocument.KEY, value);
TermDocs termDocs = indexReader.termDocs(term);
while(termDocs !=null)
{
   System.out.println("Document num:"+termDocs.doc());
   termDocs.next();
}

This is going in infinite loop and printing "Document num:0" always.
Can some one will give me information on this?

With best regards

From
Vikas R. Khengare

Re: UnExpected result for: indexReader.termDocs()

Posted by Erick Erickson <er...@gmail.com>.
Well, first you never test anything that will break the loop. TermDocs.next
()
does not set termdocs == null, but returns false....


I've always found it a bit confusing that calling next() doesn't skiph the
first term, but you want something like this....

        Term term = new Term(field, value);
        TermDocs termDocs = reader.termDocs(term);

        while (termDocs.next()) {
            System.out.println("Document num:" + termDocs.doc());
        }


Best
Erick

On 7/9/07, Vikas <vi...@unnat-e.com> wrote:
>
> Hi Friends,
>
> Before indexing somthing I want to check whether this term is already
> exist in index / NOT.[i.e. primary key kind of thing].
>
> When I am trying to get docs with indexReader.termDocs(term); it is not
> returning expected results.
> I put "TermDocs" object in while loop to print document number where I
> found this term; but it is always printing "0" and going in infinite loop.
> Where the term, I am searching is exists in index and can get it though "
> indexReader.terms(term);"
>
> Here is the code -->
>
> Term term = new Term( SomeDocument.KEY, value);
> TermDocs termDocs = indexReader.termDocs(term);
> while(termDocs !=null)
> {
>    System.out.println("Document num:"+termDocs.doc());
>    termDocs.next();
> }
>
> This is going in infinite loop and printing "Document num:0" always.
> Can some one will give me information on this?
>
> With best regards
>
> From
> Vikas R. Khengare
>