Lucene Search Engine and Indexing
Lucene Search Engine and Indexing
How to use Lucene search
- Create and populate a SearchCriteria bean. There are several SearchCriteria implementations:
- CategorySearchCriteria
- CustomerSearchCriteria
- ProductAssociationSearchCriteria
- ProductSearchCriteria
- PromotionSearchCriteria
- ShippingServiceLevelSearchCriteria
- 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 | |
customer(CM) | first name | customer first name | n/a | |
customer(CM) | last name | customer last name | n/a | |
customer(CM) | customer number | customer number(uidPk) | n/a | |
customer(CM) | | customer email | n/a | |
customer(CM) | phone number | phone number | 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 customer 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 to true to create a new index. This 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 to false 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.
- provides two key methods.
- void buildIndex(final boolean rebuild) - If rebuild is true, creates a new index, if rebuild is false, 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 in buildIndex.properties is set to true, calls buildIndex(true)
- if LastBuildDate property in buildIndex.properties is null or empty, calls buildIndex(true)
- else calls buildIndex(false)
PropertiesDao
- This DAO reads and saves properties files.
- getPropertiesFile(buildIndex) - Retrieves buildIndex.properties.
- storePropertiesFile(buildIndexProperties, buildIndex) - saves the Rebuild and LastBuildDate properties to buildIndex.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 documents.
- updateDocumentInIndex(documentList, product uid key, locale) - Update a document in an existing index.
- deleteDocumentInIndex(termsList, locale) - Delete a document in an existing index.