Skip to main content
Version: 7.5.x

Lucene Search Engine and Indexing

  1. Create and populate a SearchCriteria bean. There are several SearchCriteria implementations:

    • CategorySearchCriteria
    • CustomerSearchCriteria
    • ProductAssociationSearchCriteria
    • ProductSearchCriteria
    • PromotionSearchCriteria
    • SkuSearchCriteria
    • UserSearchCriteria
  2. 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);
  3. 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​

ObjectSearch FieldsIndex FieldsLocaleComment
product(SF)keywordproduct display name; brand display name; sku code; attribute value which are defined indexableall localesIf you give multiple keywords, like: keyword1 keyword2. The search result will products whose index fields contain both keyword1 and keyword2.
product(SF)active onlystart date & end daten/aproduct is active if : start date <= now <= end date
category(CM)keywordcategory display name, codesystem default locale
category(CM)active onlystart date & end daten/acategory is active if : start date <= now <= end date
category(CM)inactive onlystart date & end daten/acategory is inactive if : now <= start date or now >= end date
product(CM)keywordproduct display name, code, sku codesystem default locale
product(CM)active onlystart date & end daten/aproduct is active if : start date <= now <= end date
product(CM)inactive onlystart date & end daten/aproduct is active if : now <= start date or now >= end date
product(CM)brand codebrand coden/a
product(CM)category uidall ancestor categories uidsn/a
customer(CM)first namecustomer first namen/a
customer(CM)last namecustomer last namen/a
customer(CM)customer numbercustomer number(uidPk)n/a
customer(CM)emailcustomer emailn/a
customer(CM)phone numberphone numbern/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. It 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 (Data Access Object) 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

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