Skip to content

Instantly share code, notes, and snippets.

@jeremyb
Forked from anonymous/documents.yaml
Last active October 5, 2020 15:54
Show Gist options
  • Save jeremyb/c6fc7c11b8a4b5967008 to your computer and use it in GitHub Desktop.
Save jeremyb/c6fc7c11b8a4b5967008 to your computer and use it in GitHub Desktop.
Hierarchical facets with Elasticsearch
_type: product
category_level0: Books
category_level1: Computers & Technology
category_level2: Network Programming
name: Pro AngularJS
---
_type: product
category_level0: Books
category_level1: Computers & Technology
category_level2: Online Searching
name: ElasticSearch Server Second Edition
---
_type: product
category_level0: Books
category_level1: Arts & Photography
category_level2: Photography & Video
name: Humans of New York
product:
properties:
category_level0:
type: string
index: not_analyzed
category_level1:
type: string
index: not_analyzed
category_level2:
type: string
index: not_analyzed
name:
type: string
#!/bin/bash
export ELASTICSEARCH_ENDPOINT="http://localhost:9200"
# Create indexes
curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
"mappings": {
"product": {
"properties": {
"category_level0": {
"type": "string",
"index": "not_analyzed"
},
"category_level1": {
"type": "string",
"index": "not_analyzed"
},
"category_level2": {
"type": "string",
"index": "not_analyzed"
},
"name": {
"type": "string"
}
}
}
}
}'
# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"product"}}
{"category_level0":"Books","category_level1":"Computers & Technology","category_level2":"Network Programming","name":"Pro AngularJS"}
{"index":{"_index":"play","_type":"product"}}
{"category_level0":"Books","category_level1":"Computers & Technology","category_level2":"Online Searching","name":"ElasticSearch Server Second Edition"}
{"index":{"_index":"play","_type":"product"}}
{"category_level0":"Books","category_level1":"Arts & Photography","category_level2":"Photography & Video","name":"Humans of New York"}
'
# Do searches
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
"size": 0,
"aggregations": {
"category": {
"terms": {
"field": "category_level0"
},
"aggs": {
"level1": {
"terms": {
"field": "category_level1"
},
"aggs": {
"level2": {
"terms": {
"field": "category_level2"
}
}
}
}
}
}
}
}
'
# Auto generated by Found's Play-tool at 2014-11-13T09:48:45+01:00
version: 0
title: Hierarchical facets with Elasticsearch
description: ""
aggregations:
category:
buckets:
-
key: "Books"
doc_count: 3
level1:
buckets:
-
key: "Computers & Technology"
doc_count: 2
level2:
buckets:
-
key: "Network Programming"
doc_count: 1
-
key: "Online Searching"
doc_count: 1
-
key: "Arts & Photography"
doc_count: 1
level2:
buckets:
-
key: "Photography & Video"
doc_count: 1
size: 0
aggregations:
category:
terms:
field: category_level0
aggs:
level1:
terms:
field: category_level1
aggs:
level2:
terms:
field: category_level2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment