T53161 WIP create elasticsearch app / doc / stuff
This commit is contained in:
0
pillar/api/search/__init__.py
Normal file
0
pillar/api/search/__init__.py
Normal file
38
pillar/api/search/algolia_indexing.py
Normal file
38
pillar/api/search/algolia_indexing.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import logging
|
||||
|
||||
from algoliasearch.helpers import AlgoliaException
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def push_updated_user(user_to_index: dict):
|
||||
"""Push an update to the Algolia index when a user item is updated"""
|
||||
|
||||
from pillar.api.utils.algolia import index_user_save
|
||||
|
||||
try:
|
||||
index_user_save(user_to_index)
|
||||
except AlgoliaException as ex:
|
||||
log.warning(
|
||||
'Unable to push user info to Algolia for user "%s", id=%s; %s', # noqa
|
||||
user_to_index.get('username'),
|
||||
user_to_index.get('objectID'), ex)
|
||||
|
||||
|
||||
def index_node_save(node_to_index: dict):
|
||||
from pillar.api.utils import algolia
|
||||
|
||||
try:
|
||||
algolia.index_node_save(node_to_index)
|
||||
except AlgoliaException as ex:
|
||||
log.warning(
|
||||
'Unable to push node info to Algolia for node %s; %s', node_to_index, ex) # noqa
|
||||
|
||||
|
||||
def index_node_delete(delete_id: str):
|
||||
|
||||
from pillar.api.utils import algolia
|
||||
try:
|
||||
algolia.index_node_delete(delete_id)
|
||||
except AlgoliaException as ex:
|
||||
log.warning('Unable to delete node info to Algolia for node %s; %s', delete_id, ex) # noqa
|
66
pillar/api/search/documents.py
Normal file
66
pillar/api/search/documents.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import logging
|
||||
|
||||
import elasticsearch_dsl as es
|
||||
from elasticsearch_dsl import analysis
|
||||
# from pillar import current_app
|
||||
|
||||
# define elasticsearch document mapping.
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
edge_ngram_filter = analysis.token_filter(
|
||||
'edge_ngram_filter',
|
||||
type='edge_ngram',
|
||||
min_gram=1,
|
||||
max_gram=15
|
||||
)
|
||||
|
||||
|
||||
autocomplete = es.analyzer(
|
||||
'autocomplete',
|
||||
tokenizer='standard',
|
||||
filter=['lowercase', edge_ngram_filter]
|
||||
)
|
||||
|
||||
|
||||
class User(es.DocType):
|
||||
"""
|
||||
Elastic document describing user
|
||||
"""
|
||||
|
||||
name = es.String(
|
||||
fielddata=True,
|
||||
analyzer=autocomplete,
|
||||
)
|
||||
|
||||
|
||||
class Node(es.DocType):
|
||||
"""
|
||||
Elastic document describing user
|
||||
"""
|
||||
|
||||
node_type = es.Keyword()
|
||||
|
||||
x_code = es.String(
|
||||
multi=True,
|
||||
fielddata=True,
|
||||
analyzer=autocomplete,
|
||||
)
|
||||
|
||||
|
||||
def create_doc_from_user_data(user_to_index):
|
||||
doc_id = user_to_index['objectID']
|
||||
doc = User(_id=doc_id)
|
||||
return doc
|
||||
|
||||
|
||||
def create_doc_from_node_data(node_to_index):
|
||||
|
||||
# node stuff
|
||||
doc_id = node_to_index['objectID']
|
||||
doc = Node(_id=doc_id)
|
||||
doc.node_type = node_to_index['node_type']
|
||||
|
||||
return doc
|
25
pillar/api/search/elastic_indexing.py
Normal file
25
pillar/api/search/elastic_indexing.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def push_updated_user(user_to_index: dict):
|
||||
"""Push an update to the Algolia index when a user item is updated"""
|
||||
|
||||
log.warning(
|
||||
'WIP USER ELK INDEXING %s %s',
|
||||
user_to_index.get('username'),
|
||||
user_to_index.get('objectID'))
|
||||
|
||||
|
||||
def index_node_save(node_to_index: dict):
|
||||
|
||||
log.warning(
|
||||
'WIP USER NODE INDEXING %s',
|
||||
node_to_index.get('objectID'))
|
||||
|
||||
|
||||
def index_node_delete(delete_id: str):
|
||||
|
||||
log.warning(
|
||||
'WIP NODE DELETE INDEXING %s', delete_id)
|
Reference in New Issue
Block a user