nick78
nick78

Reputation: 261

Dynamic Querydsl Filter predicates depending on the search result

I developed a product search with Spring MVC and Spring Data. Users can search for products by name/description (Manual input. Search on product#name and product#description with "like") or by a category (Clickable link). When the result is displayed the users should be able to narrow down the result further with some filter predicates like price, size, color and so on.Therefore I implemented some Querydsl Predicates.

Let's say the user found red, green and yellow products then I want to show only these colors in my filter. I don't want to use static values and provide all possible colors that you can imagine.

I can't iterate over the whole result and put all available colors in a list since I am using Paging so I don't have the whole result set in the heap to iterate over.

Do I have to execute an additional sql query for every filter predicate on the result set? I thought about an additional table with pre-computed results. Well I could use this for the category search. But since Users can do a manual input search as well on product#name and product#description I can't forecast every possible search string...

The products are changing daily because I do several Spring Batch CSV imports every night (updating, deleting and adding products).

Any idea?

Thanks
Nick

Upvotes: 2

Views: 1446

Answers (1)

nick78
nick78

Reputation: 261

Solved it. Created a custom query with a predicate containing the search String or the category. If a filter attribut is set, I add it as an inner join to the query. In the filter I have a List for every attribute containing the valid values. E.g.

...
query.from(product).where(predicate);    
if(isColorFilterSet()) {    
    JPAQuery colorQuery = query.from(color).innerJoin(product.colors, color)
                  .groupBy(color);
    List<Color> colors = colorQuery.list(color);
    filter.setFilterableBaseColors(colors);
}

Upvotes: 1

Related Questions