-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_handler_base.py
62 lines (48 loc) · 1.65 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
from abc import ABC, abstractmethod
from ..helpers.env_helper import EnvHelper
from ..common.source_document import SourceDocument
from azure.search.documents import SearchClient
class SearchHandlerBase(ABC):
_VECTOR_FIELD = "content_vector"
_IMAGE_VECTOR_FIELD = "image_vector"
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 []
def delete_from_index(self, blob_url) -> None:
documents = self.search_by_blob_url(blob_url)
if documents is None or documents.get_count() == 0:
return
files_to_delete = self.output_results(documents)
self.delete_files(files_to_delete)
@abstractmethod
def create_search_client(self) -> SearchClient:
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
@abstractmethod
def search_by_blob_url(self, blob_url):
pass