Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
#40 elasticsearch controller class added
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemisDicoTiar committed Oct 5, 2021
1 parent bc76798 commit 76d943d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ zipp==3.5.0

datasets~=1.11.0
mysqlclient~=2.0.3
scikit-learn~=0.24.2
scikit-learn~=0.24.2
wandb~=0.12.2
elasticsearch~=7.15.0
Empty file.
58 changes: 58 additions & 0 deletions storyteller/elasticsearch/elastic_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from elasticsearch import Elasticsearch


class ElasticController:
def __init__(self,
cloud_id: str,
user_id: str,
user_pw: str,
index_name: str):
self.elastic = Elasticsearch(cloud_id, http_auth={user_id, user_pw})
self.index = index_name

def read(self,
query: dict,
highlight: dict = None):
"""
:param query:
:param highlight:
'''
>>> # Example input parameter
>>> query = {
>>> 'match_phrase': {
>>> 'eg': {
>>> 'query': query,
>>> 'analyzer': 'nori'
>>> }
>>> }
>>> }
>>> highlight = {
>>> 'fields': {
>>> 'eg': {
>>> 'type': 'plain',
>>> 'fragment_size': 15,
>>> 'number_of_fragments': 2,
>>> 'fragmenter': 'span'
>>> }
>>> }
>>> }
'''
:return:
"""
return self.elastic.search(index=self.index, query=query, highlight=highlight)

def write(self,
info,
bulk: bool):
"""
:param bulk:
:param info:
:return:
"""

if bulk:
# TODO: Bulk upload 구현.
self.elastic.bulk(index=self.index, doc_type='_doc')
else:
return self.elastic.index(index=self.index, doc_type='_doc', document=info)

38 changes: 20 additions & 18 deletions storyteller/examples/elasticsearch/explore_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,39 @@


# print(es)
#<Elasticsearch([{'host': 'storyteller.es.asia-northeast3.gcp.elastic-cloud.com', \
# <Elasticsearch([{'host': 'storyteller.es.asia-northeast3.gcp.elastic-cloud.com', \
# 'port': 9243, 'use_ssl': True}])>

def searchAPI(es : Elasticsearch,
index : str,
query : str):
def searchAPI(es: Elasticsearch,
index: str,
query: str):
body = {
'match_phrase': {
'eg': {
'query': query,
'analyzer': 'nori'
}
'match_phrase': {
'eg': {
'query': query,
'analyzer': 'nori'
}
}
}
highlight = {
'fields': {
'eg': {
'type': 'plain',
'fragment_size': 15,
'number_of_fragments': 2,
'fragmenter': 'span'
}
'fields': {
'eg': {
'type': 'plain',
'fragment_size': 15,
'number_of_fragments': 2,
'fragmenter': 'span'
}
}
}
res = es.search(index=index, query=body, highlight=highlight)
return res


def main():
query = '산 넘어 산'
cloud_id = "https://storyteller.es.asia-northeast3.gcp.elastic-cloud.com:9243/"
es = Elasticsearch(cloud_id,
http_auth=("teang1995", ELASTICSEARCH_PASSWORD))
es = Elasticsearch(cloud_id,
http_auth=("teang1995", ELASTICSEARCH_PASSWORD))

res = searchAPI(es, 'wisdom_test', query)
print(res)
Expand All @@ -47,5 +48,6 @@ def main():
'_id': '4-hBMHwBo8ESdJpAdGPQ', '_score': 1.0, '_source': {'wisdom': '산 넘어 산', 'eg': '텐트나 담요 등 구호물자를 노린 약탈이 잇따르고 있다.'}}]}}
'''


if __name__ == "__main__":
main()

0 comments on commit 76d943d

Please sign in to comment.