portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jf...@apache.org
Subject cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/search/lucene LuceneSearchService.java
Date Tue, 17 Feb 2004 03:59:56 GMT
jford       2004/02/16 19:59:56

  Modified:    src/java/org/apache/jetspeed/services/search/lucene
                        LuceneSearchService.java
  Log:
  Updated service implementation with field/keyword multimaps
  Changed to MultifieldQueryParser
  Finer grained exception handling and logging
  Enhanced search results with more data
  
  Revision  Changes    Path
  1.6       +242 -68   jakarta-jetspeed/src/java/org/apache/jetspeed/services/search/lucene/LuceneSearchService.java
  
  Index: LuceneSearchService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/search/lucene/LuceneSearchService.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- LuceneSearchService.java	23 Jul 2003 19:50:23 -0000	1.5
  +++ LuceneSearchService.java	17 Feb 2004 03:59:56 -0000	1.6
  @@ -62,8 +62,12 @@
   import java.util.Collection;
   import java.util.ArrayList;
   import java.util.Iterator;
  +import java.util.Map;
  +import java.util.Set;
   
   // Jetspeed imports
  +import org.apache.commons.collections.MultiHashMap;
  +import org.apache.commons.collections.MultiMap;
   import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
   import org.apache.jetspeed.services.logging.JetspeedLogger;
   import org.apache.jetspeed.services.search.HandlerFactory;
  @@ -88,6 +92,8 @@
   import org.apache.lucene.index.Term;
   import org.apache.lucene.index.IndexWriter;
   import org.apache.lucene.index.IndexReader;
  +import org.apache.lucene.queryParser.MultiFieldQueryParser;
  +import org.apache.lucene.queryParser.ParseException;
   import org.apache.lucene.queryParser.QueryParser;
   import org.apache.lucene.search.IndexSearcher;
   import org.apache.lucene.search.Hits;
  @@ -109,6 +115,9 @@
        */    
       private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(LuceneSearchService.class.getName());
       
  +    private static final int KEYWORD = 0;
  +    private static final int TEXT = 1;
  +    
       private static final String CONFIG_DIRECTORY = "directory";
       private File rootDir = null;
       private String indexRoot = null;
  @@ -248,16 +257,44 @@
       {
           Searcher searcher = null;
           Hits hits = null;
  +        
           try
           {
               searcher = new IndexSearcher(rootDir.getPath());
  -            Analyzer analyzer = new StandardAnalyzer();
  -            Query query = QueryParser.parse(searchString, ParsedObject.FIELDNAME_CONTENT,
analyzer);
  +        }
  +        catch (IOException e)
  +        {
  +            logger.error("Failed to create index search using path " + rootDir.getPath());
  +            return null;
  +        }
  +        
  +        Analyzer analyzer = new StandardAnalyzer();
  +        
  +        String[] searchFields = {ParsedObject.FIELDNAME_CONTENT, ParsedObject.FIELDNAME_DESCRIPTION,
ParsedObject.FIELDNAME_FIELDS,
  +                           ParsedObject.FIELDNAME_KEY, ParsedObject.FIELDNAME_KEYWORDS,
ParsedObject.FIELDNAME_LANGUAGE,
  +                           ParsedObject.FIELDNAME_SCORE, ParsedObject.FIELDNAME_TITLE,
ParsedObject.FIELDNAME_TYPE,
  +                           ParsedObject.FIELDNAME_URL, ParsedObject.FIELDNAME_CLASSNAME};
  +                            
  +        Query query= null;
  +        try
  +        {
  +            query = MultiFieldQueryParser.parse(searchString, searchFields, analyzer);
  +//          Query query = QueryParser.parse(searchString, ParsedObject.FIELDNAME_CONTENT,
analyzer);
  +        }
  +        catch (ParseException e)
  +        {
  +            logger.info("Failed to parse query " + searchString);
  +            return null;
  +        }
  +        
  +        try
  +        {
               hits = searcher.search(query);
           }
  -        catch (Exception e)
  +        catch (IOException e)
           {
  -            logger.error("Exception", e);
  +           logger.error("Error while peforming search.", e);
  +           return null;
           }
   
           // Copy hits to the result list
  @@ -270,6 +307,8 @@
               try
               {
                   doc = hits.doc(counter);
  +                addFieldsToParsedObject(doc, result);
  +                
                   result.setScore(hits.score(counter));
                   result.setType(doc.getField(ParsedObject.FIELDNAME_TYPE).stringValue());
                   result.setKey(doc.getField(ParsedObject.FIELDNAME_KEY).stringValue());
  @@ -279,14 +318,14 @@
                   if (url != null)
                   {
                       result.setURL(new URL(url.stringValue()));
  -                }                
  +                }
  +                
                   results.add(counter, result);
               }
               catch (Exception ioe)
               {
                   logger.error("Exception", ioe);
               }
  -            result = null;
           }
   
           if (searcher != null)
  @@ -302,6 +341,56 @@
           }
           return results;
       }
  +    
  +    private void addFieldsToParsedObject(Document doc, ParsedObject o)
  +    {
  +        try
  +        {
  +            MultiMap keywords = new MultiHashMap();
  +            MultiMap multiFields = new MultiHashMap();
  +            
  +            Field classNameField = doc.getField(ParsedObject.FIELDNAME_CLASSNAME);
  +            if(classNameField != null)
  +            {
  +                String className = classNameField.stringValue();
  +                ObjectHandler handler = HandlerFactory.getHandler(className);
  +                
  +                Set fields = handler.getFields();
  +                Iterator fieldIter = fields.iterator();
  +                while(fieldIter.hasNext())
  +                {
  +                    String fieldName = (String)fieldIter.next();
  +                    Field[] docFields = doc.getFields(fieldName);
  +                    if(fields != null)
  +                    {
  +                        for(int i=0; i<docFields.length; i++)
  +                        {
  +                            Field field = docFields[i];
  +                            if(field != null)
  +                            {
  +                                String value = field.stringValue();
  +                                if(field.isTokenized())
  +                                {
  +                                    multiFields.put(fieldName, value);
  +                                }
  +                                else //it's a keyword
  +                                {
  +                                    keywords.put(fieldName, value);
  +                                }
  +                            }
  +                        }
  +                    }
  +                }
  +            }
  +            
  +            o.setMultiKeywords(keywords);
  +            o.setMultiFields(multiFields);
  +        }
  +        catch(Exception e)
  +        {
  +            logger.error("Error trying to add fields to parsed object.", e);
  +        }
  +    }
   
       /**
        * 
  @@ -320,7 +409,7 @@
        */
       public boolean add(Object o)
       {
  -        Collection c = new ArrayList();
  +        Collection c = new ArrayList(1);
           c.add(o);
   
           return add(c);
  @@ -336,82 +425,167 @@
       {
           boolean result = false;
   
  -        try 
  +        IndexWriter indexWriter;
  +        try
           {
  -            IndexWriter indexWriter = new IndexWriter(rootDir, new StandardAnalyzer(),
false);
  +            indexWriter = new IndexWriter(rootDir, new StandardAnalyzer(), false);
  +        }
  +        catch (IOException e)
  +        {
  +            logger.error("Error while creating index writer. Skipping add...", e);
  +            return result;
  +        }
   
  -            Iterator it = c.iterator();
  -            while (it.hasNext()) 
  +        Iterator it = c.iterator();
  +        while (it.hasNext()) 
  +        {
  +            Object o = it.next();
  +            // Look up appropriate handler
  +            ObjectHandler handler = null;
  +            try
               {
  -                Object o = it.next();
  -                // Look up appropriate handler
  -                ObjectHandler handler = HandlerFactory.getHandler(o);
  +                handler = HandlerFactory.getHandler(o);
  +            }
  +            catch (Exception e)
  +            {
  +                logger.error("Failed to create hanlder for object " + o.getClass().getName());
  +                continue;
  +            }
   
  -                // Parse the object
  -                ParsedObject parsedObject = handler.parseObject(o);
  +            // Parse the object
  +            ParsedObject parsedObject = handler.parseObject(o);
   
  -                // Create document
  -                Document doc = new Document();
  +            // Create document
  +            Document doc = new Document();
   
  -                // Populate document from the parsed object
  -                if (parsedObject.getKey() != null)
  -                {
  -                    doc.add(Field.Keyword(ParsedObject.FIELDNAME_KEY, parsedObject.getKey()));
  -                }
  -                if (parsedObject.getType() != null)
  -                {
  -                    doc.add(Field.Text(ParsedObject.FIELDNAME_TYPE, parsedObject.getType()));
  -                }
  -                if (parsedObject.getTitle() != null)
  -                {
  -                    doc.add(Field.Text(ParsedObject.FIELDNAME_TITLE, parsedObject.getTitle()));
  -                }
  -                if (parsedObject.getDescription() != null)
  -                {
  -                    doc.add(Field.Text(ParsedObject.FIELDNAME_DESCRIPTION, parsedObject.getDescription()));
  -                }
  -                if (parsedObject.getContent() != null)
  -                {
  -                    doc.add(Field.Text(ParsedObject.FIELDNAME_CONTENT, parsedObject.getContent()));
  -                }
  -                if (parsedObject.getLanguage() != null)
  -                {
  -                    doc.add(Field.Text(ParsedObject.FIELDNAME_LANGUAGE, parsedObject.getLanguage()));
  
  -                }
  -                if (parsedObject.getURL() != null)
  -                {
  -                    doc.add(Field.Text(ParsedObject.FIELDNAME_URL, parsedObject.getURL().toString()));
  -                }
  +            // Populate document from the parsed object
  +            if (parsedObject.getKey() != null)
  +            {
  +                doc.add(Field.Keyword(ParsedObject.FIELDNAME_KEY, parsedObject.getKey()));
  +            }
  +            if (parsedObject.getType() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_TYPE, parsedObject.getType()));
  +            }
  +            if (parsedObject.getTitle() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_TITLE, parsedObject.getTitle()));
  +            }
  +            if (parsedObject.getDescription() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_DESCRIPTION, parsedObject.getDescription()));
  +            }
  +            if (parsedObject.getContent() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_CONTENT, parsedObject.getContent()));
  +            }
  +            if (parsedObject.getLanguage() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_LANGUAGE, parsedObject.getLanguage()));
  
  +            }
  +            if (parsedObject.getURL() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_URL, parsedObject.getURL().toString()));
  +            }
  +            if(parsedObject.getClassName() != null)
  +            {
  +                doc.add(Field.Text(ParsedObject.FIELDNAME_CLASSNAME, parsedObject.getClassName()));
  +            }
   
  -                // TODO: How to handle keywords and fields
  -                /*String[] keywords = parsedObject.getKeywords();
  -                if (keywords != null)
  -                {
  -                    for (int i = 0; i < keywords.length; i++)
  -                    {
  -                        doc.add(Field.Keyword();
  -                    }
  -                } */
  +            MultiMap multiKeywords = parsedObject.getMultiKeywords();
  +            addFieldsToDocument(doc, multiKeywords, KEYWORD);
  +            
  +            MultiMap multiFields = parsedObject.getMultiFields();
  +            addFieldsToDocument(doc, multiFields, TEXT);
  +            
  +            Map fields = parsedObject.getFields();
  +            addFieldsToDocument(doc, fields, TEXT);
   
  -                // Add the document to search index
  +            // Add the document to search index
  +            try
  +            {
                   indexWriter.addDocument(doc);
  -                logger.debug("Index Document Count = " + indexWriter.docCount());
  -                logger.info("Added '" + parsedObject.getTitle() + "' to index");
  -                result = true;
               }
  +            catch (IOException e)
  +            {
  +               logger.error("Error adding document to index.", e);
  +            }
  +            logger.debug("Index Document Count = " + indexWriter.docCount());
  +            logger.info("Added '" + parsedObject.getTitle() + "' to index");
  +            result = true;
  +        }
   
  +        try
  +        {
               indexWriter.optimize();
  -            indexWriter.close();
  -
           }
  -        catch (Exception e)
  +        catch (IOException e)
           {
  -            logger.error("Exception", e);
  -            result = false;
  +            logger.error("Error while trying to optimize index.");
  +        }
  +        finally
  +        {
  +            try
  +            {
  +                indexWriter.close();
  +            }
  +            catch (IOException e)
  +            {
  +               logger.error("Error while closing index writer.", e);
  +            }
           }
   
           return result;
       }
  +    
  +    private void addFieldsToDocument(Document doc, Map fields, int type)
  +    {
  +        if(fields != null)
  +        {
  +            Iterator keyIter = fields.keySet().iterator();
  +            while(keyIter.hasNext())
  +            {
  +                Object key = keyIter.next();
  +                if(key != null)
  +                {
  +                    Object values = fields.get(key);
  +                    if(values != null)
  +                    {
  +                        if(values instanceof Collection)
  +                        {
  +                            Iterator valueIter = ((Collection)values).iterator();
  +                            while(valueIter.hasNext())
  +                            {
  +                                Object value = valueIter.next();
  +                                if(value != null)
  +                                {
  +                                    if(type == TEXT)
  +                                    {
  +                                        doc.add(Field.Text(key.toString(), value.toString()));
  +                                    }
  +                                    else
  +                                    {
  +                                        doc.add(Field.Keyword(key.toString(), value.toString()));
  +                                    }
  +                                }
  +                            }
  +                        }
  +                        else
  +                        {
  +                            if(type == TEXT)
  +                            {
  +                                doc.add(Field.Text(key.toString(), values.toString()));
  +                            }
  +                            else
  +                            {
  +                                doc.add(Field.Keyword(key.toString(), values.toString()));
  +                            }
  +                        }
  +                    }
  +                }
  +            } 
  +        }
  +    }
   
       /**
        * 
  @@ -421,7 +595,7 @@
        */
       public boolean remove(Object o)
       {
  -        Collection c = new ArrayList();
  +        Collection c = new ArrayList(1);
           c.add(o);
   
           return remove(c);
  @@ -489,7 +663,7 @@
        */
       public boolean update(Object o)
       {
  -        Collection c = new ArrayList();
  +        Collection c = new ArrayList(1);
           c.add(o);
   
           return update(c);
  
  
  

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


Mime
View raw message