Developers

SDK Search 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(options: Object)

Options

The following options can be set when you create a new store:

Name Type Default Description
forceFilters Array [] Filters to send in every request, along with the selected facet refinements. For the format to use, see the /federated-search endpoint.
searchParams Object { matchHighlightTags: ['<strong class="inbenta-match">', '</strong>'], correctionHighlightTags: ['<em class="inbenta-correction">', '</em>'], expansionHighlightTags: ['<em class="inbenta-expansion">', '</em>'] } The API parameters to send in every request. For the format to use, see the /federated-search endpoint. Note: API parameters facets and attributes cannot be modified this way. For more detail, see the searchPARAMS note below.

searchParams note

You can use the store option searchParams to modify API search queries. However, some API attributes may be handled by other linked components. This means that even if you set some searchParams, they may not apply, depending on the combination of components that you use.

Here are the components that may affect this option:

Component Affected API attributes
Results offset, length
Refinement tabs attribute, sort
Refinement list attribute, sort
Results per page offset, length
Router filters, query, offset
Semantic autocomplete query, offset, length
Sort by selector sort

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
advancedSearchOptions Object The advanced search options:
allowExactSearchPattern allows users to search for an exact text with the use of quotes.
applySplitQueryMaxCharacters sets the maximum number of characters that a query should have to split it. (-1 indicates that the query should not be split)
// 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, filter: String = 'equals') Refine a facet with the given value and filter type. Valid filter types are equals, lt (less than), lte(less or equal than), gt (greater than), gte (greater or equal than).
addReturnNullFacet(name: String) Make an existing facet return null values (as '_null').
clearRefinements() Remove all refinements.
getAllContents() Retrieve all contents from the database, you can add filters or facets before calling the method and the contents will be filtered before returning them.
removeFacetRefinement(name: String, value: String = null, filter: String = 'equals') Remove the given refinement of a filter type. If value is null, all refinements for the facet are removed. Valid filter types are equals, lt (less than), lte(less or equal than), gt (greater than), gte (greater or equal than).
removeReturnNullFacet(name: String) Remove an existing facet from returning null values.
reset() Remove all cached query results. This method returns a promise.
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
advancedSearchOptions Object The advanced search options. allowExactSearchPattern: allows to search for an exact text while using quotes. applySplitQueryMaxCharacters: Set the max number of characters that a query should have to split it. (-1 indicates that the query shouldn't be split)
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
retryQuery String The retry query if using the advanced search options returned no result but retryWithoutAdvancedOptions was true and a retry was made that returned results.
retryWithoutAdvancedOptions Boolean Whether to retry the query without using advanced options.
searchParams Object The API parameters to send in every request. For the format to use, see the /federated-search endpoint.
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).