Skip to content

Commit f3fbe18

Browse files
zubeydecivelekzzacharo
authored andcommitted
demos: add range facet to opensearch and videos
1 parent a181ac4 commit f3fbe18

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

src/demos/cern-videos/App.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Accordion, Card, Container, Grid, Image, Item, Menu } from "semantic-ui
1414
import { InvenioSearchApi } from "../../lib/api/contrib/invenio";
1515
import {
1616
BucketAggregation,
17+
RangeFacet,
1718
EmptyResults,
1819
Error,
1920
ReactSearchKit,
@@ -180,6 +181,21 @@ export class App extends Component {
180181
<Grid relaxed style={{ padding: "2em 0" }}>
181182
<Grid.Row columns={2}>
182183
<Grid.Column width={4}>
184+
<RangeFacet
185+
title="Publication Year"
186+
agg={{
187+
aggName: "years",
188+
}}
189+
rangeSeparator="--"
190+
defaultRanges={[
191+
{ label: "Last 1 year", type: "years", value: 1 },
192+
{ label: "Last 10 years", type: "years", value: 10 },
193+
]}
194+
enableCustomRange
195+
dateRangeToLabel="-"
196+
customDatesLabel="Choose dates"
197+
/>
198+
<br />
183199
<BucketAggregation
184200
title="Categories"
185201
agg={{

src/demos/opensearch/App.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { OSSearchApi } from "../../lib/api/contrib/opensearch";
2424
import {
2525
BucketAggregation,
26+
RangeFacet,
2627
EmptyResults,
2728
Error,
2829
ReactSearchKit,
@@ -159,6 +160,17 @@ export class App extends Component {
159160
<Grid relaxed style={{ padding: "2em 0" }}>
160161
<Grid.Row columns={2}>
161162
<Grid.Column width={4}>
163+
<RangeFacet
164+
title="Year"
165+
agg={{ aggName: "years" }}
166+
rangeSeparator=".."
167+
defaultRanges={[
168+
{ label: "Last 1 year", type: "years", value: 1 },
169+
{ label: "Last 5 years", type: "years", value: 5 },
170+
{ label: "Last 6 months", type: "months", value: 6 },
171+
]}
172+
enableCustomRange
173+
/>
162174
<BucketAggregation
163175
title="Tags"
164176
agg={{ field: "tags", aggName: "tags_agg" }}

src/demos/opensearch/DemoOSRequestSerializer.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,48 @@ export class DemoOSRequestSerializer {
8686
subtype_agg: "employee_type.subtype",
8787
};
8888
const aggValueObj = this.getFilters(filters);
89+
const mustClauses = [];
8990
// conver to object
90-
const terms = Object.keys(aggValueObj).map((aggName) => {
91-
const obj = {};
91+
Object.keys(aggValueObj).forEach((aggName) => {
92+
const values = aggValueObj[aggName];
93+
if (aggName === "years") {
94+
const rangeValue = values[0];
95+
if (!rangeValue) return;
96+
97+
const [fromPart, toPart] = rangeValue.split("..");
98+
if (!fromPart || !toPart) return;
99+
100+
const fromYear = fromPart.slice(0, 4);
101+
const toYear = toPart.slice(0, 4);
102+
103+
const gte = fromPart.length > 4 ? fromPart : `${fromYear}-01-01`;
104+
const lte = toPart.length > 4 ? toPart : `${toYear}-12-31`;
105+
106+
mustClauses.push({
107+
range: {
108+
date: { gte, lte },
109+
},
110+
});
111+
112+
return;
113+
}
92114
const fieldName = aggFieldsMapping[aggName];
93-
obj[fieldName] = aggValueObj[aggName];
94-
return { terms: obj };
115+
if (fieldName) {
116+
mustClauses.push({
117+
terms: {
118+
[fieldName]: values,
119+
},
120+
});
121+
}
95122
});
96-
bodyParams["post_filter"] = { bool: { must: terms } };
123+
124+
if (mustClauses.length) {
125+
bodyParams.post_filter = {
126+
bool: {
127+
must: mustClauses,
128+
},
129+
};
130+
}
97131
}
98132

99133
// simulate a backend that defines all the possible complex aggregations per index
@@ -114,6 +148,16 @@ export class DemoOSRequestSerializer {
114148
};
115149
_extend(bodyParams["aggs"], aggBucketNestedEmployeeType);
116150

151+
_extend(bodyParams.aggs, {
152+
years: {
153+
date_histogram: {
154+
field: "date",
155+
calendar_interval: "year",
156+
format: "yyyy",
157+
},
158+
},
159+
});
160+
117161
return bodyParams;
118162
};
119163
}

0 commit comments

Comments
 (0)