Skip to content

Instantly share code, notes, and snippets.

@jeroavf
Last active November 5, 2018 16:32
Show Gist options
  • Save jeroavf/5261c3878e76f07888883dcf039b0f88 to your computer and use it in GitHub Desktop.
Save jeroavf/5261c3878e76f07888883dcf039b0f88 to your computer and use it in GitHub Desktop.
Acesso a ElasticSeach via python
# coding: utf-8
# #### Teste de conexão ao elasticsearch via python
#
# Ativar o docker container do elasticsearch :
# docker pull docker.elastic.co/elasticsearch/elasticsearch:6.4.2
# docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.4.2
#
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'bot': 'sgpbot' ,
'pergunta': 'ola' ,
'resposta': 'como vai' ,
'intencao': 'saudar' ,
'confianca': '0.61' ,
'timestamp': datetime.now()
}
res = es.index(index="test-index" , doc_type = 'tweet' , id = 5 , body = doc)
print(res['result'])
doc = {
'bot': 'sgpbot' ,
'pergunta': 'prazo das ferias maximo' ,
'resposta': '30 dias' ,
'intencao': 'prazo-ferias' ,
'confianca': '0.91' ,
'timestamp': datetime.now()
}
res = es.index(index="test-index" , doc_type = 'tweet' , id = 6 , body = doc)
print(res['result'])
res = es.get(index="test-index", doc_type='tweet', id=3)
print(res[ '_source' ])
es.indices.refresh(index="test-index")
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(bot)s: %(pergunta)s" % hit["_source"])
#Importa o elasticsearch-dsl
from elasticsearch_dsl import Search
client = Elasticsearch()
s = Search( using=client , index = "text-index") .filter("term" , category="pergunta") .query("match",title = "ferias" )
response = s.execute
print(response)
for hit in response:
print(hit)
#### melhorar depois
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment