# Python에서 Elastic Search 사용하기
# AWS Elasticsearch Service 연결하기
AWS Elasticsearch service의 Access 정책을 IAM으로 설정한다. 그리고 필요한 패키지를 다운 받는다.
pip install elasticsearch
pip install requests
pip install requests-aws4auth
1
2
3
2
3
아래 코드를 통해 Elasticsearch 에 연결할수있다.
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import os
AWS_ACCESS_KEY = os.environ['AWS_ACCESS_KEY']
AWS_SECRET_KEY = os.environ['AWS_SECRET_KEY']
region = 'ap-northeast-2'
service = 'es'
awsauth = AWS4Auth(AWS_ACCESS_KEY, AWS_SECRET_KEY, region, service)
host = os.environ['AWS_ES_HOST']
# ex) tojung.search.net
es = Elasticsearch(
hosts = [{'host': host, 'port': 443}],
http_auth = awsauth,
use_ssl = True,
verify_certs = True,
connection_class = RequestsHttpConnection
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Document 조회
GET /itisindex/itistype/itisid
// shell
curl -XGET https://search.example.net:9999/itisindex/itistype/100
1
2
3
2
3
// python
es.get(index='itisindex', doc_type='itistype', id='100')
1
2
3
2
3
# 검색 API
GET /_search
//shell
curl -XGET https://search.example.net:9999/_search
{
"query": { ... }
}
1
2
3
4
5
6
2
3
4
5
6
//python
es.search(body={
"query": {
...
}
})
# // example1
# es.search(body={
# "query": {
# "match": {
# "col1": "hello"
# }
# }
# })
# // example2
# es.search(body={
# "query": {
# "wildcard": {
# "col1": "*elo*"
# }
# }
# })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Index 추가
PUT /mymyindex
// shell
curl -XPUT https://search.example.net:9999/mymyindex
{
"settings": { ... },
"mappings": { ... }
}
1
2
3
4
5
6
7
2
3
4
5
6
7
// python
es.indices.create(index='mymyindex', body={
"settings": { ... },
"mappings": { ... }
})
1
2
3
4
5
6
2
3
4
5
6
# Document 추가
PUT /mymyindex/mymytype/myid
//shell
curl -XPUT https://search.example.net:9999/mymyindex/mymytype/105
{
"col1": "hello world",
"itisfield": "hihi"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
//python
es.index(index='mymyindex', doc_type='mymytype', id='105', body={
"col1": "hello world",
"itisfield": "hihi"
})
1
2
3
4
5
6
2
3
4
5
6
# Bulk Insert
import elasticsearch
doc1 = {
'_index': 'mymyindex',
'_type': 'mymytype',
'_id': '123',
'_source': {
'field1': 'hello world!! ',
'hello': 'hello world!! dd'
}
}
doc2 = {
'_index': 'mymyindex',
'_type': 'mymytype',
'_id': '1234',
'_source': {
'field1': '22 hello world!! ',
'hello': '22 hello world!! dd'
}
}
doc3 = {
'_index': 'mymyindex',
'_type': 'mymytype',
'_id': '456',
'_source': {
'field1': '33 hello world!! ',
'hello': '33 hello world!! dd'
}
}
elasticsearch.helpers.bulk(es, [doc1, doc2, doc3])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# RAW REST API 호출
perform_request
elasticsearch-py 라이브러리에 원하는 기능이 없다면 perform_request()를 이용하는 것이 편하다.
es.transport.perform_request(
method=" ... ", # ex) "PUT", "GET" , ..
url=" ... ", # ex) /myindex/mytype/1000
body={
...
}
)
1
2
3
4
5
6
7
2
3
4
5
6
7
# Reference
Tags:
elasticsearch
파이썬