Skip to content

Instantly share code, notes, and snippets.

@gma
Created August 27, 2014 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gma/caef19e6271aec0f9e56 to your computer and use it in GitHub Desktop.
Save gma/caef19e6271aec0f9e56 to your computer and use it in GitHub Desktop.
// The query that (in the Sense console) produced the contents of results.js
// GET planner_development/_search?search_type=count
{
"query": {
"terms": {
"tag_ngrams": [
"fo"
]
}
},
"aggregations": {
"popular_tags": {
"terms": {
"field": "tags"
}
}
}
}
// This file shows the output of a `"match_all": {}` query, so you can see the source data
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "planner_development",
"_type": "card",
"_id": "2",
"_score": 1,
"_source": {
"board": 138992,
"title": "Only tagged with foo",
"tags": [
"foo"
],
"tag_ngrams": [
"foo"
]
}
},
{
"_index": "planner_development",
"_type": "card",
"_id": "7",
"_score": 1,
"_source": {
"board": 138992,
"title": "Tagged with foo-bar",
"tags": [
"foo-bar"
],
"tag_ngrams": [
"foo-bar"
]
}
},
{
"_index": "planner_development",
"_type": "card",
"_id": "3",
"_score": 1,
"_source": {
"board": 138992,
"title": "Tagged with bar",
"tags": [
"bar"
],
"tag_ngrams": [
"bar"
]
}
},
{
"_index": "planner_development",
"_type": "card",
"_id": "5",
"_score": 1,
"_source": {
"board": 138992,
"title": "Tagged with foo and bar",
"tags": [
"foo",
"bar"
],
"tag_ngrams": [
"foo",
"bar"
]
}
}
]
}
}
// This file shows how I've setup indexing tags as ngrams
//
// curl http://localhost:9200/planner_development/_settings?pretty=true
{
"planner_development" : {
"settings" : {
"index" : {
"uuid" : "69yzbIUuSemtWCNrCzctKg",
"analysis" : {
"analyzer" : {
"autocomplete" : {
"type" : "custom",
"filter" : [ "lowercase", "autocomplete_filter" ],
"tokenizer" : "whitespace"
}
},
"filter" : {
"autocomplete_filter" : {
"min_gram" : "1",
"type" : "ngram",
"max_gram" : "20"
}
}
},
"number_of_replicas" : "1",
"number_of_shards" : "5",
"version" : {
"created" : "1020199"
}
}
}
}
}
// curl http://localhost:9200/planner_development/_mapping?pretty=true
{
"planner_development" : {
"mappings" : {
"card" : {
"include_in_all" : false,
"properties" : {
"board" : {
"type" : "long",
"include_in_all" : false
},
"tag_ngrams" : {
"type" : "string",
"index_analyzer" : "autocomplete",
"search_analyzer" : "whitespace",
"include_in_all" : false
},
"tags" : {
"type" : "string",
"analyzer" : "whitespace",
"include_in_all" : false
},
"title" : {
"type" : "string",
"analyzer" : "english",
"include_in_all" : false
}
}
}
}
}
}
// This is close to what I want, but I don't want "bar" to appear in the
// buckets. It's there because a document was tagged with "foo" and "bar",
// and the aggregation is returning all tags on documents that matched the
// ngram-based query for tags containing "fo".
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"popular_tags": {
"buckets": [
{
"key": "foo",
"doc_count": 2
},
{
"key": "bar",
"doc_count": 1
},
{
"key": "foo-bar",
"doc_count": 1
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment