Implement custom search¶
This example shows how to create a new service which searches for products that:
- are discontinued
- are visible
It sets the sorting option to
relevance, adds a Manufacturer facet,
and boosts some of the fields (they will be more relevant than others).
The built-in implementation of search interface has all methods required for searching using the Solr gateway.
Create search method¶
Create a new method in the controller called
Use the existing
searchProducts() method as a basis. Adjust it to use a new parameter for filtering only discontinued products:
1 2 3 4 5 6
Implement the controller action¶
- Inject the search service into the controller.
- Create a new
EshopQueryobject. This object has all the parameters of the new Solr query.
EshopQuerysupports different filters/conditions, which are described below. To get discontinued product, use
SearchTermCondition, which has the search phrase and the specific field. In this case, in the current data model, all discontinued fields have the word
DECLINEin Solr field
- Add a condition for visibility, to make sure only visible products are returned.
- Add boosting so that SKU and name are more important than long description.
- The field boosting condition is implementation dependent. This means that the field name values which are passed to the constructor are different for eContent and content model search.
- For content model data, you need to pass the content Field identifier strings.
- For eContent you need to pass the raw Solr field names, including the default search field (which is
- Additionally add offset and limit for pagination purposes.
- Set sorting by relevance.
- Add product facet option for manufacturers.
- Now perform the search with the new parameters.
- The number of total hits is available in
- The search results is available in the
- Facets for search result are available in the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
ContentTypesCondition - filters results by Content Type. Valid Content Types are for example
SearchTermCondition - filters results by phrase, potentially in a specific field. For example: search for
SE10000 in field
1 2 3 4
SectionCondition - filters results by Section ID. Returns only elements that belong to the given Section.
SubtreeCondition - filters results by setting a path. Only elements under this path are fetched.
VisibilityCondition - filters results that are shown or hidden.
FieldBoosting - defines which fields should be boosted in search result. They have higher priority in search.
RelevanceSorting - sorts the results by relevance, which is internal Solr implementation for the
ProductFieldSorting - sorting for products. Supports sorting by name, SKU or price in a given direction.
ContentNameSorting - sorts the results by content Field name in a given direction.
SimpleProductFieldFacet - facet for specific product Field.
ProductSpecificationsFieldFacet - custom facet for product specification information, stored in Matrix Field Type.