use-case-shopping/src/main/java/ai/vespa/example/shopping/site/data/Categories.java
<reponame>jdagdelen/vespa-sample-apps<gh_stars>0// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package ai.vespa.example.shopping.site.data;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.Map;

public class Categories {

    static String query() {
        String yql = "select * from sources item where userQuery()";
        String grouping = "all( " +
                "group(array.at(categories,0)) each( " +
                    "output(count()) all(" +
                        "group(array.at(categories,1)) each( " +
                            "output(count()) all(" +
                                "group(array.at(categories,2)) each( " +
                                    "output(count())" +
                                ")" +
                            ")" +
                        ")" +
                    ")" +
                ")" +
            ")";

        yql += " | " + grouping;

        SimpleQueryBuilder query = new SimpleQueryBuilder("/search/");
        query.add("query", "sddocname:item");
        query.add("hits", "0");
        query.add("yql", yql);
        return query.toString();
    }

    public static Map<String, JsonNode> data(SimpleHttpClient client, Map<String, String> properties) {
        return Map.of("categories", client.getJson(query()));
    }

    public static String getCategoryName(String name) {
        return name.substring(name.lastIndexOf('|') + 1);
    }


}
