Here's an example of a facet query, complete with the results of 
post-processing it as a hierarchical combination facet (not yet available 
in ES 0.90.X). The script is used to provide the response in such a way 
that I can easily break it down into a hierarchical combination of 
arbitrary depth. In this example, it shows the top ten words used in cities 
in the USA and Puerto Rico by state.

First, the full query, including the facet. It was issued against 
approximately 26K documents within a type that contains all cities in the 
US and Puerto Rico (originally from the US Census). That's why the 
match_all with no additional filtering is used. The query was built using 
the Java API, and the JSON below was emitted by the Java API:

{
  "from" : 0,
  "size" : 50000,
  "timeout" : 60000,
  "query" : {
    "match_all" : { }
  },
  "version" : true,
  "explain" : false,
  "fields" : [ "_ttl", "_source" ],
  "facets" : {
    "state_city_combinations" : {
      "terms" : {
        "size" : 10,
        "script" : "doc['state'].value + \"~~~\" + doc['city'].value"
      }
    }
  }
}

The response. The "terms" JSON object is what ElasticSearch returned, while 
the "combinations" is what I calculated by processing what ElasticSearch 
returned. Just as a point of interest:
 
{
  "facets" : {
    "state_city_combinations" : {
      "_type" : "terms",
      "total" : 25376,
      "other" : 25123,
      "missing" : 0,
      "combinations" : {
        "fl" : {
          "beach" : 61,
          "lake" : 18,
          "citi" : 13
        },
        "mo" : {
          "citi" : 36
        },
        "tx" : {
          "citi" : 33
        },
        "il" : {
          "citi" : 22
        },
        "ny" : {
          "east" : 21
        },
        "mn" : {
          "lake" : 19
        },
        "wi" : {
          "lake" : 16
        },
        "ia" : {
          "citi" : 14
        }
      },
      "terms" : [ {
        "term" : "fl~~~beach",
        "count" : 61
      }, {
        "term" : "mo~~~citi",
        "count" : 36
      }, {
        "term" : "tx~~~citi",
        "count" : 33
      }, {
        "term" : "il~~~citi",
        "count" : 22
      }, {
        "term" : "ny~~~east",
        "count" : 21
      }, {
        "term" : "mn~~~lake",
        "count" : 19
      }, {
        "term" : "fl~~~lake",
        "count" : 18
      }, {
        "term" : "wi~~~lake",
        "count" : 16
      }, {
        "term" : "ia~~~citi",
        "count" : 14
      }, {
        "term" : "fl~~~citi",
        "count" : 13
      } ]
    }
  }
}



Brian

-- 
You received this message because you are subscribed to the Google Groups 
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/a4e90cd6-3785-498e-a1fe-a3b1acca2ba9%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to