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 Donna L Gresh <gr...@us.ibm.com> on 2007/03/19 14:35:26 UTC

question about getting all terms in a section of the documents

I am a very new user of Lucene, and thus far am amazed at its speed and 
ease of use.
I have a question about something in the FAQ though. I have a need to get 
all
terms in a specific section of the document; I want to create a database 
of term vs
an identifier of the document containing the term. There is a part of the 
FAQ which
answers this directly, and says:
try
{
    TermEnum terms = indexReader.terms(new Term("FIELD-NAME-HERE", ""));
    while ("FIELD-NAME-HERE".equals(enum.term().field()))
    {
        // ... collect enum.term().text() ...

        if (!terms.next())
            break;
    }
}
finally
{
    terms.close();
}

However, this doesn't compile for me; Is "enum" supposed to be "terms"? 
Also, the terms.close()
statement is outside the scope of terms. I changed to the following, is 
this correct and should the
FAQ be changed?

try
                {
                    TermEnum terms = indexReader.terms(new 
Term("FIELD-NAME-HERE", ""));
 
                    while ("FIELD-NAME-HERE".equals( 
terms.term().field()))
                    {
                     // ... collect enum.term().text() ...
                String term = terms.term().text();
                        System.out.println(term);
                     if (!terms.next())
                            break;
                    }
                    terms.close();
                }



Thanks, and I hope this isn't a crazy question-

Re: question about getting all terms in a section of the documents

Posted by Antony Bowesman <ad...@teamware.com>.
Donna L Gresh wrote:
> 
> Also, the terms.close()
> statement is outside the scope of terms. I changed to the following, is 
> this correct and should the
> FAQ be changed?
> 
> try
>                 {
>                     TermEnum terms = indexReader.terms(new 
> Term("FIELD-NAME-HERE", ""));
>  
>                     while ("FIELD-NAME-HERE".equals( 
> terms.term().field()))
>                     {
>                      // ... collect enum.term().text() ...
>                 String term = terms.term().text();
>                         System.out.println(term);
>                      if (!terms.next())
>                             break;
>                     }
>                     terms.close();
>                 }

I assume the original reason for the finally block was to demonstrate that the 
TermEnum must be closed, so perhaps it should be

TermEnum terms = null;
try
{
...
}
finally
{
    if (terms != null)
        terms.close();
}

the same applies to TermDocs.  Maybe others?
Antony





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