lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephane.Gi...@apis-ca.fr
Subject Réf. : Re: do a simple search
Date Wed, 05 Jan 2005 09:56:08 GMT
>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



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message