jack-zheng
7/25/2018 - 2:19 AM

elasticsearch, quick start, docker

elasticsearch, quick start, docker

Quick Start

How to install

# pull image
docker pull docker.elastic.co/elasticsearch/elasticsearch:6.3.2

# start ES container
docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2

# access 9200 to ensure service up
curl -XGET 'localhost:9200'

CURD demo (curl)

# show all indexs
curl -XGET 'localhost:9200/_cat/indices?v'

# show all documents
curl -XGET 'localhost:9200/novels2/_search?pretty'

# show document by id
curl -XGET 'localhost:9200/novels2/authors/1?pretty'

# search document with wildcard
curl -XGET 'localhost:9200/novels2/_search?pretty' 
-d '{"query":{"wildcard":{"name":"ja*"}}}' 
-H 'Content-type:application/json'

Python Way

# create one record

In [5]: import json
In [7]: body = json.loads('{"name":"jack", "age":18}')
In [8]: from elasticsearch import Elasticsearch
In [9]: con = {"host":"localhost", "post":9200}
In [13]: con = [{"host": "localhost", "port":9200}]
In [14]: es = Elasticsearch(con)
In [17]: es.index(index='test', doc_type='people', id=1, body=body)
Out[17]:
{'_index': 'test',
 '_type': 'people',
 '_id': '1',
 '_version': 1,
 'result': 'created',
 '_shards': {'total': 2, 'successful': 1, 'failed': 0},
 '_seq_no': 0,
 '_primary_term': 1}
 
 # query a record
 In [18]: es.get(index='test', doc_type='people', id=1)
Out[18]:
{'_index': 'test',
 '_type': 'people',
 '_id': '1',
 '_version': 1,
 'found': True,
 '_source': {'name': 'jack', 'age': 18}}
 
 # search record with wildcard
 In [21]: es.search(index='test', body={"query": {"wildcard":{"name":"j*"}}})
Out[21]:
{'took': 48,
 'timed_out': False,
 '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
 'hits': {'total': 1,
  'max_score': 1.0,
  'hits': [{'_index': 'test',
    '_type': 'people',
    '_id': '1',
    '_score': 1.0,
    '_source': {'name': 'jack', 'age': 18}}]}}
    
  # some other search type like
  # prefix: search by rule as **start with**
  # match: something like exactly match

  • 使用 wildcard 做 match 的时候, 由于目标字符串中有大些字母, 就用大写的去匹配, 结果失败, 用小写的去匹配就可以工作, 看了下别人的评论, 应该是和什么analyzed 有关

docker compose command

Source

offical ES Website