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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:06:45+00:00 2026-05-28T01:06:45+00:00

I have a database with 10 fields, and I need to construct a query

  • 0

I have a database with 10 fields, and I need to construct a query that looks something like the following pseudo code:

theQuery = ((field1 == A) && 
           (field2 == B) && 
           (field3 == C) && 
           (field4 == D) && 
           (field5 == E) && 
           (field6 == F) && 
           (field7 == G) && 
           ((field8 == H) || (field9 == H) || (field10 == H)))

That is to say that I need fields 1-7 to definitely contain the corresponding supplied variable, and I need the variable H to definitely appear in at least one of fields 8-10.

I have been trying to use the MultiFieldQueryParser, but the problem that I have is that the BooleanClauses supplied are MUST, MUST_NOT and SHOULD, and we can set the default operator of the MultiFieldQueryParser to be either AND or OR.

When I try using AND and setting fields 1-7 with MUST and fields 8-10 with SHOULD, the query parser basically ignores fields 8-10 and gives me back anything that contains the specified data in fields 1-7.

I haven’t yet tried setting the default operator to OR, because I’m guessing that the query will return results that contain one or more of the supplied variables in fields 1-10.

For those that wish to see code, my code is as follows:

ArrayList queries = new ArrayList();
ArrayList fields = new ArrayList();
ArrayList flags = new ArrayList();

if(varA != null && !varA.equals(""))
{
    queries.Add(varA);
    fields.Add("field1");
    flags.Add(BooleanClause.Occur.Must);
}
//... The same for 2-7
if(varH != null && !varH.equals(""))
{
    queries.Add(varA);
    queries.Add(varA);
    queries.Add(varA);
    fields.Add("field8");
    fields.Add("field9");
    fields.Add("field10");
    flags.Add(BooleanClause.Occur.Should);
    flags.Add(BooleanClause.Occur.Should);
    flags.Add(BooleanClause.Occur.Should);
}
Query q = MultiFieldQueryParser.parse(VERSION.LUCENE_34, 
                                      queries.toArray(), 
                                      fields.toArray(),
                                      flags.toArray(),
                                      theAnalyzer);

Obviously this is somewhat simplified as the ArrayLists don’t neatly return me arrays of Strings and BooleanClause.Occurs, but you get the idea.

Does anyone know of a way of forming a multifield query, including both boolean ANDs and boolean ORs?

Thanks,
Rik

  • 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-28T01:06:46+00:00Added an answer on May 28, 2026 at 1:06 am

    I don’t really understand your notation, so it’s hard to figure out what the problem is. But just use standard queries:

     BooleanQuery topQuery = new BooleanQuery();
     topQuery.add(new TermQuery(...), BooleanClause.Occur.Must);
     etc.
    

    Or just do it in text and let the parser parse it for you: +field1:A +field2:B ...

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

Sidebar

Related Questions

I have a database full of text fields that look like this: (paragraph of
I have a database that I need to query over and over as fast
I have a database table with a field that I need to read from
I have a database with DateTime fields that are currently stored in local time.
I have a database structure that has a Person table which contains fields such
I have a dropdown box that I construct with PHP. Here is the code:
I have a database table containing fields RACE, ETHNICITY and ETH. I need to
At this present point in time I have some code that does the following:
I have a database table named 'favoritecats' with the following fields: id catName catId
I have a database table with 6 fields that holds information about restaurants: id

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.