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. The store acts as a link between all the components. You can use the Search store for custom SDK behaviors, like applying custom filters, or 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 offers the Search store as an advanced solution for power users, to give them the extra flexibility they need to implement complex SDK behaviors, 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 searchStore variable inside the component that the builder returns, and inside any component that the SDK creates.

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 updates 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.
addReturnNullFacet(name: String) Make an existing facet return null values (as '_null').
clearRefinements() Remove all refinements.
removeFacetRefinement(name: String, value: String = null) Remove the given refinement. If value is null, all refinements for the facet are removed.
removeReturnNullFacet(name: String) Removes an existing facet from returning null values.
toggleFacetRefinement(name: String, value: String) Enables or disables the refinement.

Reading the store state

The store has the following 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, for example if you want to provide custom rendering.

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

Optimizations

Max results

The store can calculate the optimal maxResults parameter in the search request automatically, to query only the number of results needed for the current request without affecting the response.

The maxResults parameter is set to length + offset only if all the following conditions are met:

  • No faceting.
  • No sorting.
  • No filtering.
  • No user-provided maxResults parameter (via searchParams).