lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bianca Pereira <>
Subject EnglishAnalyzer vs WhiteSpaceAnalyzer in getting Term Frequency
Date Thu, 07 Aug 2014 11:28:38 GMT

  I am new in the list and I have been working on a problem for some time
already. I would like to know if someone has any idea of how I can solve it.

 Given a term, I want to get the term frequency in a lucene document. When
I use the WhiteSpaceAnalyzer my code works properly but when I use the
EnglishAnalyzer it returns 0 as frequency for any term.

  In order to get the term appearing both as "term" or "term," in the text
the EnglishAnalyzer is the best one to be used (I suppose).

  Any help is more than welcome.

  Best Regards,

  Here is my code:


public class LuceneDescriptionIndexer implements Closeable {

private IndexWriter descWriter;

 public LuceneDescriptionIndexer(Directory luceneDirectory, Analyzer

 throws IOException {

  openIndex(luceneDirectory, analyzer);


 private void openIndex(Directory directory, Analyzer analyzer) throws
IOException {

  IndexWriterConfig descIwc = new IndexWriterConfig(LuceneConfig.
INDEX_VERSION, analyzer);

  descWriter = new IndexWriter(directory, descIwc);


 public void indexDocument(String id, String text) throws IOException {

    IndexableField idField = new StringField("id",id,Field.Store.YES);

     FieldType fieldType = new FieldType();






    Document doc = new Document();


    doc.add(new Field("description", text, fieldType));




public void close() throws IOException {






public class LuceneTermStatistics implements TermKBStatistics {

private IndexReader luceneIndexReader;

private Analyzer analyzer;

private IndexSearcher searcher;

public LuceneTermStatistics(IndexReader reader, Analyzer analyzer) {

  this.luceneIndexReader = reader;

  this.analyzer = analyzer;

  this.searcher = new IndexSearcher(reader);



 * Create an instance of LuceneTermStatistics from the Config options.


public static LuceneTermStatistics configureInstance(String indexPath,
Analyzer analyzer)

  throws IOException {

  FSDirectory index = File(indexPath));

  DirectoryReader indexReader =;

  return new LuceneTermStatistics(indexReader, analyzer);



public int getTermFrequency(String term, String id)

 throws Exception {

   int docId = getDocId(id);

   // Get the vector with the frequency for the term in all documents

  DocsEnum de = MultiFields.getTermDocsEnum(

       luceneIndexReader, MultiFields.getLiveDocs(luceneIndexReader),

       new BytesRef(term));

   // Get the frequency for the document of interest

  if (de != null) {

      int docNo;

      while((docNo = de.nextDoc()) != DocsEnum.NO_MORE_DOCS) {

         if(docNo == docId)

           return de.freq();



  return 0;


private int getDocId(String id) throws IOException {

  BooleanQuery idQuery = new BooleanQuery();

  idQuery.add(new TermQuery(new Term("id", id)), Occur.MUST);

  TopScoreDocCollector collector = TopScoreDocCollector.create(1, false);, collector);

   TopDocs topDocs = collector.topDocs();

  if (topDocs.totalHits == 0)

    return -1;

   return topDocs.scoreDocs[0].doc;



  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message