SPT Object Database API

com.sptci.prevayler
Class SearchSystem

java.lang.Object
  extended by com.sptci.prevayler.StorageSystem
      extended by com.sptci.prevayler.IndexSystem
          extended by com.sptci.prevayler.ConstraintSystem
              extended by com.sptci.prevayler.SearchSystem
All Implemented Interfaces:
AbstractDatabase<PrevalentObject>, DatabaseSystem, Serializable
Direct Known Subclasses:
ObjectGraphSystem

abstract class SearchSystem
extends ConstraintSystem

Abstracts all the full-text search indexing and de-indexing operations for the prevalent system. Indexing is performed using Lucene.

Note:The field values are converted to String using the Object.toString() method to retrieve the content to index. To ensure meaningful indices ensure that the fields annotated return meaningful values.

© Copyright 2008 Sans Pareil Technologies, Inc.

Since:
Release 0.3.0
Version:
$Id: SearchSystem.java 23 2008-11-24 19:49:55Z sptrakesh $
Author:
Rakesh Vidyadharan 2008-11-12

Nested Class Summary
private static class SearchSystem.Closer
          A thread used as a shutdown hook to the JVM to close the writer and other lucene resources.
 
Nested classes/interfaces inherited from class com.sptci.prevayler.StorageSystem
StorageSystem.TaskQueue
 
Field Summary
static String CLASS_FIELD
          The name of the field in the indexed document that stores the class name.
(package private) static String DOCUMENT_ID_FIELD
          The name of the field in the indexed document that stores the unique document id.
static String OBJECT_ID_FIELD
          The name of the field in the indexed document that stores the object id.
protected static org.apache.lucene.index.IndexReader reader
          The index reader instance to use to read the index.
static int SAVE_COUNT
          The number or index updates before a IndexWriter.commit() is invoked.
private  int saveCount
          A counter to ensure that the IndexWriter.commit() method is called periodically.
protected static org.apache.lucene.search.IndexSearcher searcher
          The index searcher instance to use to search documents.
static char SEPARATOR_CHAR
          The separator character used to delimit composite index field names.
private static long serialVersionUID
           
protected static org.apache.lucene.index.IndexWriter writer
          The index writer instance to use to create/delete documents.
 
Fields inherited from class com.sptci.prevayler.StorageSystem
logger
 
Constructor Summary
SearchSystem()
           
 
Method Summary
private  void commit()
          Commit pending writes to the index and refresh the reader.
private  org.apache.lucene.document.Document createDocument(PrevalentObject object)
          Create a document to store the full-text indices for the specified object.
private  String getDocumentId(PrevalentObject object)
          Return the unique document id for the index document representing the specified prevalent object.
private  boolean hasIndices(PrevalentObject object)
          Determine whether the prevalent object has any full-text search indices specified.
protected  void index(PrevalentObject object)
          Over-ridden to process search annotations for the object.
private  void indexClass(PrevalentObject object, org.apache.lucene.document.Document doc)
          Add full-text search indices for composite indices defined at the class level.
private  void indexFields(PrevalentObject object, org.apache.lucene.document.Document doc)
          Add full=text search indices for any fields annotated as searchable in the prevalent object.
private  void indexSearchable(PrevalentObject object, org.apache.lucene.document.Document doc, Searchable index)
          Process the Searchable annotation for the specified prevalent object class.
private  void indexSearchables(PrevalentObject object, org.apache.lucene.document.Document doc)
          Process the Searchables annotation for the specified prevalent object class.
protected  void remove(PrevalentObject object)
          Over-ridden to remove the search index for the specified prevalent object.
private  void save(PrevalentObject object, org.apache.lucene.document.Document document)
          Save the specified document to the index writer.
protected  void search(org.apache.lucene.search.Query query, org.apache.lucene.search.Filter filter, int count, org.apache.lucene.search.Sort sort, Collection<PrevalentObject> collection)
          Search the search indices and retrieve all the objects (regardless of type) that match the specified query and return up to the specified number of results.
 
Methods inherited from class com.sptci.prevayler.ConstraintSystem
checkFields, checkUnique, checkUnique, indexClass, indexFields, preAdd, preDelete, preDelete
 
Methods inherited from class com.sptci.prevayler.StorageSystem
generateOid, getIndexStorage, getIndexStorage, getPrimaryStorage, getPrimaryStorage, getReferenceStorage, getReferenceStorage, getRelationStorage, getRelationStorage, getTaskQueue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sptci.prevayler.DatabaseSystem
delete, save
 
Methods inherited from interface com.sptci.prevayler.AbstractDatabase
count, fetch, fetch, fetch, fetchIntersection, fetchUnion, search
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

writer

protected static final transient org.apache.lucene.index.IndexWriter writer
The index writer instance to use to create/delete documents.


reader

protected static transient org.apache.lucene.index.IndexReader reader
The index reader instance to use to read the index.


searcher

protected static transient org.apache.lucene.search.IndexSearcher searcher
The index searcher instance to use to search documents.


DOCUMENT_ID_FIELD

static final String DOCUMENT_ID_FIELD
The name of the field in the indexed document that stores the unique document id.

See Also:
Constant Field Values

CLASS_FIELD

public static final String CLASS_FIELD
The name of the field in the indexed document that stores the class name. "class"

See Also:
Constant Field Values

OBJECT_ID_FIELD

public static final String OBJECT_ID_FIELD
The name of the field in the indexed document that stores the object id. "objectId"

See Also:
Constant Field Values

SEPARATOR_CHAR

public static final char SEPARATOR_CHAR
The separator character used to delimit composite index field names. 35

See Also:
Constant Field Values

SAVE_COUNT

public static final int SAVE_COUNT
The number or index updates before a IndexWriter.commit() is invoked. Invoked only in batches to avoid expensive operation on each transaction.


saveCount

private int saveCount
A counter to ensure that the IndexWriter.commit() method is called periodically.

Constructor Detail

SearchSystem

SearchSystem()
Method Detail

index

protected void index(PrevalentObject object)
              throws PrevalentException
Over-ridden to process search annotations for the object. If full=text search has been specified, create a Document that holds all the indexed fields in the prevalent object.

Overrides:
index in class IndexSystem
Parameters:
object - The prevalent object to add query support for.
Throws:
PrevalentException - If errors are encountered while fetching the values of the fields in prevalent object.
See Also:
IndexSystem.indexFields(com.sptci.prevayler.PrevalentObject), IndexSystem.indexClass(com.sptci.prevayler.PrevalentObject)

remove

protected void remove(PrevalentObject object)
               throws PrevalentException
Over-ridden to remove the search index for the specified prevalent object. Over-ridden to remove the references to the prevalent object being removed from the system from the reference storage container. Remove the index for the specified prevalent object. This is typically called prior to deleting a prevalent object.

Overrides:
remove in class ConstraintSystem
Parameters:
object - The prevalent object to de-index.
Throws:
PrevalentException - If errors are encountered while removing the index for the prevalent object.

hasIndices

private boolean hasIndices(PrevalentObject object)
                    throws PrevalentException
Determine whether the prevalent object has any full-text search indices specified.

Parameters:
object - The prevalent object to check.
Returns:
Return true if any search indices have been specified.
Throws:
PrevalentException - If errors are encountered while reflecting upon the annotations in the prevalent object.

createDocument

private org.apache.lucene.document.Document createDocument(PrevalentObject object)
Create a document to store the full-text indices for the specified object.

Parameters:
object - The prevalent object to index.
Returns:
The new document instance.
See Also:
getDocumentId(PrevalentObject)

getDocumentId

private String getDocumentId(PrevalentObject object)
Return the unique document id for the index document representing the specified prevalent object.

Parameters:
object - The prevalent object for which the document id is to be retrieved.
Returns:
The unique document identifier.

indexFields

private void indexFields(PrevalentObject object,
                         org.apache.lucene.document.Document doc)
                  throws IllegalAccessException
Add full=text search indices for any fields annotated as searchable in the prevalent object.

Parameters:
object - The prevalent object whose fields are to be indexed.
doc - The document to which indices are to be added.
Throws:
IllegalAccessException - If errors are encountered while reflecting upon the fields in the prevalent object.

indexClass

private void indexClass(PrevalentObject object,
                        org.apache.lucene.document.Document doc)
                 throws IllegalAccessException
Add full-text search indices for composite indices defined at the class level. Composite index field names are assigned as a concatenation of the names of the fields that comprise the index if no value is specified for Searchable.name(). The concatenated names are delimited by the SEPARATOR_CHAR.

Parameters:
object - The prevalent object whose composite fields are to be indexed.
doc - The document to which indices are to be added.
Throws:
IllegalAccessException - If errors are encountered while reflecting upon the fields in the prevalent object.
See Also:
indexSearchables(PrevalentObject, org.apache.lucene.document.Document), indexSearchable(PrevalentObject, org.apache.lucene.document.Document, com.sptci.prevayler.annotations.Searchable)

indexSearchables

private void indexSearchables(PrevalentObject object,
                              org.apache.lucene.document.Document doc)
                       throws IllegalAccessException
Process the Searchables annotation for the specified prevalent object class. Create the composite indices for the specified array of composite indices.

Parameters:
object - The prevalent object whose composite fields are to be indexed.
doc - The document to which indices are to be added.
Throws:
IllegalAccessException - If errors are encountered while fetching the field values of the prevalent object.

indexSearchable

private void indexSearchable(PrevalentObject object,
                             org.apache.lucene.document.Document doc,
                             Searchable index)
                      throws IllegalAccessException
Process the Searchable annotation for the specified prevalent object class. Create the composite index for the array of fields.

Parameters:
object - The prevalent object whose composite fields are to be indexed.
doc - The document to which indices are to be added.
index - The searchable index annotation to process.
Throws:
IllegalAccessException - If errors are encountered while fetching the field values of the prevalent object.

save

private void save(PrevalentObject object,
                  org.apache.lucene.document.Document document)
           throws IOException
Save the specified document to the index writer. Delete an existing document instance if it exists.

Parameters:
object - The prevalent object that is being indexed.
document - The new document to add to the index.
Throws:
IOException - If errors are encountered while saving the document.
See Also:
IndexWriter.updateDocument( org.apache.lucene.index.Term, org.apache.lucene.document.Document), commit()

commit

private void commit()
             throws IOException
Commit pending writes to the index and refresh the reader.

Throws:
IOException - If errors are encountered while saving the document.

search

protected void search(org.apache.lucene.search.Query query,
                      org.apache.lucene.search.Filter filter,
                      int count,
                      org.apache.lucene.search.Sort sort,
                      Collection<PrevalentObject> collection)
               throws Exception
Search the search indices and retrieve all the objects (regardless of type) that match the specified query and return up to the specified number of results.

Parameters:
query - The query to execute.
filter - The filter to apply to the search.
count - The maximum number of results to return.
sort - The sort fields to apply to the results.
collection - The collection to which matching objects are added.
Throws:
Exception - If errors are encountered while reconstituting the prevalent objects or executing the search.
See Also:
AbstractDatabase.fetch(Class, Object)

SPT Object Database API