itemsapi / itemsjs

Extremely fast faceted search engine in JavaScript - lightweight, flexible, and simple to use

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Always show selected filter/facet (on top)

Letrab opened this issue · comments

Hi!

Currently, selecting facets/filters can become confusing, as by default apparently, a selected filter will not always be visible.

Eg: https://jsfiddle.net/cigol/0ef9qeos/5/

First select "Morgan Freeman" from the actors. Secondly select "falling into a well" as tag.
Now "Morgan Freeman" is not shown in the actors list anymore... So it's impossible to unselect it again as well...

Is there an option to always show it (on top)?

Thanks!

Hey, definitely good point. Filters should be sorted by filters_count and is_selected, but it's not supported now though.. I hope it will be introduced in some future version. For now I propose looking for some workaround

Thanks for your response.

Fairly new to JS development, but this seems to work for me? :

  // transform object of objects to array of objects
  buckets = _.map(buckets, function (val, key) {
    return {
      key: key,
      doc_count: val,
      selected: helpers.includes_any_element(key, agg.filters)
    };
  });

  if (agg.sort === 'term') {
    buckets = _.orderBy(buckets, ['selected', 'key'], ['desc', agg.order || 'asc']);
  } else {
    buckets = _.orderBy(buckets, ['selected', 'doc_count', 'key'], ['desc', agg.order || 'desc', 'asc']);
  }

Not sure how to contribute and write tests. But this addition does not break any existing tests and fixes the issue. What do you think? ;-)

Looks promising. Contributing is quite simple. You need to make a fork and work on your own version. You modify code, add a test which tests this new sorting case. Once it is finished you commit to your fork and propose a merge with this master repo. If it is ok then I am accepting and I create a new version of this library.

Your above example if it is correct solves more than 50% the problem. Contribution is just only a process which works very similar everywhere

Okay, thank you.

Will do! (Was confused with the double code in dist, but now I see you push that one to comply with npm, right?) So I'll make my changes in the src folder files.

Only hard point, for me, will be how to "select" a filter in the test cases. Is the aggregationsSpec the correct place for this?

What do you mean with more than 50%? Is there anything missing still? Solves all the problems from my original report though.

Thanks!

The dist is being used for the frontend side. This is minified version of /src. You can ignore it.

I see your point with "selecting" filter.
The filters should be provided to the .buckets function.
This is how function execution looks now:
var buckets = module.exports.buckets(items, input.name, aggregations[input.name], aggregations)

It can be provided as the new argument var buckets = module.exports.buckets(items, input.name, aggregations[input.name], aggregations, input.filters)

Or modified second argument var buckets = module.exports.buckets(items, input, aggregations[input.name], aggregations) but there will be more refactoring in tests so maybe the first will be simplier for now.

aggregationsSpec tests are higher level tests of facets. There is also bucketsSpec which tests exactly this new case. For me adding tests to bucketsSpec is better

In 50% I mean you have completed most of this issue by understanding code and making modification. The rest is just a test and a contribution.

I had to use aggregationsSpec, because I had to call .aggregations because I use multiple filters, no?
Also the extending of the API didn't seem necessary after all?

Validated with a test case, which also fails without my addition in lib.js.

Feel free to comment/edit/....

Thanks!

sorry -but this default feature is not good for short facet lists as causes blinking effect. I think that more productive way to make it as parameter of aggregation config.

@SergeyRe for the short lists that's right. Feel free to create a PR - it can be configurable by i.e. chosen_filters_on_top: false in configuration. By default chosen filters should be on top like now

Thank you for invitation. I hope i will try. By the way is there option to exclude facets with zero doc_count or it is also welcome for PR?

It also needs a PR. It can be named like hide_zero_doc_count. You can also exclude them in your project quite easily while you are rendering facets UI

it makes me crazy ))
actually i need selected facet value always be presented ( to show customer his choice made and possibility to deselect it)
even if it has zero doc_count (i.e. after followed query search)
no other zero doc_count facets are had to be presented
whats aggs config i have to do for this?

What is your current used ItemsJS version ? In the newest one itemsjs@2.1.20, selected facet value should be always on top by default.
You can also specify different filters sorting for specific facet (in this case genres) like:

aggregations: {
  genres: {
    sort: ['selected', 'key'],
      order: ['desc', 'asc']
  }
}

You can sort facet filters by selected, key (alphabetically) or count (the most / the least common).
The default sorting is: selected, count (desc), key

sorry for bad cleariness
my post more related to #117
let me continue there