algolia / instantsearch

⚡️ Libraries for building performant and instant search and recommend experiences with Algolia. Compatible with JavaScript, TypeScript, React and Vue.
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/
MIT License
3.73k stars 526 forks source link

Init with searchParameters & Numeric filter #1264

Closed ctjhoa closed 8 years ago

ctjhoa commented 8 years ago

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

vvo commented 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?

ctjhoa commented 8 years ago

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.

bobylito commented 8 years ago

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.

ctjhoa commented 8 years ago

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

bobylito commented 8 years ago

@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.

ctjhoa commented 8 years ago

Ok thanks!

bobylito commented 8 years ago

Glad we could help @ctjhoa Feel free to ask us anything :)