Skip to content

Commit

Permalink
"categories" filter in search
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Oct 27, 2023
1 parent 299d344 commit 094466b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/io/quarkus/search/app/SearchService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.search.app;

import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -33,6 +35,7 @@ public class SearchService {
@Operation(summary = "Search for any resource")
@Transactional
public SearchResult<SearchHit> search(@RestQuery @DefaultValue(QuarkusVersions.LATEST) String version,
@RestQuery List<String> categories,
@RestQuery String q,
@RestQuery @DefaultValue("0") int page) {
var result = session.search(Guide.class)
Expand All @@ -43,6 +46,10 @@ public SearchResult<SearchHit> search(@RestQuery @DefaultValue(QuarkusVersions.L

root.add(f.match().field("version").matching(version));

if (categories != null && !categories.isEmpty()) {
root.add(f.terms().field("categories").matchingAny(categories));
}

if (q != null && !q.isBlank()) {
root.add(f.bool().must(f.simpleQueryString()
.field("title").boost(10.0f)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/quarkus/search/app/entity/Guide.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class Guide {
// Using PathWrapper because of https://hibernate.atlassian.net/browse/HSEARCH-4988
public PathWrapper fullContentPath;

@KeywordField(name = "categories")
public Set<String> categories;

@FullTextField(name = "topics")
@KeywordField(name = "topics_faceting", searchable = Searchable.YES, projectable = Projectable.YES, aggregable = Aggregable.YES)
public Set<String> topics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private Guide parseGuide(GuidesDirectory guidesDirectory, Path path) {
Asciidoc.parse(path, title -> guide.title = title,
Map.of("summary", summary -> guide.summary = summary,
"keywords", keywords -> guide.keywords = keywords,
"categories", categories -> guide.categories = toSet(categories),
"topics", topics -> guide.topics = toSet(topics),
"extensions", extensions -> guide.extensions = toSet(extensions)));
return guide;
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/io/quarkus/search/app/SearchServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,17 @@ void version() {
.allSatisfy(hit -> assertThat(hit).extracting(SearchHit::id, InstanceOfAssertFactories.STRING)
.startsWith("/versions/main/guides/"));
}

@Test
void categories() {
var result = given()
.queryParam("q", "orm")
.queryParam("categories", "alt-languages")
.when().get()
.then()
.statusCode(200)
.extract().body().as(SEARCH_RESULT_SEARCH_HITS);
assertThat(result.hits()).extracting(SearchHit::id).containsExactlyInAnyOrder(
GuideIds.HIBERNATE_ORM_PANACHE_KOTLIN);
}
}

0 comments on commit 094466b

Please sign in to comment.