简体   繁体   English

如何为多值属性添加动态范围过滤器?

[英]How to add a Dynamic Range Filter for multi-value property?

The DynamicRangeFilter in Searchkit is a nice and easy solution to eg filter years. Searchkit中的DynamicRangeFilter是一个很好的简单解决方案,例如过滤年。 You would just save a year field in Elasticsearch and use the filter: 您只需在Elasticsearch中保存一年字段并使用过滤器:

<DynamicRangeFilter
    field="year"
    id="year"
    title="Year"
/>

So far I didn't find out how it could be used to filter a multi-value property, eg a range of years. 到目前为止,我还没有找到如何使用它来过滤多值属性,例如一系列年份。 Imagine you have a duration property with a start and end : 想象一下,你有一个具有startend的持续时间属性:

[
  {
    "id": 123,
    "title": "Foo",
    "duration": {
      "start": 2013,
      "end": 2016
    }
  },
  {
    "id": 234,
    "title": "Bar",
    "duration": {
      "start": 2015,
      "end": 2015
    }
  },
  {
    "id": 345,
    "title": "Baz",
    "duration": {
      "start": 2017,
      "end": 2020
    }
  }
]

Now, the filter should respect the duration and display all items within range. 现在,过滤器应该尊重持续时间并显示范围内的所有项目。 I'm not sure if and how fieldOptions could be used to achieve this. 我不确定是否以及如何使用fieldOptions来实现这一目标。

You can achieve this if you use date filter component for Searchkit like follows: 如果您对Searchkit使用日期过滤器组件,则可以实现此目的,如下所示:

A calendar-style date filter component for Searchkit Searchkit的日历样式日期过滤器组件

This Searchkit filter lets users filter start-end date ranges based on a chosen start time and an optional end time. 此Searchkit过滤器允许用户根据选定的开始时间和可选的结束时间过滤开始日期范围。

See demo directory for a working example. 有关工作示例,请参阅演示目录

在此输入图像描述

The Searchkit components needed for date range filtering were originally published here under the MIT license. 需要的日期范围过滤Searchkit组件最初发表在这里在MIT许可下。

Install 安装

 npm install --save-dev searchkit-datefilter 

Example

 import { SearchkitComponent } from "searchkit"; import { DateRangeFilter, DateRangeCalendar } from "searchkit-datefilter" class App extends SearchkitComponent { render() { <div> <DateRangeFilter id="event_date" title="Date range" fromDateField="event_date.from" toDateField="event_date.to" calendarComponent={DateRangeCalendar} fieldOptions={{ type: 'embedded', options: { path: 'event_date' } }} /> </div> } } 

Properties 属性

  • fromDateField (ESField) : Required . fromDateField (ESField)必需 An elasticsearch date field to use as the beginning. 用作开头的弹性搜索日期字段。
  • toDateField (ESField) : Required . toDateField (ESField)必需 An elasticsearch date field to use as the end. 用作结束的弹性搜索日期字段。
  • id (string) : Required . id (string)必需 id of component. 组件的id。 Must be unique. 必须是独特的。 Used as key for url serialisation 用作url序列化的密钥
  • title (string) : Required . title (字符串)必填 Title used for component and for selected filters component 用于组件和选定过滤器组件的标题
  • calendarComponent (ReactComponent): Calendar component to use when rendering calendarComponent (ReactComponent):呈现时使用的日历组件

    • Compatible with DateRangeCalendar DateRangeCalendar兼容
    • Defaults to DateRangeFilterInput which just shows two date math input fields 默认为DateRangeFilterInputDateRangeFilterInput显示两个日期数学输入字段
  • fieldOptions ({type:"embedded|nested|children", options:Object}) Configures the type field that is stored in ElasticSearch, can be embedded or nested or children fieldOptions ({type:“embedded | nested | children”,options:Object})配置存储在ElasticSearch中的类型字段,可以嵌入或嵌套或子项

    • type:nested requires options.path provided type:nested需要options.path提供
    • type:children requires options.childType provided type:children需要options.childType提供
    • see Field Options in Searchkit documentation 请参阅Searchkit文档中的字段选项
  • rangeFormatter ((count:number)=> string|number) A formatter function used to convert numbers into more readable display values. rangeFormatter ((count:number)=> string | number)格式化函数,用于将数字转换为更易读的显示值。 Eg long number formatting or prefixing currencies.` 例如长号格式或前缀货币

The full description you can find in date filter code repositories: here and here . 您可以在日期过滤器代码存储库中找到完整描述: 此处此处

The full example from the part of code above you can find here . 您可以在此处找到上面代码部分的完整示例。

I hope it helps you. 我希望它对你有所帮助。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM