lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ian Lea <ian....@gmail.com>
Subject Re: Error of the code
Date Thu, 13 May 2010 13:22:57 GMT
What does vector.size() return?  You don't appear to be doing anything
with the String term in "for ( String term : vector.getTerms() )" -
presumably you intend to.


--
Ian.

On Thu, May 13, 2010 at 1:16 PM, manjula wijewickrema
<manjula53@gmail.com> wrote:
> Dear Ian,
>
> Thanks a lot for your immediate reply. As you have mentioned I replaced the
> lines as follows.
>
>
> IndexReader ir=IndexReader.open(directory);
>
> TermFreqVector vector=ir.getTermFreqVector(0,"fieldname");
>
> Now the error has been vanished and thanks for it. But I can't still see the
> results although I have moved those lines after iwriter.close(). What's the
> reason for this?
>
> sample code after modifications:
> .........................
> ........................
>
> String text = "This is the text to be indexed.";
>
>  doc.add(*new* Field("fieldname", text, Field.Store.*YES*,Field.Index.*
> ANALYZED*,Field.TermVector.*WITH_POSITIONS_OFFSETS*));
>
> iwriter.addDocument(doc);
>
> iwriter.close();
>
> IndexReader ir=IndexReader.open(directory);
>
> TermFreqVector vector=ir.getTermFreqVector(0,"fieldname");
> *
>
> int* size = vector.size();
>
> *for* ( String term : vector.getTerms() )
>
> System.*out*.println( "size = " + size );
>
> IndexSearcher isearcher = *new* IndexSearcher(directory, *true*);
> ..........
> ..........
> I appreciate your kind coperation
> Manjula
> On Thu, May 13, 2010 at 3:45 PM, Ian Lea <ian.lea@gmail.com> wrote:
>
>> You need to replace this:
>>
>> TermFreqVector vector = IndexReader.getTermFreqVector(0, "fieldname" );
>>
>> with
>>
>> IndexReader ir = whatever(...);
>> TermFreqVector vector = ir.getTermFreqVector(0, "fieldname" );
>>
>> And you'll need to move it to after the writer.close() call if you
>> want it to see the doc you've just added.
>>
>>
>>
>> --
>> Ian.
>>
>>
>>
>> On Thu, May 13, 2010 at 11:07 AM, manjula wijewickrema
>> <manjula53@gmail.com> wrote:
>> > Dear All,
>> >
>> > I am trying to get the term frequencies (through TermFreqVector) of a
>> > document (using Lucene 2.9.1). In order to do that I have used the
>> following
>> > code. But there is a compile time error in the code and I can't figure it
>> > out. Could somebody can guide me what's wrong with it.
>> > Compile time error I got:
>> > Cannot make a static reference to the non-static method
>> > getTermFreqVector(int, String) from the type IndexReader.
>> >
>> > Code:
>> >
>> >  *import* org.apache.lucene.analysis.standard.StandardAnalyzer;
>> >
>> > *import* org.apache.lucene.document.Document;
>> > *
>> >
>> > import* org.apache.lucene.document.Field;
>> > *
>> >
>> > import* org.apache.lucene.index.IndexWriter;
>> > *
>> >
>> > import* org.apache.lucene.queryParser.ParseException;
>> > *
>> >
>> > import* org.apache.lucene.queryParser.QueryParser;
>> > *
>> >
>> > import* org.apache.lucene.search.*;
>> > *
>> >
>> > import* org.apache.lucene.store.Directory;
>> > *
>> >
>> > import* org.apache.lucene.store.RAMDirectory;
>> > *
>> >
>> > import* org.apache.lucene.util.Version;
>> >
>> > *
>> >
>> > import* org.apache.lucene.index.IndexReader;
>> > *
>> >
>> > import* org.apache.lucene.index.TermEnum;
>> > *
>> >
>> > import* org.apache.lucene.index.Term;
>> > *
>> >
>> > import* org.apache.lucene.index.TermFreqVector;
>> >
>> > *
>> >
>> > import* java.io.IOException;
>> > *
>> >
>> > public* *class* DemoTest {
>> >
>> > *public* *static* *void* main(String[] args) {
>> >
>> > StandardAnalyzer analyzer = *new*
>> StandardAnalyzer(Version.*LUCENE_CURRENT*
>> > );
>> >
>> > *try* {
>> >
>> > Directory directory = *new* RAMDirectory();
>> >
>> > IndexWriter iwriter = *new* IndexWriter(directory, analyzer,
>> > *true*,*new*IndexWriter.MaxFieldLength(25000));
>> >
>> > Document doc = *new* Document();
>> >
>> > String text = "This is the text to be indexed.";
>> >
>> > doc.add(*new* Field("fieldname", text, Field.Store.*YES*,Field.Index.*
>> > ANALYZED*,Field.TermVector.*WITH_POSITIONS_OFFSETS*));
>> >
>> > iwriter.addDocument(doc);
>> >
>> > TermFreqVector vector = IndexReader.getTermFreqVector(0, "fieldname" );
>> > *
>> >
>> > int* size = vector.size();
>> >
>> > *for* ( String term : vector.getTerms() )
>> >
>> > System.*out*.println( "size = " + size );
>> >
>> > iwriter.close();
>> >
>> > IndexSearcher isearcher = *new* IndexSearcher(directory, *true*);
>> >
>> > QueryParser parser = *new* QueryParser(Version.*LUCENE_CURRENT*,
>> "fieldname",
>> > analyzer);
>> >
>> > Query query = parser.parse("text");
>> >
>> > ScoreDoc[] hits = isearcher.search(query, *null*, 1000).scoreDocs;
>> >
>> > System.*out*.println("hits.length(1) = " + hits.length);
>> >
>> > // Iterate through the results:
>> >
>> > *for* (*int* i = 0; i < hits.length; i++) {
>> >
>> > Document hitDoc = isearcher.doc(hits.doc);
>> >
>> > System.*out*.println("hitDoc.get(\"fieldname\") (This is the text to be
>> > indexed) = " +
>> >
>> > hitDoc.get("fieldname"));
>> >
>> > }
>> >
>> > isearcher.close();
>> >
>> > directory.close();
>> >
>> > } *catch* (Exception ex) {
>> >
>> > ex.printStackTrace();
>> >
>> > }
>> >
>> > }
>> >
>> > }
>> >
>> >
>> >
>> > Thanks in advance
>> >
>> > Manjula
>> >
>>
>>  ---------------------------------------------------------------------
>> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
>> For additional commands, e-mail: java-user-help@lucene.apache.org
>>
>>
>

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


Mime
View raw message