From e64811c3f60f805e995d1a1f914fceb06049b872 Mon Sep 17 00:00:00 2001 From: Raffael Tancman Date: Tue, 27 Oct 2020 20:52:07 -0300 Subject: [PATCH] Add kibana and python examples --- docker-compose.yml | 41 +++++++++++++++++++++++++ kibana/README.md | 5 +++ kibana/example1.md | 3 ++ kibana/example2.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++ python/README.md | 7 +++++ python/example1.py | 21 +++++++++++++ python/example2.py | 0 7 files changed, 153 insertions(+) create mode 100644 docker-compose.yml create mode 100644 kibana/README.md create mode 100644 kibana/example1.md create mode 100644 kibana/example2.md create mode 100644 python/README.md create mode 100644 python/example1.py create mode 100644 python/example2.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5f944aa --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +version: '2.2' +services: + es: + image: docker.elastic.co/elasticsearch/elasticsearch:7.9.0 + container_name: es + environment: + - node.name=es + - cluster.name=es-docker-cluster + - discovery.seed_hosts=es + - cluster.initial_master_nodes=es + - bootstrap.memory_lock=true + - "ES_JAVA_OPTS=-Xms512m -Xmx512m" + ulimits: + memlock: + soft: -1 + hard: -1 + volumes: + - es_data:/usr/share/elasticsearch/data + ports: + - 9200:9200 + networks: + - elastic + + kibana: + image: docker.elastic.co/kibana/kibana:7.9.0 + container_name: kibana + ports: + - 5601:5601 + environment: + ELASTICSEARCH_URL: http://es:9200 + ELASTICSEARCH_HOSTS: http://es:9200 + networks: + - elastic + +volumes: + es_data: + driver: local + +networks: + elastic: + driver: bridge \ No newline at end of file diff --git a/kibana/README.md b/kibana/README.md new file mode 100644 index 0000000..548c410 --- /dev/null +++ b/kibana/README.md @@ -0,0 +1,5 @@ +# elasticsearch-sandbox kibana + + +Vamos utilizar o kibana para rodar todos os comandos de criação de indices, para criar os documentos e buscar os mesmos. + diff --git a/kibana/example1.md b/kibana/example1.md new file mode 100644 index 0000000..bb11376 --- /dev/null +++ b/kibana/example1.md @@ -0,0 +1,3 @@ +# elasticsearch-sandbox kibana example 1 + +Vamos seguir os passos do artigo https://www.rtancman.com.br/information-retrieval/elasticsearch-como-ferramenta-de-busca.html#executando-comandos-do-elasticsearch \ No newline at end of file diff --git a/kibana/example2.md b/kibana/example2.md new file mode 100644 index 0000000..f83d45e --- /dev/null +++ b/kibana/example2.md @@ -0,0 +1,76 @@ +# elasticsearch-sandbox kibana example 2 + +Vamos ver na prática como os analyzers e os tokenizers funcionam. + +``` +PUT /example2 +{ + "settings": { + "index": { + "analysis": { + "filter": { + "synonym": { + "type": "synonym", + "lenient": true, + "synonyms": [ + "i-pod, i pod => ipod", + "universe, cosmos" + ] + } + }, + "analyzer": { + "default": { + "filter": [ + "asciifolding", + "lowercase", + "stop", + "synonym" + ], + "tokenizer": "standard" + } + } + } + } + } +} + +GET /_analyze +{ + "tokenizer": "standard", + "filter": [ "stop" ], + "text": "a quick fox jumps over the lazy dog" +} + +POST /example2/_analyze +{ + "analyzer": "default", + "text": "Este é um exemplo de texto que eu gostaria de indexar." +} + +POST /example2/_analyze +{ + "analyzer": "default", + "text": "i pod e cosmos são coisas bem loucas!" +} + + +POST /example2/_doc +{ + "title":"Este é um exemplo de texto que eu gostaria de indexar." +} + +POST /example2/_doc +{ + "title":"exemplo para comprar um i pod." +} + +GET /example2/_search +{ + "query": { + "query_string": { + "query":"exemplo de texto", + "analyzer": "default" + } + } +} +``` \ No newline at end of file diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..80a2af0 --- /dev/null +++ b/python/README.md @@ -0,0 +1,7 @@ +# elasticsearch-sandbox python + + +Vamos utilizar o cliente python oficial do elasticsearch que é o [elastic/elasticsearch-py](https://github.com/elastic/elasticsearch-py). + +O link da documentação é o [https://elasticsearch-py.readthedocs.io/en/master/index.html](https://elasticsearch-py.readthedocs.io/en/master/index.html). + diff --git a/python/example1.py b/python/example1.py new file mode 100644 index 0000000..23b3cfe --- /dev/null +++ b/python/example1.py @@ -0,0 +1,21 @@ +from datetime import datetime +from elasticsearch import Elasticsearch +es = Elasticsearch() + +doc = { + 'author': 'kimchy', + 'text': 'Elasticsearch: cool. bonsai cool.', + 'timestamp': datetime.now(), +} +res = es.index(index="test-index", id=1, body=doc) +print(res['result']) + +res = es.get(index="test-index", id=1) +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']['value']) +for hit in res['hits']['hits']: + print("%(timestamp)s %(author)s: %(text)s" % hit["_source"]) \ No newline at end of file diff --git a/python/example2.py b/python/example2.py new file mode 100644 index 0000000..e69de29