Searching
Get data from the index
Last updated
Was this helpful?
Get data from the index
Last updated
Was this helpful?
Ucommerce has five built-in indexes: Store, Product, Catalog, Category, and PriceGroup.
To query an index, you need to take a dependency on IIndex<T>
where T
is one of the .
The IIndex
interface allows you to query the index with a LINQ-like fluid syntax.
To get started, you get the ISearch<Model>
object that you will do your queries on from the index.
Be aware that the interface is LINQ-like but not LINQ. This means the interface does not support certain things. See for more details.
You can use the Where()
method to filter the results in the index down to only the objects you are interested in. See below for filtering on preconfigured dimensions.
The simplest filter is to match on equality.
The Match class is for more advanced matching like Full Text, Literal, Fuzzy, and Wildcard.
To query in a range, you can use the Range(int from, int to)
method on the Match class.
You can sort the results with the Order
and OrderByDescending
methods.
Paging can be done using the Skip and Take methods.
Like with LINQ, the execution of the query is deferred until you finish the query definition and get the results by calling one of the following methods.
You should call these methods last to avoid unexpected behavior and keep the query on a search engine level instead of in memory. This also avoids the use of excessive resources on your web server.
If you need a total count for a query, it is available on the query result as TotalCount
. The TotalCount
property gives you the total number of objects in the index.
To get the number of objects the query returns, use the Count
property on the Results
property.
To give suggestions to the user when they are typing in a search field, you can use the Suggestions()
search method.
The Fuzzy parameter controls whether or not the suggestions should be fuzzy, which takes spelling errors into account. This will impact performance, so keep that in mind.
Product
ProductSearchModel
Store
StoreSearchModel
Catalog
CatalogSearchModel
Category
CategorySearchModel
PriceGroup
PriceGroupSearchModel
Although the fluid query syntax is LINQ-like, it isn't fully LINQ-compatible.
❌ Using Contains()
on a search model collection property of complex objects does not work:
✅ Using Contains()
on a search model collection property of simple types works fine:
✅ Using Contains()
on an in-memory collection checking for an index model property works fine:
The method ToSuggestions
mentioned in also triggers the query execution.
To get suggestions on a field you need to have added Suggestable()
to the .
Facets can be used to filter based on preconfigured dimensions. First, follow the documentation on how to . After facets are configured and the index created, the facets can be accessed using the ToFacets()
search method. This method can also be told to return the results of the query to avoid multiple calls to Elasticsearch.
Note: The overall facet should be used for creating a header in the filter page while the individual facet values should become actual filters, e.g. checkboxes or sliders. See for a visual example.