lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jason St. Louis" <>
Subject Re: Question on how to build a query
Date Sat, 19 Jun 2004 05:51:06 GMT
Well, I seem to have gotten something to work.  Maybe someone could just 
  comment on my approach.

I wrote my indexer so that it added each field without tokenizing it:

Field fnameField = new Field("fname", fname.toLowerCase(), true, true, 
Field lnameField = new Field("lname", lname.toLowerCase(), true, true, 
Field cityField = new Field("city", position.toLowerCase(), true, true, 

By the way, if this is the case, is the indexer even using the analyzer 
that I pass to it?

Then in my search code I create the firstname query as a WildcardQuery 
if the first name is provided (adding a * to the end if it's not already 

Term fnameTerm = null;
Query fnameQuery = null;
if( fnameIn.length() > 0)
	if( !fnameIn.endsWith("*") )
		fnameIn += "*";
	fnameTerm = new Term("fname", fnameIn);
	fnameQuery = new WildcardQuery(fnameTerm);

I then create my lastname query as either a WildcardQuery or a term 
query depending on whether it contains a *:

Term lnameTerm = new Term("lname", lnameIn);
Query lnameQuery = null;
if( lnameIn.indexOf("*") != -1 )
	lnameQuery = new WildcardQuery(lnameTerm);
	lnameQuery = new TermQuery(lnameTerm);

Lastly, I create the city query as a TermQuery.

Finally, I add the 3 queries to a booleanQuery, not adding the first 
name query if it is null (this means a first name was not provided) and 
making lastname and city required:

if(fnameQuery != null)
	overallQuery.add(fnameQuery, true, false);
overallQuery.add(lnameQuery, true, false);
overallQuery.add(positionQuery, true, false);

I then search my index and it appears to work. I haven't tested it 
extensively yet, though.

Does this seem like a reasonable way to approach this problem, or am I 
missing something that's going to bite me in the you-know-what?


Jason St. Louis wrote:
> Hi everyone.  I'm wondering if someone could help me out.
> I have created an index of a database of person records where I have 
> created documents with the following fields:
> database primary_key (stored, not indexed)
> first name (indexed)
> last name (indexed)
> city (indexed)
> I used SimpleAnalyzer when creating the index.
> I am providing a web based form to search this index.  The form has 3 
> fields for first name, last name and city (city is a drop down list).
> I want to take the users input and from these 3 fields and build a query 
> such that:
> A)last name is mandatory and can be wildcarded (I will probably make 
> sure the value begins with at least one letter)
> B)First name can be wildcarded (same as last name, although if it is 
> left blank, I would probably just search the last_name and city and 
> ignore the first name)
> C)city is mandatory and must match exactly
> How would I go about building this query?
> Do I create a wildcard query for first name and last name, a term query 
> for city and then combine them into boolean query where all 3 terms must 
> be matched?  I kind of feel like I'm grasping at straws here.  I think I 
> just need a jumpstart to understand how the Query API works.
> Thanks.
> Jason
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message