Mais conteúdo relacionado Semelhante a Busca e análise de dados escalável em tempo-real (20) Busca e análise de dados escalável em tempo-real1. BUSCA E ANÁLISE DE DADOS ESCALÁVEL EM
TEMPO-REAL
Aécio Santos
@aeciosan
aecio.solando@gmail.com
2. Como fazer busca textual e análise de dados em tempo-real em
grandes bases dados?
4. BUSCA E ANÁLISE DE DADOS ESCALÁVEL EM
TEMPO-REAL
Busca por termos, intervalos numéricos, datas, posicão
geográfica, relevância, etc
Execução consultas ad-hoc, agregação e visualização de dados
Busca distribuída com replicação e sharding automáticos
Near real-time, latência pequena, respostas em poucos ms
7. INDEXAÇÃO DE DOCUMENTOS
API
PUT /{index}/{type}/{?id}
Exemplos
$ curl XPUT
'http://localhost:9200/twitter/tweet/1' d
'{
"user" : "aeciosan",
"post_date" : "20140828T14:
00:00",
"message" : "Testando o ElasticSearch"
}'
$ curl XPUT
'http://localhost:9200/twitter/user/aeciosan' d
'{
"user" : "aeciosan",
"bio" : "Software Engineer at Zunnit Technologies"
}
8. RECUPERAÇÃO DE DOCUMENTOS
API
GET /{index}/{type}/{id}
Exemplo
curl XGET
'http://localhost:9200/twitter/tweet/1'
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "1",
"_version" : 1,
"found": true,
"_source" : {
"user" : "aeciosan",
"post_date" : "20140828T14:
00:00",
"message" : "Testando o ElasticSearch"
}
}'
9. BUSCA
API
GET /{index}/_search
GET /{index}/{type}/_search
Exemplos
$ curl XGET
'http://localhost:9200/twitter/_search?q=user:aeciosan'
$ curl XGET
'http://localhost:9200/twitter/tweet,user/_search?q=user:aeciosan'
$ curl XGET
'http://localhost:9200/_all/tweet/_search?q=message:elasticsearch'
$ curl XPOST
'http://localhost:9200/twitter/tweet/_search' d
'{
"query" : {
"term" : { "message" : "elasticsearch" }
}
}'
10. BUSCA
Vários de tipos de queries e filtros disponíveis
bool
fuzzy
geoshape
more like this
range
regexp
wildcard
...
11. BUSCA
Resposta
{
[...]
"hits": [
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_score": 0.15342641,
"_source": {
"user": "aeciosan",
"post_date": "20140828T14:
00:00",
"message": "Testando o ElasticSearch"
}
},
{
"_index": "twitter",
"_type": "tweet",
"_id": "2",
"_score": 0.11506981,
"_source": {
"user": "aeciosan",
"post_date": "20140828T14:
01:00",
"message": "Apresentação sobre ElasticSearch no BHTEC
Talks"
}
}
]
}
13. EXEMPLO: DADOS
698 documentos: dep_federal
POST /eleicoes2014/dep_federal
{
"partido": "PARTIDO",
"nome": "NOME",
"numero": 9999,
"coligacao": "PARTIDO1 / PARTIDO2 / PARTIDO3"
}
1200 documentos: dep_estadual
POST /eleicoes2014/dep_estadual
{
"partido": "PARTIDO",
"nome": "NOME",
"numero": 99999,
"coligacao": "PARTIDO1 / PARTIDO2 / PARTIDO3"
}
14. EXEMPLO 1
POST /eleicoes2014/_search?q=nome:joao
{
"size": 2,
"aggregations": {
"tipo_de_candidato": {
"terms": {
"field": "_type"
}
}
}
}
15. EXEMPLO 1: RESULTADO
{
[...],
"aggregations": {
"tipo_de_candidato": {
"buckets": [
{
"key": "dep_estadual",
"doc_count": 6
},
{
"key": "dep_federal",
"doc_count": 2
}
]
}
}
}
16. EXEMPLO 2
POST /eleicoes2014/_search
{
"size": 0,
"aggregations": {
"tipo_de_candidato": {
"terms": {
"field": "_type"
},
"aggregations": {
"por_partido": {
"terms": {
"field": "partido",
"size":2
}
},
"por_coligacao": {
"terms": {
"field": "coligacao",
"size":2
}
},
"numero_stats": {
"stats": {
"field": "numero"
}
}
}
}
}
}
17. EXEMPLO 2: RESULTADO
{
[...]
"aggregations": {
"tipo_de_candidato": {
"buckets": [
{
"key": "dep_estadual",
"doc_count": 1200,
"numero_stats": {
"count": 1200,
"min": 10000,
"max": 90999,
"avg": 36122.61666666667,
"sum": 43347140
},
"por_partido": {
"buckets": [
{
"key": "PT do B",
"doc_count": 85
},
{
"key": "PC do B",
"doc_count": 41
}
]
},
"por_coligacao": {
"buckets": [
{