Correct tag facets handling

This commit is contained in:
Stefan Hardegger
2025-07-28 14:37:58 +02:00
parent 860bf02d56
commit c08082c0d6
5 changed files with 114 additions and 21 deletions

View File

@@ -0,0 +1,31 @@
package com.storycove.dto;
public class FacetCountDto {
private String value;
private int count;
public FacetCountDto() {}
public FacetCountDto(String value, int count) {
this.value = value;
this.count = count;
}
// Getters and Setters
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}

View File

@@ -1,6 +1,7 @@
package com.storycove.dto;
import java.util.List;
import java.util.Map;
public class SearchResultDto<T> {
@@ -10,6 +11,7 @@ public class SearchResultDto<T> {
private int perPage;
private String query;
private long searchTimeMs;
private Map<String, List<FacetCountDto>> facets;
public SearchResultDto() {}
@@ -22,6 +24,16 @@ public class SearchResultDto<T> {
this.searchTimeMs = searchTimeMs;
}
public SearchResultDto(List<T> results, long totalHits, int page, int perPage, String query, long searchTimeMs, Map<String, List<FacetCountDto>> facets) {
this.results = results;
this.totalHits = totalHits;
this.page = page;
this.perPage = perPage;
this.query = query;
this.searchTimeMs = searchTimeMs;
this.facets = facets;
}
// Getters and Setters
public List<T> getResults() {
return results;
@@ -70,4 +82,12 @@ public class SearchResultDto<T> {
public void setSearchTimeMs(long searchTimeMs) {
this.searchTimeMs = searchTimeMs;
}
public Map<String, List<FacetCountDto>> getFacets() {
return facets;
}
public void setFacets(Map<String, List<FacetCountDto>> facets) {
this.facets = facets;
}
}

View File

@@ -1,6 +1,7 @@
package com.storycove.service;
import com.storycove.dto.AuthorSearchDto;
import com.storycove.dto.FacetCountDto;
import com.storycove.dto.SearchResultDto;
import com.storycove.dto.StorySearchDto;
import com.storycove.entity.Author;
@@ -227,6 +228,8 @@ public class TypesenseService {
.highlightFields("title,description")
.highlightStartTag("<mark>")
.highlightEndTag("</mark>")
.facetBy("tagNames,authorName,rating")
.maxFacetValues(100)
.sortBy(buildSortParameter(normalizedQuery, sortBy, sortDir));
// Add filters
@@ -268,6 +271,7 @@ public class TypesenseService {
List<StorySearchDto> results = convertSearchResult(searchResult);
Map<String, List<FacetCountDto>> facets = processFacetCounts(searchResult);
long searchTime = System.currentTimeMillis() - startTime;
return new SearchResultDto<>(
@@ -276,7 +280,8 @@ public class TypesenseService {
page,
perPage,
query,
searchTime
searchTime,
facets
);
} catch (Exception e) {
@@ -391,6 +396,45 @@ public class TypesenseService {
return document;
}
@SuppressWarnings("unchecked")
private Map<String, List<FacetCountDto>> processFacetCounts(SearchResult searchResult) {
Map<String, List<FacetCountDto>> facetMap = new HashMap<>();
if (searchResult.getFacetCounts() != null) {
for (FacetCounts facetCounts : searchResult.getFacetCounts()) {
String fieldName = facetCounts.getFieldName();
List<FacetCountDto> facetValues = new ArrayList<>();
if (facetCounts.getCounts() != null) {
for (Object countObj : facetCounts.getCounts()) {
if (countObj instanceof Map) {
Map<String, Object> countMap = (Map<String, Object>) countObj;
String value = (String) countMap.get("value");
Integer count = (Integer) countMap.get("count");
if (value != null && count != null && count > 0) {
facetValues.add(new FacetCountDto(value, count));
}
}
}
}
if (!facetValues.isEmpty()) {
// Sort by count descending, then by value ascending
facetValues.sort((a, b) -> {
int countCompare = Integer.compare(b.getCount(), a.getCount());
if (countCompare != 0) return countCompare;
return a.getValue().compareToIgnoreCase(b.getValue());
});
facetMap.put(fieldName, facetValues);
}
}
}
return facetMap;
}
@SuppressWarnings("unchecked")
private List<StorySearchDto> convertSearchResult(SearchResult searchResult) {
return searchResult.getHits().stream()