Store

Before using search store

Note: All examples shown are set in a Production environment.

What is a search store?

A search store is a module that the Search SDK generates to coordinate the different searches along all the components in the SDK. It manages the following:

  • Search parameters and results,
  • Filtering logic, and
  • Events layer.

When you create your Search SDK, it automatically creates the Search store, and it acts as a link to all the components. The Search store can be used for custom SDK behaviours, like applying custom filters, or even applying different search logics in different components.

Do I need to learn how to use the Search store?

No. You can use the Inbenta Search SDK without having to touch the Search store at all. Inbenta simply offers the Search store as an advanced solution, intended for power users who need the extra flexibility required to implement complex SDK behaviours, and the specialized tools to do it. If you do not have these specific needs, Inbenta recommends that you use SDK components whenever possible.

Using the Search store from the SDK

The Search store is contained in the variable "searchStore" inside the component that the builder returns, and inside any component created by the SDK.

Examples:

Builder integration

var build = sdk.build('#inbenta', buildConfig);
build.searchStore.query = "help";

Components integration

var results = sdk.component('results', '#results', resultsConfig);
results.searchStore.query = "help";

Creating a new Search store

If you need to use a different Search store than the one the SDK implements, you can create a new one like this:

var store = sdk.createStore()

Searching with the store

The store triggers searches when any of these parameters change:

Properties

Name Type Description
query String The search query
page Number The current page
perPage Number The number of results per page
userType Number The user type
sortBy Array<String> The attribute names used to sort the results
// Change the search parameters.
store.query = 'cars';
store.page = 2;
store.perPage = 1;
sort.sortBy = ['desc(_relevance)', 'asc(price)'];

// After a while, the store will update itself with the new response.

Methods

Name Description
addFacet(name: String, type: String) Use the attribute as a facet. type must be one of the following: and, or.
addFacetRefinement(name: String, value: String) Refine a facet with the given value.
removeFacetRefinement(name: String, value: String = null) Remove the given refinement. If value is null, all refinements for the facet are removed.
toggleFacetRefinement(name: String, value: String) Enables or disables the refinement.
clearRefinements() Remove all refinements.

Reading the store state

The store has some properties and methods to retrieve the current state:

Properties

Name Type Description
lastQuery String The query that triggered the actual results
hasResults Boolean Whether the search has results or not
results Array The results from the last search
totalResults String The total number of results

Methods

Name Description
getFacetValues(attribute: String, sortBy: Array<String>) : Array Retrieve the facet values for a given attribute
numberOfResultsPerRefinement(attribute: String) : Number Retrieve the number of results for the given attribute

Events

The store emits events in certain situations. You can listen to these events to provide custom rendering, for example.

store.on('result', function() {
    console.log("result:", store.results);
});
Name Description
search Emitted whether a search request is sent to the API
result Emitted whether the store results change