You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by hu...@apache.org on 2002/08/14 19:51:07 UTC
cvs commit: jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene SearchUtils.java Engine.java
husted 2002/08/14 10:51:07
Added: scaffold/src/java/org/apache/commons/scaffold/lucene
SearchUtils.java Engine.java
Log:
[SCAFFOLD] New sandbox component
Revision Changes Path
1.1 jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene/SearchUtils.java
Index: SearchUtils.java
===================================================================
package org.apache.commons.scaffold.lucene;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.search.Hits;
// import org.apache.commons.beanutils.BeanUtils; // Struts 1.1
import org.apache.struts.util.BeanUtils; // Struts 1.0.x
import org.apache.commons.scaffold.lang.PopulateException;
/**
* General purpose utility methods related to Hits
*
* @author Craig R. McClanahan
* @author Ted Husted
* @version $Revision: 1.1 $ $Date: 2002/08/14 17:51:07 $
*/
public class SearchUtils {
/**
* Populate the properties of the specified JavaBean from the specified
* Lucene document, based on matching each parameter name against the
* corresponding JavaBeans "property setter" methods in the bean's class.
* Suitable conversion is done for argument types as described under
* <code>setProperties()</code>.
*
* @param bean The JavaBean whose properties are to be set
* @param document The Lucene document whose parameters are to be used
* to populate bean properties
* @exception PopulateException if an exception is thrown while setting
* property values
*/
public static void populate(
Object bean,
Document document)
throws PopulateException {
// Build a list of relevant fields and values
HashMap properties = new HashMap();
// Iterator of field names
Enumeration fields = document.fields();
while (fields.hasMoreElements()) {
Field field = (Field) fields.nextElement();
properties.put(field.name(),field.stringValue());
}
// Set the corresponding properties of our bean
try {
BeanUtils.populate(bean, properties);
} catch (Throwable t) {
throw new PopulateException(t);
}
} // end populate()
/**
* Return a ArrayList of hits by looping and calling populate.
* No assumptions are made about fields in document. Each is
* processed with a separate call to populate. Whatever fields
* in each document that match the target bean will be
* transferred.
*
* @param hits The Hits whose documents are to be used
* to populate bean properties
* @param target An instance of the bean to populate
* @exception PopulateException if an exception is thrown while setting
* property values, populating the bean, or accessing the Hits
*/
public static Collection getCollection(
Object target,
Hits hits)
throws PopulateException {
// Use ArrayList to maintain sequence
ArrayList list = new ArrayList();
// Acquire target class
Class factory = target.getClass();
try {
// Scroll to each document; populate bean, and add to list
for (int i=0; i<hits.length(); ++i) {
Object bean = factory.newInstance();
Document doc = hits.doc(i);
populate(bean,doc);
list.add(bean);
}
} catch (Throwable t) {
throw new PopulateException(t);
}
return ((Collection) list);
} // end getCollection()
} // end SearchUtils
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Scaffold", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
1.1 jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene/Engine.java
Index: Engine.java
===================================================================
package org.apache.commons.scaffold.lucene;
import java.io.IOException;
import java.util.Collection;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.commons.scaffold.lang.ParameterException;
import org.apache.commons.scaffold.lang.ResourceException;
import org.apache.commons.scaffold.lucene.SearchUtils;
/**
* Search engine methods for Lucene.
*
* @author Ted Husted
* @version $Revision: 1.1 $ $Date: 2002/08/14 17:51:07 $
* @todo Make singleton instead of static
*/
public final class Engine {
/**
* Default path for index ["/var/lucene/scaffold"].
* Use the <code>setIndexPath</code> method on startup
* to change.
* Each application needs its own index path.
*/
private static String indexPath;
/**
* Return path for index.
*/
public static String getIndexPath() {
return indexPath;
}
/**
* Set path for index.
* MUST be set at startup before using other methods.
*/
public static void init(String _indexPath) {
indexPath = _indexPath;
}
/**
* Performance optimization to ensure JVM doesn't
* create a new empty string whenever blankNull
* needs one.
*/
private static final String BLANK_STRING = "";
/**
* Convenience method to use an empty string in place
* of a null for indexing.
*
* @param string The string to test for null
* @return A blank String [BLANK_STRING] if string is null
*/
public static final String blankNull(String string) {
if (string==null) return BLANK_STRING;
else return string;
}
/**
* Return default analyzer for application
* A non-English site should use a different analyzer
*
* @return The default analyzer
* @exception Throws ResourceException on IO error
*/
public static final Analyzer getAnalyzer() {
return new StopAnalyzer();
}
/**
* Return default searcher for application.
*
* @exception Throws ResourceException on IO error
* @return default searcher
*/
public static final Searcher getSearcher()
throws ResourceException {
try {
return new IndexSearcher(getIndexPath());
}
catch (IOException e) {
throw new ResourceException(e);
}
} // end getSearcher()
/**
* Return default writer for application.
*
* @param Set to true to create a new index; usually false.
* @return default writer
* @exception Throws ResourceException on IO error
*/
public static final IndexWriter getIndexWriter(boolean create)
throws ResourceException {
try {
return (new IndexWriter(getIndexPath(),
getAnalyzer(),create));
}
catch (IOException e) {
throw new ResourceException(e);
}
} // end getIndexWriter()
/**
* Return default reader for application.
*
* @return default reader
* @exception Throws ResourceException on IO error
*/
public static final IndexReader getIndexReader()
throws ResourceException {
try {
return (IndexReader.open(getIndexPath()));
}
catch (IOException e) {
throw new ResourceException(e);
}
} // end getIndexReader()
/**
* Return hits for model using default searcher.
*
* @return hits for model
* @param query The query we are processing
*/
public static final Hits getHits(Query query)
throws ResourceException {
try {
return getSearcher().search(query);
}
catch (IOException e) {
throw new ResourceException(e);
}
} // end getHits()
/**
* Return hits for value and field
*
* @param value The term to match ("Smith")
* @param field The field to patch ("name")
* @return hits for model
*/
public static final Query getQuery(
String value,
String field) throws ParameterException {
try {
return QueryParser.parse(value,field, getAnalyzer());
}
catch (ParseException e) {
throw new ParameterException(e);
}
} // end getQuery()
/**
* Object for synchronizing deletions to index
*/
private static final Object INDEX_LOCK = new Object();
/**
* Delete record matching term from default index.
* Access to the index is synchronized.
*
* @param term The term matching the entry to delete
* @exception Throws ResourceException on IO error
*/
public static final int deleteTerm(Term term)
throws ResourceException {
int result = 0;
synchronized (INDEX_LOCK) {
IndexReader reader = null;
try {
reader = getIndexReader();
result = reader.delete(term);
}
catch (IOException e) {
throw new ResourceException(e);
}
finally {
try {
if (reader!=null) reader.close();
}
catch (IOException e) {
// do nothing
}
}
}
return result;
} // end deleteTerm()
} // end Engine
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this Collection of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this Collection of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>