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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:49:42+00:00 2026-05-12T18:49:42+00:00

I have a Lucene index that contains documents that have a type field, this

  • 0

I have a Lucene index that contains documents that have a “type” field, this field can be one of three values “article”, “forum” or “blog”. I want the user to be able to search within these types (there is a checkbox for each document type)

How do I create a Lucene query dependent on which types the user has selected?

A couple of prerequisites are:

  • If the user doesn’t select one of the types, I want no results from that type.
  • The ordering of the results should not be affected by restricting the type field.

For reference if I were to write this in SQL (for a “blog or forum search”) I’d write:

SELECT * FROM Docs
WHERE [type] in ('blog', 'forum')
  • 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-12T18:49:42+00:00Added an answer on May 12, 2026 at 6:49 pm

    For reference, should anyone else come across this problem, here is my solution:

    IList<string> ALL_TYPES = new[] { "article", "blog", "forum" };
    string q = ...; // The user's search string
    IList<string> includeTypes = ...; // List of types to include
    Query searchQuery = parser.Parse(q);
    Query parentQuery = new BooleanQuery();
    parentQuery.Add(searchQuery, BooleanClause.Occur.SHOULD);
    // Invert the logic, exclude the other types
    foreach (var type in ALL_TYPES.Except(includeTypes))
    {
        query.Add(
            new TermQuery(new Term("type", type)),
            BooleanClause.Occur.MUST_NOT
        );
    }
    searchQuery = parentQuery;
    

    I inverted the logic (i.e. excluded the types the user had not selected), because if you don’t the ordering of the results is lost. I’m not sure why though…! It is a shame as it makes the code less clear / maintainable, but at least it works!

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

Sidebar

Related Questions

I have a simple lucene index, that contains some demo documents: Title, Keywords, H1Tag
I have a Lucene index that has several documents in it. Each document has
Situation I have the following Sitecore Lucene config: New index, type=Sitecore.Search.Index, Sitecore.Kernel Contains two
I have a Solr/Lucene index file of approximately 700 Gb. The documents that I
I have a Lucene.net index where one of the fields receives many entries that
I have some code that deletes and rebuilds a Lucene index if it is
I have a single XML file that I want to index using Lucene.NET. The
I want to have a citystate field in Lucene index which will store various
I have to index a lot documents that contain reference numbers like aaa.bbb.ddd-fff. The
I want to find the top 1000 documents in a Lucene.NET index that match

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.