Skip to content

Perform the equivalent of select distinct on a field in a course

ianibo edited this page Feb 13, 2013 · 6 revisions

There is an example in the repository at

https://github.com/k-int/XCRI-Aggregator/blob/develop/toolkits/rest/qualtype_facet.sh

Which lists all qual.type values (Including records with none). Roughly the idea is to post a JSON document into the web api that looks like

  curl -X POST "http://coursedata.k-int.com:9200/courses/_search?pretty=true" -d '
    {
      "from" : 0,
      "size" : 0,
      "query" : { "query_string" : {"query" : "*"} },
      "facets" : {
        "tags" : { "terms" : {"field" : "qual.level", "all_terms":true} }
      }
    }
  '

This says please send the following block of JSON to the search service, and pretty print the results

the JSON itself says

  • Don't bother giving me any actual records (from, size)
  • Run this "Distinct" over all documents (query:*)
  • Please tell me the different values for the field "qual.level" and make sure you return all the distinct values, not just the first ten (all_terms:true)

The response should look like

  ibbo@crick:~/dev/XCRI-Aggregator/toolkits/rest$ ./qualtype_facet.sh 
  {
    "took" : 1,
    "timed_out" : false,
    "_shards" : {
      "total" : 5,
      "successful" : 5,
      "failed" : 0
    },  
    "hits" : {
      "total" : 3080,
      "max_score" : 1.0,
      "hits" : [ ]
    },
    "facets" : {
      "tags" : {
        "_type" : "terms",
        "missing" : 0,
        "total" : 3080,
        "other" : 0,
        "terms" : [ {
          "term" : "",
          "count" : 2656
        }, {
          "term" : "Postgraduate",
          "count" : 212
        }, {
          "term" : "7",
          "count" : 108
        }, {
          "term" : "6",
          "count" : 95
        }, {
          "term" : "5",
          "count" : 5
        }, {
          "term" : "4",
          "count" : 4
        } ]
      }
    }
  }

Which says there are 3080 total docs, with 2656 documents having no qual.level (Marked as public remember), 212 at "Postgrad", 108 at level 7, etc, etc.