-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.client.get_client instead of .elastic.get_ingest_client
- Loading branch information
Showing
8 changed files
with
115 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from . import OPENSEARCH | ||
from .logging import logger | ||
|
||
import os | ||
|
||
|
||
if OPENSEARCH: | ||
from opensearchpy import OpenSearch | ||
else: | ||
from elasticsearch import Elasticsearch | ||
|
||
|
||
def get_client(index_server_baseurl: str = ""): | ||
"""index client for query or ingest | ||
either OpenSearch or Elasticsearch client, depending on OPENSEARCH env var | ||
""" | ||
|
||
raw_addr = index_server_baseurl or os.environ.get("INDEX_SERVER", "") | ||
use_ssl = bool(int(os.environ.get("INDEX_USE_SSL", "0"))) | ||
addresses = [x for x in raw_addr.split(",") if x.strip()] | ||
if not addresses: | ||
addresses.append("127.0.0.1:9200") | ||
|
||
# TODO: more auth options (cert, bearer token, api-key, etc) | ||
auth = ( | ||
os.environ.get("INDEX_LOGIN", "admin"), | ||
os.environ.get("INDEX_PASSWORD", "admin"), | ||
) | ||
|
||
if OPENSEARCH: | ||
logger.info(f"Use OpenSearch client at {addresses}") | ||
hosts = [] | ||
for address in addresses: | ||
host, port = address.rsplit(":", 1) | ||
hosts.append({"host": host, "port": port}) | ||
client = OpenSearch( | ||
hosts=hosts, | ||
http_auth=auth, | ||
use_ssl=use_ssl, | ||
verify_certs=False, | ||
) | ||
info = client.info() | ||
logger.info(f"OpenSearch client info: {info}") | ||
else: | ||
logger.info(f"Use ElasticSearch client at {addresses}") | ||
client = Elasticsearch( | ||
addresses, | ||
use_ssl=use_ssl, | ||
basic_auth=auth, | ||
verify_certs=False, | ||
) | ||
return client | ||
|
||
|
||
def get_ingest_client(elasticsearch_server_baseurl=None): | ||
logger.warn("get_client is deprecated, use get_search_client instead") | ||
return get_client(elasticsearch_server_baseurl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,8 @@ | ||
from . import OPENSEARCH | ||
from .client import get_client | ||
from .logging import logger | ||
|
||
import os | ||
|
||
|
||
if OPENSEARCH: | ||
from opensearchpy import OpenSearch | ||
else: | ||
from elasticsearch import Elasticsearch | ||
|
||
|
||
def get_search_client(elasticsearch_server_baseurl: str = ""): | ||
"""search client for query or ingest""" | ||
|
||
raw_addr = elasticsearch_server_baseurl or os.environ.get("INDEX_SERVER", "") | ||
use_ssl = bool(int(os.environ.get("INDEX_USE_SSL", "0"))) | ||
addresses = [x for x in raw_addr.split(",") if x.strip()] | ||
if not addresses: | ||
addresses.append("127.0.0.1:9200") | ||
|
||
# TODO: more auth options (cert, bearer token, api-key, etc) | ||
auth = ( | ||
os.environ.get("INDEX_LOGIN", "admin"), | ||
os.environ.get("INDEX_PASSWORD", "admin"), | ||
) | ||
|
||
if OPENSEARCH: | ||
logger.info(f"Use OpenSearch client at {addresses}") | ||
hosts = [] | ||
for address in addresses: | ||
host, port = address.rsplit(":", 1) | ||
hosts.append({"host": host, "port": port}) | ||
client = OpenSearch( | ||
hosts=hosts, | ||
http_auth=auth, | ||
use_ssl=use_ssl, | ||
verify_certs=False, | ||
) | ||
info = client.info() | ||
logger.info(f"OpenSearch client info: {info}") | ||
else: | ||
logger.info(f"Use ElasticSearch client at {addresses}") | ||
client = Elasticsearch( | ||
addresses, | ||
use_ssl=use_ssl, | ||
basic_auth=auth, | ||
verify_certs=False, | ||
) | ||
return client | ||
|
||
|
||
def get_inDEX_client(elasticsearch_server_baseurl=None): | ||
logger.warn("get_index_client is deprecated, use get_search_client instead") | ||
return get_search_client(elasticsearch_server_baseurl) | ||
def get_ingest_client(elasticsearch_server_baseurl=None): | ||
# to be removed in a 3.x release | ||
logger.warn(".elastic.get_client is deprecated, use .client.get_client instead") | ||
return get_client(elasticsearch_server_baseurl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
from .elastic import get_ingest_client | ||
from .client import get_client | ||
from .logging import logger | ||
|
||
|
||
def remove(uid, index_name): | ||
es = get_ingest_client() | ||
if es is None: | ||
logger.warning("No ElasticSearch client available.") | ||
client = get_client() | ||
if client is None: | ||
logger.warning("No index client available.") | ||
return | ||
try: | ||
es.delete(index=index_name, id=uid) | ||
client.delete(index=index_name, id=uid) | ||
except Exception: | ||
logger.exception("unindexing of {} on index {} failed".format(uid, index_name)) |