Python:ElasticSearch

De BrapciWiki
Ir para navegação Ir para pesquisar

Python:Extraindo informações URL

pip install elasticsearch

Conectando com o ElasticSearch

from datetime import datetime
from elasticsearch import Elasticsearch
class elasticsearch:
   def __init__(self) -> None:
       self.elasticsearch_url = 'http://143.54.112.91:9200'
   def connect(self):
       es = Elasticsearch(self.elasticsearch_url)
       print(es.info())
es = Elasticsearch(self.elasticsearch_url)
print(es.info())

Indexando um documento

from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
   'author': 'kimchy',
   'text': 'Elasticsearch: cool. bonsai cool.',
   'timestamp': datetime.now(),
}
resp = es.index(index="test-index", id=1, document=doc)
print(resp['result'])
resp = es.get(index="test-index", id=1)
print(resp['_source'])
es.indices.refresh(index="test-index")
resp = es.search(index="test-index", query={"match_all": {}})
print("Got %d Hits:" % resp['hits']['total']['value'])
for hit in resp['hits']['hits']:
   print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])