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 6786579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:15:45+00:00 2026-05-26T17:15:45+00:00

I am aware that the Lucene documentation says Note: The NOT operator cannot be

  • 0

I am aware that the Lucene documentation says

Note: The NOT operator cannot be used with just one term. For example,
the following search will return no results:

NOT “jakarta apache”

However, I would like to be able to form a query that returns all documents NOT containing a term. I have looked into stringing together a MatchAllDocsQuery and a TermQuery into a BooleanQuery, but I cannot seem to find the right combination.

If I index the following two documents

Doc0: content:The quick brown fox jumps over the lazy dog.
Doc1: (empty string)

The query *:* -content:fox returns both documents when I just want one document.

The RegexQuery content:^((?!fox).)*$ suggested by this StackOverflow answer returns one document but it does not seem to be working correctly because content:^((?!foo).)*$ returns one document as well when I expect it to return two documents.

I am aware of the performance implications of what I want to do. The query will only be run on a few documents so I am not too worried about performance.

Is there a way to write a Lucene query to get what I want?

  • 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-05-26T17:15:46+00:00Added an answer on May 26, 2026 at 5:15 pm

    You can use match everything and exclude the term –

    IndexSearcher searcher = new IndexSearcher("path_to_index");
    MatchAllDocsQuery everyDocClause = new MatchAllDocsQuery();
    TermQuery termClause = new TermQuery(new Term("text", "exclude_term"));
    BooleanQuery query = new BooleanQuery();
    query.add(everyDocClause, BooleanClause.Occur.MUST);
    query.add(termClause, BooleanClause.Occur.MUST_NOT);
    Hits hits = searcher.search(query);  
    

    Else, have a dummy field which some fixed value and use query

    +dummy_field:dummy_value -exclude_term
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that raw_input cannot be used in twisted. However here is my
I'm aware that I cannot have new ReflectionClass(static) in PHP (5.3) as I just
I'm aware that Python 3 fixes a lot of UTF issues, I am not
I am aware that MySQL and PostgreSQL[1] do not have that concept, so I
I'm aware that an ArrayList is probably not the way to go with this
I am aware that XDebug's delta should not be considered accurate. However, when tracing
I am aware that Action Script does not provide multithreading so when writing flex
I'm working with Lucene 3.2. How can I use a TokenFilter that doesn't just
I'm interested in working with Lucene from a Cocoa application. I'm aware that there
I am aware that views should not have code in them but in a

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.