Lucene Search Engine and Indexing
How to use Lucene search
Create and populate a
SearchCriteria
bean. There are severalSearchCriteria
implementations:CategorySearchCriteria
ProductSearchCriteria
PromotionSearchCriteria
SkuSearchCriteria
UserSearchCriteria
Inject an
indexSearchService
bean and call the service’s search method./** * Searches the index with the given search criteria. * * @param searchCriteria the search criteria * @return a list of object uids which match the given search criteria */ List search(SearchCriteria searchCriteria);
Load objects based on your needs
The search will return the UIDs of the objects that can be used to load them from the database. However, you many only need to load the top 100 products of the returned 5000 product UID list.
You can use the following method in ProductService
to load products by their UIDs.
/**
* Returns a list of <code>Product</code> based on the given uids.
* The returned products will be populated based on the given load tuner. If a
* given product Uid is not found, it won't be included in the return list.
*
* @param productUids a collection of product uids
* @param loadTuner the load tuner
* @return a list of <code>Product</code>s
*/
List findByUids(Collection productUids, ProductLoadTuner loadTuner);
Search fields and index fields
Object | Search Fields | Index Fields | Locale | Comment |
---|---|---|---|---|
product(SF) | keyword | product display name; brand display name; sku code; attribute value which are defined indexable | all locales | If you give multiple keywords, like: keyword1 keyword2. The search result will products whose index fields contain both keyword1 and keyword2. |
product(SF) | active only | start date & end date | n/a | product is active if : start date <= now <= end date |
category(CM) | keyword | category display name, code | system default locale | |
category(CM) | active only | start date & end date | n/a | category is active if : start date <= now <= end date |
category(CM) | inactive only | start date & end date | n/a | category is inactive if : now <= start date or now >= end date |
product(CM) | keyword | product display name, code, sku code | system default locale | |
product(CM) | active only | start date & end date | n/a | product is active if : start date <= now <= end date |
product(CM) | inactive only | start date & end date | n/a | product is active if : now <= start date or now >= end date |
product(CM) | brand code | brand code | n/a | |
product(CM) | category uid | all ancestor categories uids | n/a |
For an object search, if multiple search fields are given, it’s always an AND
relationship among all of them.
Index builder
The index builder constructs the index that Lucene uses to execute searches.
The index builder is invoked by a Quartz scheduled job configured in Scheduled Jobs (Search Server). The index builder uses properties from buildIndex.properties
, which is described in the following section. There are several index builders that build indices for different entities and their index files are named according to the pattern xBuildIndex.properties
where x
is the name of the entity being indexed such as a category or product.
Key classes and files
The following files and Java classes are also related to the Lucene search index support.
buildIndex.properties
Contains the following two properties.
Rebuild
: Set totrue
to create a new indexThis provides a way to create a new index when the server is already started. The default value is
false
and the value is also set tofalse
after a successful index build/update.LastBuildDate
: The date when the last build was executed successfully
IndexBuildService
This service class handles the building of the Lucene index as well as updating and deleting documents from the index. It provides two key methods.
void buildIndex(final boolean rebuild)
If rebuild is
true
, creates a new index, if rebuild isfalse
, updates the existing index.void buildIndex()
Checks the following conditions to determine whether to create a new index or update an existing index.
- If the
Rebuild
property inbuildIndex.properties
is set totrue
, callsbuildIndex(true)
- If
LastBuildDate
property inbuildIndex.properties
isnull
or empty, callsbuildIndex(true)
- Else calls
buildIndex(false)
- If the
PropertiesDao
This DAO (Data Access Object) reads and saves properties files.
getPropertiesFile(buildIndex)
: RetrievesbuildIndex.properties
storePropertiesFile(buildIndexProperties, buildIndex)
: Saves theRebuild
andLastBuildDate
properties tobuildIndex.properties
ProductService
This service class handles product retrieval.
list()
Returns list of all products, this is used to create a new index.
findByModifiedDate(lastBuildDate)
Returns a list of products whose modified date is after the last build date, this is used to update index.
findByDeletedDate(lastBuildDate)
Returns a list of deleted products whose deleted date is after the last build date, this is used to update index
IndexDao
This DAO creates new indices and updates/deletes documents from existing indices.
createIndex(documentList, locale)
: Create a new index with the passed in list of documentsupdateDocumentInIndex(documentList, product uid key, locale)
: Update a document in an existing indexdeleteDocumentInIndex(termsList, locale)
: Delete a document in an existing index
Related Code
The following packages contain code related to the Lucene search.
com.elasticpath.domain.search.*
com.elasticpath.service.index.*
com.elasticpath.persistence.IndexWriter
com.elasticpath.persistence.Searcher