You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by St...@apis-ca.fr on 2005/01/05 10:56:08 UTC

Réf. : Re: do a simple search

>On Jan 5, 2005, at 3:41 AM, Stephane.Giner@apis-ca.fr wrote:
>> I would like to search a word in differents fields of a document with 
>> an
>> OR operator.
>>
>> My fields are "id", "shortDesc" and "longDesc".
>> In java I want to search a word simultanly in "shortDesc" and 
>> "longDesc"
>> field.
>>
>> for example:
>>
>>         doc1:   id:1
>>                         shortDesc: a foo desc
>>                         longDesc: a doc long desc
>>
>>         doc2:   id:2
>>                 shortDesc:a doc short desc
>>                 longDesc:a foo long desc
>>
>>         doc3:   id:3
>>                 shortDesc:another short desc
>>                 longDesc:another long desc
>>
>> if the search word is "foo" i want to retreive doc1 and doc3.

>You meant doc1 and doc2.
yes (sorry)

>> in my program, fields are stored in fieldName list.
>> associated values are stored in fieldValue.


>What's the question?  The code you show below, at first glance, looks 
>reasonable, or at least close.  What is the value of query.toString()

>                Erik

Sorry but I found the pb.

I have alway another field "type" who is the type of the searched 
document.
I must change the request to made search like this

        type=value AND (shortDesc=value OR longDesc=value)

but I don't know how to do this ?

here is the query with fields values

Field name: type
Field value: product
Field name: shortDesc
Field value: toto
Field name: longDesc
Field value: toto
IndexManager query = type:product shortDesc:toto longDesc:toto


>         private static Hits search(List fieldName, List fieldValue) {
>                 Hits hits = null;
>
>                 int fieldNameSize  = fieldName.size();
>                 int fieldValueSize = fieldValue.size();
>                 if (fieldNameSize != fieldValueSize) {
>                         return null;
>                 }
>
>                 IndexSearcher searcher = getSearcher();
>                 if (searcher != null) {
>                         BooleanQuery query = new BooleanQuery();
>                         //populate the query with all terms
>                         for (int i=0; i<fieldNameSize; i++) {
>                                 String currentFieldName  = (String)
> fieldName.get(i);
>                                 String currentFieldValue = (String)
> fieldValue.get(i);
>
>                                 StringTokenizer tokenizer = new
> StringTokenizer(currentFieldValue);
>                                 while (tokenizer.hasMoreTokens()) {
>                                         String currentToken =
> tokenizer.nextToken();
>                                         Term currentTerm = new
> Term(currentFieldName,currentToken);
>                                         TermQuery termQuery = new
> TermQuery(currentTerm);
>
> 
> query.add(termQuery,false,false);
>                                 }
>                         }
>
>                         //do the search
>                         try {
>                                 //System.out.println("IndexManager 
> query =
> " + query.toString());
>                                 hits = searcher.search(query);
>                         }
>                         catch (IOException ioe) {
>  LogManager.log(LogManager.LOG_ERROR,"Cannot search in index.",ioe);
>                         }
>                         finally {
>                                 try {
>                                         searcher.close();
>                                 }
>                                 catch (IOException ioe) {
>  LogManager.log(LogManager.LOG_WARNING,"Cannot close searcher in search
> method.",ioe);
>                                 }
>                         }
>                 }
>
>                 return hits;
>         }


---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org



Réf. : Re: Réf. : Re: do a simple search

Posted by St...@apis-ca.fr.
OK, thanks




On Wed, 2005-01-05 at 10:56 +0100, Stephane.Giner@apis-ca.fr wrote:
> I have alway another field "type" who is the type of the searched 
> document.
> I must change the request to made search like this
> 
>         type=value AND (shortDesc=value OR longDesc=value)
> 
> but I don't know how to do this ?
> 
> here is the query with fields values
> 
> Field name: type
> Field value: product
> Field name: shortDesc
> Field value: toto
> Field name: longDesc
> Field value: toto
> IndexManager query = type:product shortDesc:toto longDesc:toto

For the type field I suggest using a TermQuery. Is the document type
from a list of defined types? i.e. is it stored as a keyword and hence
doesn't need parsing?

For the other fields I recommend trying out the
DistributingMultiFieldQueryParser class, which isn't in the main distro
yet but can be found here:

http://issues.apache.org/bugzilla/show_bug.cgi?id=32674

It handles all the awkward bits of making sure all fields are searched
correctly. 

Then combine the two query objects in a BooleanQuery.

-- 
Miles Barr <mi...@runtime-collective.com>
Runtime Collective Ltd.

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org



Re: Réf. : Re: do a simple search

Posted by Miles Barr <mi...@runtime-collective.com>.
On Wed, 2005-01-05 at 10:56 +0100, Stephane.Giner@apis-ca.fr wrote:
> I have alway another field "type" who is the type of the searched 
> document.
> I must change the request to made search like this
> 
>         type=value AND (shortDesc=value OR longDesc=value)
> 
> but I don't know how to do this ?
> 
> here is the query with fields values
> 
> Field name: type
> Field value: product
> Field name: shortDesc
> Field value: toto
> Field name: longDesc
> Field value: toto
> IndexManager query = type:product shortDesc:toto longDesc:toto

For the type field I suggest using a TermQuery. Is the document type
from a list of defined types? i.e. is it stored as a keyword and hence
doesn't need parsing?

For the other fields I recommend trying out the
DistributingMultiFieldQueryParser class, which isn't in the main distro
yet but can be found here:

http://issues.apache.org/bugzilla/show_bug.cgi?id=32674

It handles all the awkward bits of making sure all fields are searched
correctly. 

Then combine the two query objects in a BooleanQuery.

-- 
Miles Barr <mi...@runtime-collective.com>
Runtime Collective Ltd.

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Re: Réf. : Re: do a simple search

Posted by Morus Walter <mo...@tanto.de>.
Stephane.Giner@apis-ca.fr writes:
> I must change the request to made search like this
> 
>         type=value AND (shortDesc=value OR longDesc=value)
> 
> but I don't know how to do this ?
> 
create a boolean query for (shortDesc=value OR longDesc=value)
(as you do so far) and create another boolean query adding that boolean
query and the query for type:product.
For the latter use add(<query>, true, false) to make both subqueries
required.

HTH
	Morus

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org