Skip to content

Commit

Permalink
Merge pull request #255 from smartprocure/GS-8040/pop-filter-enhancem…
Browse files Browse the repository at this point in the history
…ents

[GS-8040] Add open upper bound capability to `step` query
  • Loading branch information
stellarhoof authored Aug 30, 2024
2 parents aa7e3f4 + c8c83a9 commit a5f3af5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-pigs-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'contexture-elasticsearch': minor
---

capbility of dealing with `null` ranges added to `step` query on elastic search provider
23 changes: 5 additions & 18 deletions packages/provider-elasticsearch/src/example-types/filters/step.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,26 @@ const getMinAndMax = (range) => {
return { min, max }
}

const isValueMaxOnSteps = (value, steps) => {
const max = steps[steps.length - 1]
return value === max
}

let filter = ({ field, range, steps }) => {
let filter = ({ field, range }) => {
const { min, max } = getMinAndMax(range)

/** In the last step, we do not filter by the upper bound */
if (isValueMaxOnSteps(max, steps)) {
return {
range: { [field]: pickSafeNumbers({ gte: min }) },
}
}

return {
range: { [field]: pickSafeNumbers({ gte: min, lte: max }) },
}
}

let buildQuery = (field, range, steps) => {
let buildQuery = (field, range) => {
return {
aggs: {
rangeFilter: {
filter: filter({ field, range, steps }),
filter: filter({ field, range }),
},
},
}
}

let result = async (node, search) => {
let { field, range, steps } = node
const result = await search(buildQuery(field, range, steps))
let { field, range } = node
const result = await search(buildQuery(field, range))
const recordsCount = result.aggregations.rangeFilter.doc_count
return { recordsCount }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ describe('step', () => {
expect(filter(input)).toEqual(expectedValue)
})

it('removes upper bound when range is last step', () => {
it('removes upper bound when upper bound is null', () => {
const inputWithNewRange = {
...input,
range: [0, 1000],
range: [0, null],
}
let expectedValue = { range: { test: { gte: 0 } } }
expect(filter(inputWithNewRange)).toEqual(expectedValue)
Expand All @@ -38,7 +38,7 @@ describe('step', () => {
},
},
}
let output = buildQuery(input.field, input.range, input.steps)
let output = buildQuery(input.field, input.range)
expect(output).toEqual(expectedDSL)
})
})

0 comments on commit a5f3af5

Please sign in to comment.