elasticsearch, quick start, docker
# 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'
# 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'
# 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