stackoverflowuser2010
stackoverflowuser2010

Reputation: 40859

Lucene docFreq returning 0

I'm using Lucene 3.1 to index some documents.

When I use IndexSearcher.search(), I successfully get back results for queries.

However, when I use IndexSearcher.doqFreq(), I get back 0 for a term. Can anyone offer some insight?

Also, why is there both an IndexSearcher.docFreq() and IndexReader.docFreq()? I have tried both, and both give me 0.

Here is my code:

    IndexReader indexReader = IndexReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(indexReader);

    ...

    String seachTermString = "foobar";
    String field = "body";
    Term term = new Term(field, searchTermString);
    int numDocs = searcher.docFreq(term);

and then I get numDocs=0, even though when I use IndexSearcher.search() with the same search term string, I get back hits.

Upvotes: 2

Views: 1644

Answers (4)

gaffcz
gaffcz

Reputation: 3479

Use TermEnum:

Term term = new Term(field, searchTermString);
TermEnum enum = indexReader.terms(term);
int numDocs = enum.docFreq();

And you don't need the IndexSearcher

Upvotes: 1

João Rocha da Silva
João Rocha da Silva

Reputation: 4309

Are you adding your Fields with the Field.TermVector.YES option enabled?

Document doc = new Document();
doc.add(new Field("value", documentContents, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));

Upvotes: 1

Christian
Christian

Reputation: 21

Try converting your term completely to lower case letters.

Upvotes: 2

Shashikant Kore
Shashikant Kore

Reputation: 5042

Create TermQuery from the Term you are creating to get document frequency with search.docFreq(term). Use this TermQuery for searching and check if it yields any results. It should. If this TermQuery doesn't give any results, something is amiss in the query creation in the step 1 of search in the question.

Upvotes: 1

Related Questions