(search) Adjust query parsing to trim tokens in quoted search terms

Quoted search queries that contained keywords with possessive 's endings were not returning any results, as the index does not retain that suffix, and the query parser was not stripping it away in this code path.

This solves issue #143.
This commit is contained in:
Viktor Lofgren 2025-01-05 23:33:09 +01:00
parent dcad0d7863
commit 3e66767af3
2 changed files with 22 additions and 0 deletions

View File

@ -71,6 +71,17 @@ public class QueryFactory {
String[] parts = StringUtils.split(str, '_'); String[] parts = StringUtils.split(str, '_');
// Trim down tokens to match the behavior of the tokenizer used in indexing
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
if (part.endsWith("'s") && part.length() > 2) {
part = part.substring(0, part.length()-2);
}
parts[i] = part;
}
if (parts.length > 1) { if (parts.length > 1) {
// Require that the terms appear in sequence // Require that the terms appear in sequence
queryBuilder.phraseConstraint(SearchPhraseConstraint.mandatory(parts)); queryBuilder.phraseConstraint(SearchPhraseConstraint.mandatory(parts));

View File

@ -208,6 +208,17 @@ public class QueryFactoryTest {
System.out.println(subquery); System.out.println(subquery);
} }
@Test
public void testQuotedApostrophe() {
var subquery = parseAndGetSpecs("\"bob's cars\"");
System.out.println(subquery);
Assertions.assertTrue(subquery.query.compiledQuery.contains(" bob "));
Assertions.assertFalse(subquery.query.compiledQuery.contains(" bob's "));
Assertions.assertEquals("\"bob's cars\"", subquery.humanQuery);
}
@Test @Test
public void testExpansion9() { public void testExpansion9() {
var subquery = parseAndGetSpecs("pie recipe"); var subquery = parseAndGetSpecs("pie recipe");