Skip to content

Commit

Permalink
Accept quotes for phrase matching in a query
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Jan 3, 2024
1 parent 25acc98 commit bd96522
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/quarkus/search/app/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ public SearchResult<GuideSearchHit> search(@RestQuery @DefaultValue(QuarkusVersi
.field(localizedField("fullContent_autocomplete", language)).boost(0.1f)
.matching(q)
// See: https://github.com/elastic/elasticsearch/issues/39905#issuecomment-471578025
// while the issue is about stopwords the same problem is observed for synonyms on search-analyzer side:
.flags(SimpleQueryFlag.AND, SimpleQueryFlag.OR)
// while the issue is about stopwords the same problem is observed for synonyms on search-analyzer side.
// we also add phrase flag so that entire phrases could be searched as well, e.g.: "hibernate search"
.flags(SimpleQueryFlag.AND, SimpleQueryFlag.OR, SimpleQueryFlag.PHRASE)
.defaultOperator(BooleanOperator.AND))
.should(f.match().field("origin").matching("quarkus").boost(50.0f))
.should(f.not(f.match().field(localizedField("topics", language)).matching("compatibility"))
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/quarkus/search/app/SearchServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,20 @@ void quoteEmptyQuoteTitleTranslation() {
"<span class=\"highlighted\">Duplicated</span> <span class=\"highlighted\">context</span>, <span class=\"highlighted\">context</span> <span class=\"highlighted\">locals</span>, <span class=\"highlighted\">asynchronous</span> <span class=\"highlighted\">processing</span> and <span class=\"highlighted\">propagation</span>");
}

@Test
void searchForPhrase() {
var result = given()
.queryParam("q", "\"asynchronous processing and propagation\"")
.when().get(GUIDES_SEARCH)
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
assertThat(result.hits()).extracting(GuideSearchHit::title)
.contains(
// unified highlighter will still "highlight" the phrase word by word:
"Duplicated context, context locals, <span class=\"highlighted\">asynchronous</span> <span class=\"highlighted\">processing</span> and <span class=\"highlighted\">propagation</span>");
}

private static ThrowingConsumer<String> hitsHaveCorrectWordHighlighted(AtomicInteger matches, String word,
String cssClass) {
return sentence -> {
Expand Down

0 comments on commit bd96522

Please sign in to comment.