Closed ctjhoa closed 8 years ago
Hi @ctjhoa, before I start helping you maybe you could give me some context about what you want to achieve in term of usecase/feature? Then I can provide you the best answer. i.e. why do you need to use searchParameters to provide a numeric refinement?
Let's say I have on each record a price
attribute (integer from 0 to 100).
How can I request all records that match price > 50
? Where price
value is not available through facets.
As @vvo mentionned it would help a lot if you could give us some context about why do you want to use the SearchParameters class. Are you creating a new widget?
However, here is the syntax:
var state = helper.getState();
var stateWithPrice = state.addNumericRefinement('price', '>', 50);
// state !== stateWithPrice, SearchParameters are immutable
helper.setState(stateWithPrice);
For further reference, here is the documentation of this method. We don't know your use case, but usually it's best to use the helper instead of the SearchParameters.
Ok so I don't want to create a new widget.
I want to init instantsearch.js
with a predefined numeric filter.
Something like:
var search = instantsearch({
appId: 'APP_ID',
apiKey: 'API_KEY',
indexName: 'INDEX_NAME',
searchParameters: ??????? // price > 50
});
It's not explained in the documentation
@ctjhoa Ok this makes much more sense
The internal datastructures of the numeric refinements are not documented on purpose because they might evolve whereas the API / methods are less likely to change. That's why we suggest you create a small configuration widget for this purpose:
var search = instantsearch({
appId: 'APP_ID',
apiKey: 'API_KEY',
indexName: 'INDEX_NAME'
});
search.addWidget({
init: function(params) {
var helper = params.helper;
helper.addNumericRefinement('price', '>', 5);
}
});
Be careful, if you use the currentRefinedValues
widget, the filter will be displayed and a user will be able to discard it.
Ok thanks!
Glad we could help @ctjhoa Feel free to ask us anything :)
Hi,
Could you give an example of numeric filter's usage in
searchParameters
? I tried several things according to the documentation but I always got:Invalid syntax for numeric condition
Thanks