Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:55:32+00:00 2026-06-03T23:55:32+00:00

I am trying to write a simple program using Lucene 2.9.4 which searches for

  • 0

I am trying to write a simple program using Lucene 2.9.4 which searches for a phrase query but I am getting 0 hits

public class HelloLucene {

public static void main(String[] args) throws IOException, ParseException{
    // TODO Auto-generated method stub

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
    Directory index = new RAMDirectory();

    IndexWriter w = new IndexWriter(index,analyzer,true,IndexWriter.MaxFieldLength.UNLIMITED);
    addDoc(w, "Lucene in Action");
    addDoc(w, "Lucene for Dummies");
    addDoc(w, "Managing Gigabytes");
    addDoc(w, "The Art of Computer Science");
    w.close();      

    PhraseQuery pq = new PhraseQuery();
    pq.add(new Term("content", "lucene"),0);
    pq.add(new Term("content", "in"),1);
    pq.setSlop(0);

    int hitsPerPage = 10;
    IndexSearcher searcher = new IndexSearcher(index,true);
    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
    searcher.search(pq, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;

    System.out.println("Found " + hits.length + " hits.");
    for(int i=0; i<hits.length; i++){
        int docId = hits[i].doc;
        Document d = searcher.doc(docId);
        System.out.println((i+1)+ "." + d.get("content"));
    }

    searcher.close();


}

public static void addDoc(IndexWriter w, String value)throws IOException{
    Document doc = new Document();
    doc.add(new Field("content", value, Field.Store.YES, Field.Index.NOT_ANALYZED));
    w.addDocument(doc);
}

}

Please tell me what is wrong. I have also tried using QueryParser as following

String querystr ="\"Lucene in Action\"";

    Query q = new QueryParser(Version.LUCENE_29, "content",analyzer).parse(querystr);

But this is also not working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:55:34+00:00Added an answer on June 3, 2026 at 11:55 pm

    There are two issues with the code (and they have nothing to do with your version of Lucene):

    1) the StandardAnalyzer does not index stopwords (like “in”), so the PhraseQuery will never be able to find the phrase “Lucene in”

    2) as mentioned by Xodarap and Shashikant Kore, your call to create a document needs to include Index.ANALYZED, otherwise Lucene does not use the Analyzer on this section of the Document. There’s probably a nifty way to do it with Index.NOT_ANALYZED, but I’m not familiar with it.

    For an easy fix, change your addDoc method to:

    public static void addDoc(IndexWriter w, String value)throws IOException{
        Document doc = new Document();
        doc.add(new Field("content", value, Field.Store.YES, Field.Index.ANALYZED));
        w.addDocument(doc);
    }
    

    and modify your creation of the PhraseQuery to:

        PhraseQuery pq = new PhraseQuery();
        pq.add(new Term("content", "computer"),0);
        pq.add(new Term("content", "science"),1);
        pq.setSlop(0);
    

    This will give you the result below since both “computer” and “science” are not stopwords:

        Found 1 hits.
        1.The Art of Computer Science
    

    If you want to find “Lucene in Action”, you can increase the slop of this PhraseQuery (increasing the ‘gap’ between the two words):

        PhraseQuery pq = new PhraseQuery();
        pq.add(new Term("content", "lucene"),0);
        pq.add(new Term("content", "action"),1);
        pq.setSlop(1);
    

    If you really want to search for the sentence “lucene in”, you will need to select a different analyzer (like the SimpleAnalyzer). In Lucene 2.9, just replace your call to the StandardAnalyzer with:

        SimpleAnalyzer analyzer = new SimpleAnalyzer();
    

    Or, if you’re using version 3.1 or higher, you need to add the version information:

        SimpleAnalyzer analyzer = new SimpleAnalyzer(Version.LUCENE_35);
    

    Here is a helpful post on a similar issue (this will help you get going with PhraseQuery):
    Exact Phrase search using Lucene? — see WhiteFang34’s answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Pascal and I am trying to write a simple program, but
I am trying to write simple program using ONLY for loop ( if then
I'm new to java and trying to write a simple program using JDBC. It
I'm trying to write a simple program to calculate betweeness using brandes_betweenness_centrality from boostlib.
I'm using Visual Studio 2010. I'm trying to write simple Camera class in OpenGL.
I'm trying to write a simple program using WinSnmp in C++. There is very
I'm trying to write a simple C program on Ubuntu using Eclipse CDT (yes,
I trying to use OpenCL using HASKELL. I write a simple program converting a
I'm trying to write a simple program which uses libcurl to perform HTTP POST
I'm trying to write a simple program to encrypt and decrypt files using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.