Skip to content

Instantly share code, notes, and snippets.

@themouette
Forked from clintongormley/terms.mkd
Last active December 15, 2015 07:09
Show Gist options
  • Save themouette/5221705 to your computer and use it in GitHub Desktop.
Save themouette/5221705 to your computer and use it in GitHub Desktop.

Declare child mapping:

curl -XPUT 'http://localhost:9200/test' -d '{
    "settings": {
        "analysis": {
            "analyzer": {
                "fullfields": {
                    "tokenizer" : "keyword",
                    "filter" : ["lowercase"],
                    "analyzer": "keyword"
                }
            }
        }
    },
    "mappings" : {
        "test": {
            "properties" : {
                "text" : {"type" : "string", "analyzer" : "fullfields"}
            }
        },
        "book" : {
            "_parent" : {
                "type" : "test"
            },
            "properties" : {
                "username" : {"type" : "string", "analyzer" : "fullfields"}
            }
        }
    }
}'

Index root docs:

curl -XPUT 'http://127.0.0.1:9200/test/test/1?pretty=1'  -d '
{
   "text" : "foo bar"
}
'

curl -XPUT 'http://127.0.0.1:9200/test/test/2?pretty=1'  -d '
{
   "text" : "baz"
}
'

Index children:

curl -XPUT 'http://127.0.0.1:9200/test/book/1?pretty=1&parent=1'  -d '
{
   "username": "foo"
}
'

curl -XPUT 'http://127.0.0.1:9200/test/book/2?pretty=1&parent=1'  -d '
{
   "username": "bar"
}
'

curl -XPUT 'http://127.0.0.1:9200/test/book/3?pretty=1&parent=2'  -d '
{
   "username": "baz"
}
'

Check the terms facet:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "facets" : {
      "text" : {
         "terms" : {
            "all_terms" : true,
            "size": 70,
            "field" : "text",
            "order": "term"
         }
      }
   },
   "size" : 0
}
'

# {                                                                                                                                         
#   "took" : 1,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 1,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [ ]
#   },
#   "facets" : {
#     "text" : {
#       "_type" : "terms",
#       "missing" : 0,
#       "total" : 2,
#       "other" : 0,
#       "terms" : [ {
#         "term" : "foo bar",
#         "count" : 1
#       }, {
#         "term" : "baz",
#         "count" : 1
#       } ]
#     }
#   }
# }

Delete one doc:

curl -XDELETE 'http://127.0.0.1:9200/test/test/_query?pretty=1' -d '{
    "term": {"text": "foo bar"}
}'

The terms no longer show up in the terms facet:

curl -XGET 'http://127.0.0.1:9200/test/test/_search?pretty=1'  -d '
{
   "facets" : {
      "text" : {
         "terms" : {
            "all_terms" : true,
            "size": 70,
            "field" : "text",
            "order": "term"
         }
      }
   },
   "size" : 0
}
'

# {
#   "took" : 1,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 1,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 1,
#     "max_score" : 1.0,
#     "hits" : [ ]
#   },
#   "facets" : {
#     "text" : {
#       "_type" : "terms",
#       "missing" : 0,
#       "total" : 1,
#       "other" : 0,
#       "terms" : [ {
#         "term" : "baz",
#         "count" : 1
#       } ]
#     }
#   }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment