Sitecore Commerce: Elasticsearch driven Faceted Search/Navigation for eCommerce – Part 3

This post is 3rd in a series of posts about using elastic search for faceted search/navigation with Sitecore Commerce. In this post we will improve our index query developed before to include aggregation and filtering by facets.

Here are the links to other posts in this series.
Part 1: Facet Search/Navigation Basics & Example + Setting up elasticsearch and kibana
Part 2: Creating the Index and Defining the data model

Aggregation: The elasticsearch aggregations frameworks provides aggregated data based on a search query. Here is a detailed description of Aggregations in elastic search.

Filtering: This feature is an obvious one and allows filtering of content based on specified filters. Each filter results in a true/false answer which controls whether a document is included in the result set or not. Here is a detailed description of Filters feature in elastic search.

Updated Search Query Here is our search query updated to reflect aggregation and filter clauses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
GET /productitems/_search
{
  "query":{
    "bool":{
      "must":{
        "multi_match":{
          "query": "Ventura Jacket",
          "type" : "best_fields",
          "fields": ["variant_name", "variant_primary_color"]
        }
      },
      "filter":{
        "bool": {
          "must": [
            {"terms": {"product_brand.keyword" :["The North Face"]}},
            {"terms": {"product_activity.keyword" :["hiking"]}}
          ]
        }
      }
    }
  },
  "collapse":{
    "field" : "product_id.keyword",
    "inner_hits": {
      "name" : "matching_variants",
      "size": 1
    }
  },
  "aggs":{
    "brand":{
      "terms": {"field": "product_brand.keyword", "size":10 }
    },
    "variant_primary_color":{
      "terms": {"field": "variant_primary_color.keyword", "size":10 }
    },
    "product_activity":{
      "terms": {"field": "product_activity.keyword", "size":10 }
    }
  },
 
 "explain": false
}

Query Results

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
{
  "took": 67,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 0.60858977,
    "hits": [
      {
        "_index": "productitems",
        "_type": "_doc",
        "_id": "Y6IlmmYBoxRprfxd0bkf",
        "_score": 0.60858977,
        "_source": {
          "product_id": "NF0A2VD3",
          "product_name": "MEN'S VENTURE 2 JACKET",
          "product_image": "http://www.khopdi.com/nf0a2vd3_5pl_hero",
          "product_base_price": 99,
          "product_description": "lightweight, waterproof and windproof jacket that features a durable yet breathable ripstop exterior, Relaxed Fit and adjustable hood that leaves room for a helmet and extra layer",
          "product_brand": "The North Face",
          "product_category": "Men's|Jackets & Vests|Rainwear",
          "product_activity": "hiking",
          "variant_name": "Red Men's Venture 2 Jacket - Small",
          "variant_id": "5PLR-S",
          "variant_price": 99,
          "variant_primary_color": "Red",
          "variant_colors": [
            "RAGE RED",
            "FIG BROWN"
          ],
          "variant_size": "S",
          "variant_image": "http://www.khopdi.com/nf0a2vd3_5pl_hero"
        },
        "fields": {
          "product_id.keyword": [
            "NF0A2VD3"
          ]
        },
        "inner_hits": {
          "matching_variants": {
            "hits": {
              "total": 9,
              "max_score": 0.60858977,
              "hits": [
                {
                  "_index": "productitems",
                  "_type": "_doc",
                  "_id": "Y6IlmmYBoxRprfxd0bkf",
                  "_score": 0.60858977,
                  "_source": {
                    "product_id": "NF0A2VD3",
                    "product_name": "MEN'S VENTURE 2 JACKET",
                    "product_image": "http://www.khopdi.com/nf0a2vd3_5pl_hero",
                    "product_base_price": 99,
                    "product_description": "lightweight, waterproof and windproof jacket that features a durable yet breathable ripstop exterior, Relaxed Fit and adjustable hood that leaves room for a helmet and extra layer",
                    "product_brand": "The North Face",
                    "product_category": "Men's|Jackets & Vests|Rainwear",
                    "product_activity": "hiking",
                    "variant_name": "Red Men's Venture 2 Jacket - Small",
                    "variant_id": "5PLR-S",
                    "variant_price": 99,
                    "variant_primary_color": "Red",
                    "variant_colors": [
                      "RAGE RED",
                      "FIG BROWN"
                    ],
                    "variant_size": "S",
                    "variant_image": "http://www.khopdi.com/nf0a2vd3_5pl_hero"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  },
  "aggregations": {
    "product_activity": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "hiking",
          "doc_count": 9
        }
      ]
    },
    "variant_primary_color": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Blue",
          "doc_count": 3
        },
        {
          "key": "Red",
          "doc_count": 3
        },
        {
          "key": "Yellow",
          "doc_count": 3
        }
      ]
    },
    "brand": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "The North Face",
          "doc_count": 9
        }
      ]
    }
  }
}

Notice the new “aggregations” object in the result object with the facets (ex: variant_primary_color) and the corresponding buckets (Blue, Red, Yellow). Also, the results are filtered by our product_brand and product_activity filter clauses.

That’s it for this post. In the coming posts in this series we’ll build a bare-bones elasticesearch Provider for Sitecore 9 and start connecting the two. Fun stuff ahead, stay tuned.