elastic / app-search-javascript

Elastic App Search Official JavaScript Client
https://www.elastic.co/products/app-search
Apache License 2.0
66 stars 21 forks source link

Retrieve selected facets first with disjunctiveFacets #24

Open afunnydev opened 4 years ago

afunnydev commented 4 years ago

Let's say we are using the facet with the following options:

const options = {
  facets: {
    location: [{ type: 'value', size: 6 }],
  },
  disjunctiveFacets: ['location'],
  filters: { location: ['Canada', 'United States']},
};
client
  .search('yoga', options);
  .then(({ info }) => {
    console.log(info.facets.location);
  });

If the location Canada is not in the first 6 most popular facet results for this query, it is not included in info.facets.location. Only the 6 most populars are accessible. However, I can see we retrieve this data from the request that is not Facet-Only (since it returns only the two facets that we used to filter).

If we use the facets results to display checkboxes (with multiple selection active), it is a problem since the Canada option will not be shown, and then cannot be remove from the filters. We need to "cache" that selected value, and then display it without it's count.

I don't know if this should be implemented at a endpoint level, but I think the active filters should be returned as the first values when retrieving facet values, so that the UI can be implemented out-of-the-box. Even if they are empty (count = 0). That way, it will be easier to implement a UI based on them.

JasonStoltz commented 4 years ago

I agree, this is a good feature suggestion. And yes, I think it should definitely be at the endpoint level. In the meantime, I would recommend fetching a much larger "size" and implement that sort yourself.

afunnydev commented 4 years ago

Thanks. That's what I'll do for the moment. It's unfortunately still a issue for selected facets with no results, since they are not returned even if the size is large. I will just cache that for now. Curious on how bad the impact on performance couldl be for a size > 100 (meaning I query for litteraly all facet values)?

JasonStoltz commented 4 years ago

I wouldn't expect it to be bad. Try experimenting with it, I'd be curious to know how it performs as well.