Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/es_highlight_exclude.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'contexture-elasticsearch': patch
---

Handle node `exclude` while highlighting, that is, remove the excluded fields from highlighting. Also, expand `include` to handle glob expansion for highlighting.
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,22 @@ export let getRequestHighlightFields = (schema, node) => {
// are currently using to work around performance issues when highlighting
// fields not included in the node.
if (!node.highlight?.highlightOtherMatches && _.isArray(node.include)) {
const include = expandGlobs(schema, node.include)
let subFields = getSchemaSubFields({
fields: _.pick(node.include, schema.fields),
fields: _.pick(include, schema.fields),
})
highlightFields = _.pick(
_.uniq([...node.include, ..._.keys(subFields)]),
_.uniq([...include, ..._.keys(subFields)]),
highlightFields
)
}
if (_.isArray(node.exclude)) {
const exclude = expandGlobs(schema, node.exclude)
let subFields = getSchemaSubFields({
fields: _.pick(exclude, schema.fields),
})
highlightFields = _.omit(
_.uniq([...exclude, ..._.keys(subFields)]),
highlightFields
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,112 @@ describe('getRequestHighlightFields()', () => {
},
})
})
it('should expand include', () => {
const schema = {
fields: {
state: {
elasticsearch: {
dataType: 'text',
mapping: {
fields: {
keyword: { type: 'keyword' },
subfield: { type: 'text' },
},
},
},
},
'city.street': {
elasticsearch: {
dataType: 'text',
mapping: {
fields: {
keyword: { type: 'keyword' },
subfield: { type: 'text' },
},
},
},
},
},
}
const node = { include: ['city'] }
const actual = getRequestHighlightFields(schema, node)
expect(actual).toEqual({
'city.street': {},
'city.street.subfield': {},
})
})
it('should expand exclude, irrespective of highlightOtherMatches', () => {
const schema = {
fields: {
state: {
elasticsearch: {
dataType: 'text',
mapping: {
fields: {
keyword: { type: 'keyword' },
subfield: { type: 'text' },
},
},
},
},
'city.street': {
elasticsearch: {
dataType: 'text',
mapping: {
fields: {
keyword: { type: 'keyword' },
subfield: { type: 'text' },
},
},
},
},
},
}
const node1 = {
exclude: ['city'],
highlight: { highlightOtherMatches: true },
}
const actual1 = getRequestHighlightFields(schema, node1)
expect(actual1).toEqual({ state: {}, 'state.subfield': {} })
const node2 = {
exclude: ['city'],
highlight: { highlightOtherMatches: false },
}
const actual2 = getRequestHighlightFields(schema, node2)
expect(actual2).toEqual({ state: {}, 'state.subfield': {} })
})
it('should handle both include and exclude', () => {
const schema = {
fields: {
state: {
elasticsearch: {
dataType: 'text',
mapping: {
fields: {
keyword: { type: 'keyword' },
subfield: { type: 'text' },
},
},
},
},
'city.street': {
elasticsearch: {
dataType: 'text',
mapping: {
fields: {
keyword: { type: 'keyword' },
subfield: { type: 'text' },
},
},
},
},
},
}
const node = {
include: ['state'],
exclude: ['state'],
}
const actual = getRequestHighlightFields(schema, node)
expect(actual).toEqual({})
})
})