Skip to content

Commit

Permalink
Low-hanging perf improvements in highlighting code
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Hernandez committed Feb 5, 2024
1 parent eea302d commit 1237b3a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-ghosts-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'contexture-elasticsearch': patch
---

Low-hanging perf improvements in elasticsearch highlighting
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { groupStats } from './groupStatUtils.js'
// [1, 2, 3] -> [{to: 1}, {from: 1, to: 2}, {from: 2, to: 3}, {from: 3}]
let boundariesToRanges = _.flow(
F.mapIndexed((to, i, list) => F.compactObject({ from: list[i - 1], to })),
(arr) => F.push({ from: _.last(arr).to }, arr)
(arr) => F.pushOn(arr, { from: _.last(arr).to })
)

let drilldownToRange = (drilldown) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,19 @@ let getFieldSubFields = (field) =>
/**
* Returns object of all subfields in a schema.
*/
let getSchemaSubFields = (schema) =>
F.reduceIndexed(
(acc, field, path) =>
F.mergeOn(
acc,
_.mapKeys((k) => `${path}.${k}`, getFieldSubFields(field))
),
{},
schema.fields
)
let getSchemaSubFields = _.memoize(
(schema) =>
F.reduceIndexed(
(acc, field, path) => {
let subFields = getFieldSubFields(field)
for (let k in subFields) acc[`${path}.${k}`] = subFields[k]
return acc
},
{},
schema.fields
),
_.get('elasticsearch.index')
)

/**
* Returns object of all group fields and their subfields in a schema.
Expand Down Expand Up @@ -148,7 +151,7 @@ export let getAllHighlightFields = _.memoize((schema) => {
let collectKeysAndValues = (f, coll) =>
F.reduceTree()(
(acc, val, key) =>
f(val) ? F.push(val, acc) : f(key) ? F.push(key, acc) : acc,
f(val) ? F.pushOn(acc, val) : f(key) ? F.pushOn(acc, key) : acc,
[],
coll
)
Expand Down

0 comments on commit 1237b3a

Please sign in to comment.