-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(lxl-web, supersearch): Narrow search query when editing parts (LWS-273) #1190
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fc57145
Get edited ranges by parsing syntax tree
johanbissemattsson 21f493a
Add tests
johanbissemattsson fba4131
Only do search request if trimmed value isn't empty
johanbissemattsson 2dc8026
Update test desciptions
johanbissemattsson 4609ded
Calculate editedRanges in boolean queries
johanbissemattsson afd2ceb
Merge branch 'develop' into feat/LWS-273-find-using-cursor
johanbissemattsson 0965660
Fix codemirror-lang-lxlquery test
johanbissemattsson c639e73
Merge branch 'develop' into feat/LWS-273-find-using-cursor
johanbissemattsson 22caa24
Narrow down search query when editing qualifier parts
johanbissemattsson e4f524c
Rework getEditedPartQuery into getEditedPartEntries
johanbissemattsson a08e65b
Add test for getEditedPartEntries
johanbissemattsson 7058bc1
Keep _q as is for now (but add _qualifier param)
johanbissemattsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartEntries.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import getEditedPartEntries from './getEditedPartEntries'; | ||
|
||
describe('getEditedPartEntries', () => { | ||
it('narrows down search query when editing qualifier parts', () => { | ||
expect(getEditedPartEntries('hello title:"hej"', 16)).toEqual([['_qualifier', 'title:"hej"']]); | ||
}); | ||
it('keeps query as is when editing year qualifiers', () => { | ||
expect(getEditedPartEntries('hello ÅR:2024', 13)).toEqual([]); | ||
}); | ||
it('narrows down search query by base class for query codes', () => { | ||
expect(getEditedPartEntries('astrid lindgren subject:"winter"', 27)).toEqual([ | ||
['_qualifier', `"rdf:type":Topic "winter"`], | ||
['min-reverseLinks.totalItems', '1'] | ||
]); | ||
}); | ||
it('otherwise keeps query as is', () => { | ||
expect(getEditedPartEntries('hello', 5)).toEqual([]); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedPartEntries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import getEditedRanges from './getEditedRanges.js'; | ||
|
||
/** | ||
* TODO: How should we handle translated query codes and qualifier keys? | ||
*/ | ||
|
||
const QUALIFIER_KEY_BY_BASE_CLASS = { | ||
Library: 'itemHeldBy', | ||
Agent: 'contributor', | ||
Topic: 'subject', | ||
Subject: 'subject', | ||
Language: 'SPRÅK', | ||
GenreForm: 'genreForm', | ||
Person: 'person', | ||
Work: 'titel' | ||
}; | ||
|
||
const SKIP_QUALIFIERS = ['år']; | ||
|
||
/** | ||
* Gets the URLSearchParams entries which should be appended/replaced with new values when editing a part of a query. | ||
*/ | ||
|
||
function getEditedPartEntries(query: string, cursor: number): [string, string][] { | ||
const editedRanges = getEditedRanges(query, cursor); | ||
|
||
/** | ||
* Narrow down search query when editing qualifier parts | ||
*/ | ||
if (editedRanges.qualifierKey && editedRanges.qualifierOperator && editedRanges.qualifierValue) { | ||
const qualifierKey = query.slice(editedRanges.qualifierKey.from, editedRanges.qualifierKey.to); | ||
const qualifierOperator = query.slice( | ||
editedRanges.qualifierOperator.from, | ||
editedRanges.qualifierOperator.to | ||
); | ||
const qualifierValue = query.slice( | ||
editedRanges.qualifierValue.from, | ||
editedRanges.qualifierValue.to | ||
); | ||
|
||
if (SKIP_QUALIFIERS.includes(qualifierKey.toLowerCase())) { | ||
return []; // Keep query as is when editing year qualifiers | ||
} | ||
|
||
const baseClass = Object.entries(QUALIFIER_KEY_BY_BASE_CLASS).find( | ||
([, key]) => key === qualifierKey | ||
)?.[0]; | ||
|
||
if (baseClass) { | ||
return [ | ||
['_qualifier', `"rdf:type":${baseClass} ${qualifierValue}`], | ||
['min-reverseLinks.totalItems', '1'] // ensure results are linked/used atleast once | ||
]; | ||
} | ||
|
||
return [['_qualifier', qualifierKey + qualifierOperator + qualifierValue]]; | ||
} | ||
|
||
/** | ||
* Otherwise keep query entries as is | ||
*/ | ||
return []; | ||
} | ||
|
||
export default getEditedPartEntries; |
119 changes: 119 additions & 0 deletions
119
lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedRanges.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import getEditedRanges from './getEditedRanges'; | ||
|
||
describe('getEditedRanges', () => { | ||
it('calculates the edited range for a simple free-text query', () => { | ||
const query = 'hello'; | ||
const editedRanges = getEditedRanges(query, 5); | ||
expect(editedRanges).toEqual({ from: 0, to: 5 }); | ||
expect(query.slice(editedRanges.from, editedRanges.to)).toBe('hello'); | ||
}); | ||
|
||
it('calculates the edited range (with included ranges of qualifier parts) when editing a qualifier', () => { | ||
const query = 'hasTitle:"a"'; | ||
const editedRanges = getEditedRanges(query, 11); | ||
expect(editedRanges).toEqual({ | ||
from: 0, | ||
to: 12, | ||
qualifierKey: { | ||
from: 0, | ||
to: 8 | ||
}, | ||
qualifierOperator: { | ||
from: 8, | ||
to: 9 | ||
}, | ||
qualifierValue: { | ||
from: 9, | ||
to: 12 | ||
} | ||
}); | ||
expect(query.slice(editedRanges.from, editedRanges.to)).toBe('hasTitle:"a"'); | ||
expect(query.slice(editedRanges.qualifierKey?.from, editedRanges.qualifierKey?.to)).toBe( | ||
'hasTitle' | ||
); | ||
expect( | ||
query.slice(editedRanges.qualifierOperator?.from, editedRanges.qualifierOperator?.to) | ||
).toBe(':'); | ||
expect(query.slice(editedRanges.qualifierValue?.from, editedRanges.qualifierValue?.to)).toBe( | ||
'"a"' | ||
); | ||
}); | ||
|
||
it('calculates the edited range when editing a string', () => { | ||
expect(getEditedRanges('"hello"', 6)).toEqual({ | ||
from: 0, | ||
to: 7 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a group', () => { | ||
expect(getEditedRanges('(hello world)', 9)).toEqual({ | ||
from: 0, | ||
to: 13 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part after a qualifier', () => { | ||
expect(getEditedRanges('hasTitle:"a" hello', 18)).toEqual({ | ||
from: 12, | ||
to: 18 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part after a qualifier', () => { | ||
expect(getEditedRanges('hasTitle:"a" hello', 18)).toEqual({ | ||
from: 12, | ||
to: 18 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part before a qualifier', () => { | ||
expect(getEditedRanges('hello hasTitle:"a"', 5)).toEqual({ | ||
from: 0, | ||
to: 6 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part surrounded by qualifiers', () => { | ||
expect(getEditedRanges('hasTitle:"a" hello hasTitle:"b', 18)).toEqual({ | ||
from: 12, | ||
to: 19 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part after a group', () => { | ||
expect(getEditedRanges('(hi) hello', 7)).toEqual({ | ||
from: 4, | ||
to: 10 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part before a group', () => { | ||
expect(getEditedRanges('hello (hi)', 3)).toEqual({ | ||
from: 0, | ||
to: 6 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part surrounded by groups', () => { | ||
expect(getEditedRanges('(hi) hello (hej)', 7)).toEqual({ | ||
from: 4, | ||
to: 11 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part before a boolean operator', () => { | ||
expect(getEditedRanges('hello AND world', 5)).toEqual({ | ||
from: 0, | ||
to: 6 | ||
}); | ||
}); | ||
|
||
it('calculates the edited range when editing a part after a boolean operator', () => { | ||
expect(getEditedRanges('hello AND world', 12)).toEqual({ | ||
from: 9, | ||
to: 15 | ||
}); | ||
}); | ||
}); |
84 changes: 84 additions & 0 deletions
84
lxl-web/src/routes/api/[[lang=lang]]/supersearch/getEditedRanges.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { lxlQuery } from 'codemirror-lang-lxlquery'; | ||
|
||
type Range = { | ||
from: number; | ||
to: number; | ||
}; | ||
|
||
export type EditedRanges = Range & { | ||
qualifierKey?: Range; | ||
qualifierOperator?: Range; | ||
qualifierValue?: Range; | ||
}; | ||
|
||
function getEditedRanges(query: string, cursor: number): EditedRanges { | ||
const tree = lxlQuery.language.parser.parse(query); | ||
const innerNode = tree.resolveInner(cursor); | ||
|
||
/** | ||
* Return `from` and `to` from qualifier parts if editing qualifier value | ||
*/ | ||
if (innerNode.parent?.type.is('QualifierValue')) { | ||
const qualifierNode = innerNode.parent.parent; | ||
const qualifierKeyNode = qualifierNode?.getChild('QualifierKey'); | ||
const qualifierOperatorNode = qualifierNode?.getChild('QualifierOperator'); | ||
const qualiferValueNode = qualifierNode?.getChild('QualifierValue'); | ||
if (qualifierNode) { | ||
return { | ||
from: qualifierNode.from, | ||
to: qualifierNode.to, | ||
...(qualifierKeyNode && { | ||
qualifierKey: { from: qualifierKeyNode.from, to: qualifierKeyNode.to } | ||
}), | ||
...(qualifierOperatorNode && { | ||
qualifierOperator: { from: qualifierOperatorNode.from, to: qualifierOperatorNode.to } | ||
}), | ||
...(qualiferValueNode && { | ||
qualifierValue: { from: qualiferValueNode.from, to: qualiferValueNode.to } | ||
}) | ||
}; | ||
} | ||
} | ||
|
||
let from = 0; | ||
let to = query.length; | ||
|
||
/** | ||
* Adjust `from` and `to` if enclosed qualifiers or groups are found BEFORE the edited part | ||
* */ | ||
tree.iterate({ | ||
from: 0, | ||
to: cursor, | ||
enter(node) { | ||
if (node.type.is('Qualifier') || node.type.is('Group') || node.type.is('BooleanOperator')) { | ||
if (node.to > cursor) { | ||
from = node.from; | ||
to = node.to; | ||
} else { | ||
from = node.to; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Adjust `from` and `to` if enclosed qualifiers or groups are found AFTER the edited part | ||
* */ | ||
tree.iterate({ | ||
from, | ||
to, | ||
enter(node) { | ||
if ( | ||
(node.type.is('Qualifier') || node.type.is('Group') || node.type.is('BooleanOperator')) && | ||
node.from > cursor && | ||
node.from < to | ||
) { | ||
to = node.from; | ||
} | ||
} | ||
}); | ||
|
||
return { from, to }; | ||
} | ||
|
||
export default getEditedRanges; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting
BooleanOperator
(oddly enough) breaks the highlighting of BooleanQuerys as it is implemented now. But of course we should do it.My suggestion is when this and #1186 is merged, we properly re-add (the option to) highlighting operators within a valid BooleanQuery.