AddSearch / search-ui

JavaScript library to develop Search UIs for the web
https://www.addsearch.com/
MIT License
24 stars 4 forks source link

Howto filter on publication dates #50

Closed Lefthandmedia closed 3 years ago

Lefthandmedia commented 3 years ago

Hi, Each page has an article:published_time but how can i make a filter that shows all hits from only last week or all from just last month. I assumed the range filter would do the trick. But i can't see how.

tnx RDG

anttiai commented 3 years ago

Hi,

The article:published_time is indexed automatically in a field called doc_date. Range filters can contain both start and end boundaries, or just one boundary. Following operators are supported:

The filter for the last 7 days (without end boundary) would be:

{'range': {'doc_date': {'gte': '2021-02-24'}}}

A complete example of a radio button group with a few date range options would be:

searchui.filters({
  containerId: 'radio-filters-container',
  type: AddSearchUI.FILTER_TYPE.RADIO_GROUP,
  options: {
    nofilter: {
      label: 'All results'
    },
    thisyear: {
      label: 'This year',
      filter: {'range': {'doc_date': {'gte': '2021-01-01'}}}
    },
    lastyear: {
      label: 'Last year',
      filter: {'range': {'doc_date': {'gte': '2020-01-01', 'lt':'2021-01-01'}}}
    }
  }
});