Skip to content

Commit

Permalink
Merge pull request #43 from yrodiere/and-by-default
Browse files Browse the repository at this point in the history
Use the AND operator by default for multi-term queries
  • Loading branch information
yrodiere authored Nov 10, 2023
2 parents dcc3904 + 8da3dc3 commit 16cb34f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/io/quarkus/search/app/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.eclipse.microprofile.openapi.annotations.Operation;

import org.hibernate.search.engine.search.common.BooleanOperator;
import org.hibernate.search.mapper.orm.session.SearchSession;

import org.jboss.resteasy.reactive.RestQuery;
Expand Down Expand Up @@ -61,7 +62,8 @@ public SearchResult<SearchHit> search(@RestQuery @DefaultValue(QuarkusVersions.L
.field("title_autocomplete").boost(1.0f)
.field("summary_autocomplete").boost(0.5f)
.field("fullContent_autocomplete").boost(0.1f)
.matching(q))
.matching(q)
.defaultOperator(BooleanOperator.AND))
.should(f.not(f.match().field("topics").matching("compatibility"))
.boost(50.0f)));
}
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/io/quarkus/search/app/SearchServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ void queryMatchingPrefixTerm() {
assertThat(result.total()).isEqualTo(8);
}

@Test
void queryMatchingTwoTerms() {
var result = search("orm elasticsearch");
// We expect an AND by default
assertThat(result.hits()).extracting(SearchHit::id)
.containsExactlyInAnyOrder(GuideIds.HIBERNATE_SEARCH_ORM_ELASTICSEARCH);
assertThat(result.total()).isEqualTo(1);
}

@Test
void queryEmptyString() {
var result = search("");
Expand Down

0 comments on commit 16cb34f

Please sign in to comment.