-
Notifications
You must be signed in to change notification settings - Fork 384
/
search_handler_base.py
70 lines (53 loc) · 1.74 KB
/
search_handler_base.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from abc import ABC, abstractmethod
from ..helpers.env_helper import EnvHelper
from ..common.source_document import SourceDocument
class SearchHandlerBase(ABC):
def __init__(self, env_helper: EnvHelper):
self.env_helper = env_helper
self.search_client = self.create_search_client()
def search_with_facets(self, query: str, facets: list[str]):
if self.search_client is None:
return None
return self.search_client.search(query, facets=facets)
def get_unique_files(self, results, facet_key: str):
if results:
return [facet["value"] for facet in results.get_facets()[facet_key]]
return []
@abstractmethod
def create_search_client(self):
pass
@abstractmethod
def perform_search(self, filename):
pass
@abstractmethod
def process_results(self, results):
pass
@abstractmethod
def get_files(self):
pass
@abstractmethod
def output_results(self, results):
pass
@abstractmethod
def delete_files(self, files):
pass
@abstractmethod
def query_search(self, question) -> list[SourceDocument]:
pass
def delete_by_source(self, source) -> None:
if source is None:
return
documents = self._get_documents_by_source(source)
if documents is None:
return
files_to_delete = self.output_results(documents)
self.delete_files(files_to_delete)
def _get_documents_by_source(self, source):
if source is None:
return None
return self.search_client.search(
"*",
select="id, title",
include_total_count=True,
filter=f"source eq '{source}'",
)