-
-
Notifications
You must be signed in to change notification settings - Fork 193
feat(search): support quoted phrases in advanced search syntax #151
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
Merged
Changes from all commits
Commits
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 hidden or 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,17 @@ | ||
| import { formatAdvancedSearchText, formatAdvancedSearchValue } from "./searchSyntax"; | ||
|
|
||
| test('formatAdvancedSearchValue keeps simple values unquoted', () => { | ||
| expect(formatAdvancedSearchValue('Inbox')).toBe('Inbox'); | ||
| }); | ||
|
|
||
| test('formatAdvancedSearchValue quotes values with spaces', () => { | ||
| expect(formatAdvancedSearchValue('Daily Reads')).toBe('"Daily Reads"'); | ||
| }); | ||
|
|
||
| test('formatAdvancedSearchValue escapes quotes inside quoted values', () => { | ||
| expect(formatAdvancedSearchValue('Daily "Reads"')).toBe('"Daily \\"Reads\\""'); | ||
| }); | ||
|
|
||
| test('formatAdvancedSearchText returns prefixed text with trailing space', () => { | ||
| expect(formatAdvancedSearchText('collection:', 'Daily Reads')).toBe('collection:"Daily Reads" '); | ||
| }); |
This file contains hidden or 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,18 @@ | ||
| export function formatAdvancedSearchValue(value: string) { | ||
| const trimmedValue = value.trim(); | ||
|
|
||
| if (!trimmedValue) { | ||
| return ''; | ||
| } | ||
|
|
||
| if (!/[\s"]/.test(trimmedValue)) { | ||
| return trimmedValue; | ||
| } | ||
|
|
||
| return `"${trimmedValue.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; | ||
| } | ||
|
|
||
| export function formatAdvancedSearchText(prefix: string, value: string) { | ||
| const formattedValue = formatAdvancedSearchValue(value); | ||
| return formattedValue ? `${prefix}${formattedValue} ` : undefined; | ||
| } |
This file contains hidden or 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 hidden or 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
58 changes: 58 additions & 0 deletions
58
.../huntly-server/src/test/java/com/huntly/server/service/LuceneServiceSearchSyntaxTest.java
This file contains hidden or 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,58 @@ | ||
| package com.huntly.server.service; | ||
|
|
||
| import com.huntly.server.config.HuntlyProperties; | ||
| import com.huntly.server.domain.entity.Collection; | ||
| import com.huntly.server.repository.CollectionRepository; | ||
| import com.huntly.server.repository.PageRepository; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class LuceneServiceSearchSyntaxTest { | ||
|
|
||
| @Test | ||
| void splitSearchTokens_keepsQuotedAdvancedSearchValueTogether() { | ||
| List<String> tokens = LuceneService.splitSearchTokens("machine collection:\"Daily Reads\" author:\"Jane Doe\""); | ||
|
|
||
| assertThat(tokens).containsExactly("machine", "collection:Daily Reads", "author:Jane Doe"); | ||
| } | ||
|
|
||
| @Test | ||
| void splitSearchTokens_unescapesQuotedCharactersInsideQuotedValue() { | ||
| List<String> tokens = LuceneService.splitSearchTokens("collection:\"Daily \\\"Reads\\\"\""); | ||
|
|
||
| assertThat(tokens).containsExactly("collection:Daily \"Reads\""); | ||
| } | ||
|
|
||
| @Test | ||
| void extractAdvancedSearchKeyword_usesFirstSeparatorOnly() { | ||
| String keyword = LuceneService.extractAdvancedSearchKeyword("url:https://example.com/a:b", ":"); | ||
|
|
||
| assertThat(keyword).isEqualTo("https://example.com/a:b"); | ||
| } | ||
|
|
||
| @Test | ||
| void extractCompleteSearch_usesQuotedCollectionNameForCollectionFilter() { | ||
| CollectionRepository collectionRepository = mock(CollectionRepository.class); | ||
| Collection collection = new Collection(); | ||
| collection.setId(42L); | ||
| collection.setName("Daily Reads"); | ||
| when(collectionRepository.findByNameContainingIgnoreCase("Daily Reads")).thenReturn(List.of(collection)); | ||
|
|
||
| LuceneService luceneService = new LuceneService( | ||
| mock(PageRepository.class), | ||
| mock(PageListService.class), | ||
| new HuntlyProperties(), | ||
| collectionRepository | ||
| ); | ||
|
|
||
| LuceneService.CompleteSearch completeSearch = luceneService.extractCompleteSearch("machine collection:\"Daily Reads\""); | ||
|
|
||
| assertThat(completeSearch.getKeyword()).isEqualTo("machine"); | ||
| assertThat(completeSearch.getCollectionIds()).containsExactly(42L); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
With
splitSearchTokenspreserving whitespace inside quoted phrases,collection:values can now include leading/trailing spaces from inside the quotes, and the laterfindByNameContainingIgnoreCase(collectionName)lookup may fail to match unexpectedly. Consider normalizing the extracted collection value before querying (e.g., trimming).Severity: low
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.