lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephane.Gi...@apis-ca.fr
Subject do a simple search
Date Wed, 05 Jan 2005 08:41:42 GMT
hello

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.

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

thanks
        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;
        }
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message