POST http://localhost:9200/books/book/_search HTTP/1.1
User-Agent: Fiddler
content-type: application/json
Host: localhost:9200
Content-Length: 135
{
"query" : { "match_all" : {} },
"facets" : {
"author_facet" : {
"terms" : {
"field" : "author"
}
}
}
}
...i w odpowiedzi otrzymujemy:
{
"took":4,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":5,
"max_score":1.0,
"hits":[
(...)
]
},
"facets":{
"author_facet":{
"_type":"terms",
"missing":0,
"total":10,
"other":0,
"terms":[
{
"term":"michio",
"count":3
},
{
"term":"kaku",
"count":3
},
{
"term":"roger",
"count":2
},
{
"term":"penrose",
"count":2
}
]
}
}
}
Dane możemy także agregować definiując zakresy, na przykład
POST http://localhost:9200/books/book/_search HTTP/1.1
User-Agent: Fiddler
content-type: application/json
Host: localhost:9200
Content-Length: 246
{
"query" : { "match_all" : {} },
"facets" : {
"ranges_facet_result" : {
"range" : {
"field" : "year",
"ranges" : [
{ "to" : 1995 },
{ "from" : 1995, "to" : 2000 },
{ "from" : 2000 }
]
}
}
}
}
Zwrócone nam zostanie więcej informacji niż poprzednio, ponieważ agregujemy po polu typu numerycznego
{
"took":145,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":5,
"max_score":1.0,
"hits":[
]
},
"facets":{
"ranges_facet_result":{
"_type":"range",
"ranges":[
{
"to":1995.0,
"count":2,
"min":1989.0,
"max":1993.0,
"total_count":2,
"total":3982.0,
"mean":1991.0
},
{
"from":1995.0,
"to":2000.0,
"count":1,
"min":1995.0,
"max":1995.0,
"total_count":1,
"total":1995.0,
"mean":1995.0
},
{
"from":2000.0,
"count":2,
"min":2004.0,
"max":2006.0,
"total_count":2,
"total":4010.0,
"mean":2005.0
}
]
}
}
}
Powyższe zapytanie możemy także wykonać przy równomiernym rozłożeniu danych (tak jak w histogramie).
POST http://localhost:9200/books/book/_search HTTP/1.1
User-Agent: Fiddler
content-type: application/json
Host: localhost:9200
Content-Length: 156
{
"query" : { "match_all" : {} },
"facets" : {
"total_histogram" : {
"histogram" : {
"field" : "year",
"interval" : 5
}
}
}
}
Najwięcej danych do analizy dostarcza nam jednak zapytanie typu statistical.
POST http://localhost:9200/books/book/_search HTTP/1.1
User-Agent: Fiddler
content-type: application/json
Host: localhost:9200
Content-Length: 139
{
"query" : { "match_all" : {} },
"facets" : {
"statistical_test" : {
"statistical" : {
"field" : "price"
}
}
}
}
ElasticSearch zwraca między innymi zakres, średnią, wariancję, odchylenie standardowe i sumę kwadratów.
{
"took":55,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":5,
"max_score":1.0,
"hits":[(...)]
},
"facets":{
"statistical_test":{
"_type":"statistical",
"count":5,
"total":114.67999999999999,
"min":12.06,
"max":42.0,
"mean":22.936,
"sum_of_squares":3157.3112,
"variance":105.40214400000013,
"std_deviation":10.266554631423345
}
}
}
Brak komentarzy:
Prześlij komentarz