Sonal
Sonal

Reputation: 425

facetFilters in autocomplete algolia

I m facing difficulties setting facetFilters for algolia-autocomplete. If i want to filter based on a few organizations like : source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ ['organization_id:1', 'organization_id:10'], ['listing_status:Published' ]] }) It works perfectly fine.

Now I want to pick the values from a text box, it does not work. If the value of a text box is

<input type="hidden" id="marketplace_organization_ids" value="'organization_id:1', 'organization_id:10'"> source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ [$('#marketplace_organization_ids').val()], ['listing_status:Published' ]] }), Tried using array also but no result:

var name = ["'organization_id:1'","'organization_id:10'"]; source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ [name.join(',')], ['listing_status:Published' ]] })

I have used the above with one organization like and t works fine:

source: $.fn.autocomplete.sources.hits(index, { hitsPerPage: 5, facetFilters:[ ['organization_id:'+$('#user_organization_id').val() , 'distributors.id:'+$('#user_organization_id').val()] ] }),

Appreciate any help.

Upvotes: 2

Views: 216

Answers (1)

Jonathan Montane
Jonathan Montane

Reputation: 396

It seems that you are passing an incorrect value in your facetFilters, due to not parsing the value extracted from your input.

source: $.fn.autocomplete.sources.hits(index, {
  hitsPerPage: 5,
  facetFilters:[
    [$('#marketplace_organization_ids').val()],
    ['listing_status:Published']
  ]
})

evaluates to

source: $.fn.autocomplete.sources.hits(index, {
  hitsPerPage: 5,
  facetFilters:[
    ["'organization_id:1', 'organization_id:10'"],
    ['listing_status:Published']
  ]
})

According to the Algolia's documentation about facetFilters, you should instead provide an array of strings rather a string representation of an array, e.g.

source: $.fn.autocomplete.sources.hits(index, {
  hitsPerPage: 5,
  facetFilters:[
    ['organization_id:1', 'organization_id:10'], // no more double-quotes
    ['listing_status:Published']
  ]
})

You can do that by splitting your value before passing it to the array:

const organizationFilter = $('#marketplace_organization_ids').val()
  .split(',') // split string into array
  .map(filter => filter.trim()) // remove whitespaces

console.log(organizationFilter); // ['organization_id:1', 'organization_id:10']

Upvotes: 1

Related Questions