File size: 18,045 Bytes
f4456af
 
 
b691127
 
2332381
f4456af
b691127
f4456af
b691127
 
22fe41e
3079197
 
b691127
 
 
 
f4456af
22fe41e
f4456af
 
3079197
b691127
3079197
f4456af
 
 
3079197
 
028acc4
 
3079197
 
 
 
22fe41e
f4456af
22fe41e
 
1eb186a
b691127
 
f4456af
 
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4456af
b691127
 
 
 
 
22fe41e
 
f4456af
b691127
 
 
22fe41e
 
f4456af
b691127
 
 
f4456af
b691127
f4456af
22fe41e
b691127
f4456af
 
 
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22fe41e
b691127
f4456af
 
b691127
f4456af
b691127
f4456af
 
b691127
f4456af
 
22fe41e
f4456af
 
22fe41e
f4456af
 
 
22fe41e
f4456af
 
b691127
c372afe
 
b691127
 
c372afe
 
b691127
 
 
 
 
c372afe
22fe41e
c372afe
 
 
22fe41e
c372afe
 
b691127
 
 
 
 
 
 
 
 
 
 
 
f4456af
b691127
 
1eb186a
b691127
 
 
 
1eb186a
b691127
 
 
 
 
f4456af
22fe41e
b691127
 
f4456af
b691127
f4456af
b691127
 
 
 
 
 
 
 
 
 
 
22fe41e
b691127
 
 
 
 
 
 
f4456af
b691127
 
 
 
 
 
 
 
 
f4456af
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22fe41e
b691127
 
f4456af
 
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22fe41e
b691127
f4456af
b691127
 
 
 
 
f4456af
22fe41e
b691127
 
f4456af
b691127
 
 
f4456af
 
b691127
 
 
f4456af
 
 
 
 
b691127
f4456af
 
b691127
f4456af
 
 
 
 
 
 
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
f4456af
b691127
 
 
3079197
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22fe41e
b691127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22fe41e
3079197
b691127
 
 
 
 
22fe41e
b691127
22fe41e
 
b691127
22fe41e
b691127
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import re
import json
import time
import os
from typing import List, Dict

import elasticsearch
import copy
from elasticsearch import Elasticsearch
from elasticsearch_dsl import UpdateByQuery, Q, Search, Index
from elastic_transport import ConnectionTimeout
from api.utils.log_utils import logger
from rag import settings
from rag.utils import singleton
from api.utils.file_utils import get_project_base_directory
import polars as pl
from rag.utils.doc_store_conn import DocStoreConnection, MatchExpr, OrderByExpr, MatchTextExpr, MatchDenseExpr, FusionExpr
from rag.nlp import is_english, rag_tokenizer

logger.info("Elasticsearch sdk version: "+str(elasticsearch.__version__))


@singleton
class ESConnection(DocStoreConnection):
    def __init__(self):
        self.info = {}
        for _ in range(10):
            try:
                self.es = Elasticsearch(
                    settings.ES["hosts"].split(","),
                    basic_auth=(settings.ES["username"], settings.ES["password"]) if "username" in settings.ES and "password" in settings.ES else None,
                    verify_certs=False,
                    timeout=600
                )
                if self.es:
                    self.info = self.es.info()
                    logger.info("Connect to es.")
                    break
            except Exception:
                logger.exception("Fail to connect to es")
                time.sleep(1)
        if not self.es.ping():
            raise Exception("Can't connect to ES cluster")
        v = self.info.get("version", {"number": "5.6"})
        v = v["number"].split(".")[0]
        if int(v) < 8:
            raise Exception(f"ES version must be greater than or equal to 8, current version: {v}")
        fp_mapping = os.path.join(get_project_base_directory(), "conf", "mapping.json")
        if not os.path.exists(fp_mapping):
            raise Exception(f"Mapping file not found at {fp_mapping}")
        self.mapping = json.load(open(fp_mapping, "r"))

    """
    Database operations
    """
    def dbType(self) -> str:
        return "elasticsearch"

    def health(self) -> dict:
        return dict(self.es.cluster.health()) + {"type": "elasticsearch"}

    """
    Table operations
    """
    def createIdx(self, indexName: str, knowledgebaseId: str, vectorSize: int):
        if self.indexExist(indexName, knowledgebaseId):
            return True
        try:
            from elasticsearch.client import IndicesClient
            return IndicesClient(self.es).create(index=indexName,
                                                 settings=self.mapping["settings"],
                                                 mappings=self.mapping["mappings"])
        except Exception:
            logger.exception("ES create index error %s" % (indexName))

    def deleteIdx(self, indexName: str, knowledgebaseId: str):
        try:
            return self.es.indices.delete(indexName, allow_no_indices=True)
        except Exception:
            logger.exception("ES delete index error %s" % (indexName))

    def indexExist(self, indexName: str, knowledgebaseId: str) -> bool:
        s = Index(indexName, self.es)
        for i in range(3):
            try:
                return s.exists()
            except Exception as e:
                logger.exception("ES indexExist")
                if str(e).find("Timeout") > 0 or str(e).find("Conflict") > 0:
                    continue
        return False

    """
    CRUD operations
    """
    def search(self, selectFields: list[str], highlightFields: list[str], condition: dict, matchExprs: list[MatchExpr], orderBy: OrderByExpr, offset: int, limit: int, indexNames: str|list[str], knowledgebaseIds: list[str]) -> list[dict] | pl.DataFrame:
        """
        Refers to https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
        """
        if isinstance(indexNames, str):
            indexNames = indexNames.split(",")
        assert isinstance(indexNames, list) and len(indexNames) > 0
        assert "_id" not in condition
        s = Search()
        bqry = None
        vector_similarity_weight = 0.5
        for m in matchExprs:
            if isinstance(m, FusionExpr) and m.method=="weighted_sum" and "weights" in m.fusion_params:
                assert len(matchExprs)==3 and isinstance(matchExprs[0], MatchTextExpr) and isinstance(matchExprs[1], MatchDenseExpr) and isinstance(matchExprs[2], FusionExpr)
                weights = m.fusion_params["weights"]
                vector_similarity_weight = float(weights.split(",")[1])
        for m in matchExprs:
            if isinstance(m, MatchTextExpr):
                minimum_should_match = "0%"
                if "minimum_should_match" in m.extra_options:
                    minimum_should_match = str(int(m.extra_options["minimum_should_match"] * 100)) + "%"
                bqry = Q("bool",
                            must=Q("query_string", fields=m.fields,
                                type="best_fields", query=m.matching_text,
                                minimum_should_match = minimum_should_match,
                                boost=1),
                            boost = 1.0 - vector_similarity_weight,
                        )
                if condition:
                    for k, v in condition.items():
                        if not isinstance(k, str) or not v:
                            continue
                        if isinstance(v, list):
                            bqry.filter.append(Q("terms", **{k: v}))
                        elif isinstance(v, str) or isinstance(v, int):
                            bqry.filter.append(Q("term", **{k: v}))
                        else:
                            raise Exception(f"Condition `{str(k)}={str(v)}` value type is {str(type(v))}, expected to be int, str or list.")
            elif isinstance(m, MatchDenseExpr):
                assert(bqry is not None)
                similarity = 0.0
                if "similarity" in m.extra_options:
                    similarity = m.extra_options["similarity"]
                s = s.knn(m.vector_column_name,
                    m.topn,
                    m.topn * 2,
                    query_vector = list(m.embedding_data),
                    filter = bqry.to_dict(),
                    similarity = similarity,
                )
        if matchExprs:
            s.query = bqry
        for field in highlightFields:
            s = s.highlight(field)

        if orderBy:
            orders = list()
            for field, order in orderBy.fields:
                order = "asc" if order == 0 else "desc"
                orders.append({field: {"order": order, "unmapped_type": "float",
                                 "mode": "avg", "numeric_type": "double"}})
            s = s.sort(*orders)

        if limit > 0:
            s = s[offset:limit]
        q = s.to_dict()
        # logger.info("ESConnection.search [Q]: " + json.dumps(q))

        for i in range(3):
            try:
                res = self.es.search(index=indexNames,
                                     body=q,
                                     timeout="600s",
                                     # search_type="dfs_query_then_fetch",
                                     track_total_hits=True,
                                     _source=True)
                if str(res.get("timed_out", "")).lower() == "true":
                    raise Exception("Es Timeout.")
                logger.info("ESConnection.search res: " + str(res))
                return res
            except Exception as e:
                logger.exception("ES search [Q]: " + str(q))
                if str(e).find("Timeout") > 0:
                    continue
                raise e
        logger.error("ES search timeout for 3 times!")
        raise Exception("ES search timeout.")

    def get(self, chunkId: str, indexName: str, knowledgebaseIds: list[str]) -> dict | None:
        for i in range(3):
            try:
                res = self.es.get(index=(indexName),
                                id=chunkId, source=True,)
                if str(res.get("timed_out", "")).lower() == "true":
                    raise Exception("Es Timeout.")
                if not res.get("found"):
                    return None
                chunk = res["_source"]
                chunk["id"] = chunkId
                return chunk
            except Exception as e:
                logger.exception(f"ES get({chunkId}) got exception")
                if str(e).find("Timeout") > 0:
                    continue
                raise e
        logger.error("ES search timeout for 3 times!")
        raise Exception("ES search timeout.")

    def insert(self, documents: list[dict], indexName: str, knowledgebaseId: str) -> list[str]:
        # Refers to https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
        operations = []
        for d in documents:
            assert "_id" not in d
            assert "id" in d
            d_copy = copy.deepcopy(d)
            meta_id = d_copy["id"]
            del d_copy["id"]
            operations.append(
                {"index": {"_index": indexName, "_id": meta_id}})
            operations.append(d_copy)

        res = []
        for _ in range(100):
            try:
                r = self.es.bulk(index=(indexName), operations=operations,
                                     refresh=False, timeout="600s")
                if re.search(r"False", str(r["errors"]), re.IGNORECASE):
                    return res

                for item in r["items"]:
                    for action in ["create", "delete", "index", "update"]:
                        if action in item and "error" in item[action]:
                            res.append(str(item[action]["_id"]) + ":" + str(item[action]["error"]))
                return res
            except Exception as e:
                logger.warning("Fail to bulk: " + str(e))
                if re.search(r"(Timeout|time out)", str(e), re.IGNORECASE):
                    time.sleep(3)
                    continue
        return res

    def update(self, condition: dict, newValue: dict, indexName: str, knowledgebaseId: str) -> bool:
        doc = copy.deepcopy(newValue)
        del doc['id']
        if "id" in condition and isinstance(condition["id"], str):
            # update specific single document
            chunkId = condition["id"]
            for i in range(3):
                try:
                    self.es.update(index=indexName, id=chunkId, doc=doc)
                    return True
                except Exception as e:
                    logger.exception(f"ES failed to update(index={indexName}, id={id}, doc={json.dumps(condition, ensure_ascii=False)})")
                    if str(e).find("Timeout") > 0:
                        continue
        else:
            # update unspecific maybe-multiple documents
            bqry = Q("bool")
            for k, v in condition.items():
                if not isinstance(k, str) or not v:
                    continue
                if isinstance(v, list):
                    bqry.filter.append(Q("terms", **{k: v}))
                elif isinstance(v, str) or isinstance(v, int):
                    bqry.filter.append(Q("term", **{k: v}))
                else:
                    raise Exception(f"Condition `{str(k)}={str(v)}` value type is {str(type(v))}, expected to be int, str or list.")
            scripts = []
            for k, v in newValue.items():
                if not isinstance(k, str) or not v:
                    continue
                if isinstance(v, str):
                    scripts.append(f"ctx._source.{k} = '{v}'")
                elif isinstance(v, int):
                    scripts.append(f"ctx._source.{k} = {v}")
                else:
                    raise Exception(f"newValue `{str(k)}={str(v)}` value type is {str(type(v))}, expected to be int, str.")
            ubq = UpdateByQuery(
                index=indexName).using(
                self.es).query(bqry)
            ubq = ubq.script(source="; ".join(scripts))
            ubq = ubq.params(refresh=True)
            ubq = ubq.params(slices=5)
            ubq = ubq.params(conflicts="proceed")
            for i in range(3):
                try:
                    _ = ubq.execute()
                    return True
                except Exception as e:
                    logger.error("ES update exception: " + str(e) + "[Q]:" + str(bqry.to_dict()))
                    if str(e).find("Timeout") > 0 or str(e).find("Conflict") > 0:
                        continue
        return False

    def delete(self, condition: dict, indexName: str, knowledgebaseId: str) -> int:
        qry = None
        assert "_id" not in condition
        if "id" in condition:
            chunk_ids = condition["id"]
            if not isinstance(chunk_ids, list):
                chunk_ids = [chunk_ids]
            qry = Q("ids", values=chunk_ids)
        else:
            qry = Q("bool")
            for k, v in condition.items():
                if isinstance(v, list):
                    qry.must.append(Q("terms", **{k: v}))
                elif isinstance(v, str) or isinstance(v, int):
                    qry.must.append(Q("term", **{k: v}))
                else:
                    raise Exception("Condition value must be int, str or list.")
        logger.info("ESConnection.delete [Q]: " + json.dumps(qry.to_dict()))
        for _ in range(10):
            try:
                res = self.es.delete_by_query(
                    index=indexName,
                    body = Search().query(qry).to_dict(),
                    refresh=True)
                return res["deleted"]
            except Exception as e:
                logger.warning("Fail to delete: " + str(filter) + str(e))
                if re.search(r"(Timeout|time out)", str(e), re.IGNORECASE):
                    time.sleep(3)
                    continue
                if re.search(r"(not_found)", str(e), re.IGNORECASE):
                    return 0
        return 0


    """
    Helper functions for search result
    """
    def getTotal(self, res):
        if isinstance(res["hits"]["total"], type({})):
            return res["hits"]["total"]["value"]
        return res["hits"]["total"]

    def getChunkIds(self, res):
        return [d["_id"] for d in res["hits"]["hits"]]

    def __getSource(self, res):
        rr = []
        for d in res["hits"]["hits"]:
            d["_source"]["id"] = d["_id"]
            d["_source"]["_score"] = d["_score"]
            rr.append(d["_source"])
        return rr

    def getFields(self, res, fields: List[str]) -> Dict[str, dict]:
        res_fields = {}
        if not fields:
            return {}
        for d in self.__getSource(res):
            m = {n: d.get(n) for n in fields if d.get(n) is not None}
            for n, v in m.items():
                if isinstance(v, list):
                    m[n] = v
                    continue
                if not isinstance(v, str):
                    m[n] = str(m[n])
                # if n.find("tks") > 0:
                #     m[n] = rmSpace(m[n])

            if m:
                res_fields[d["id"]] = m
        return res_fields

    def getHighlight(self, res, keywords: List[str], fieldnm: str):
        ans = {}
        for d in res["hits"]["hits"]:
            hlts = d.get("highlight")
            if not hlts:
                continue
            txt = "...".join([a for a in list(hlts.items())[0][1]])
            if not is_english(txt.split(" ")):
                ans[d["_id"]] = txt
                continue

            txt = d["_source"][fieldnm]
            txt = re.sub(r"[\r\n]", " ", txt, flags=re.IGNORECASE|re.MULTILINE)
            txts = []
            for t in re.split(r"[.?!;\n]", txt):
                for w in keywords:
                    t = re.sub(r"(^|[ .?/'\"\(\)!,:;-])(%s)([ .?/'\"\(\)!,:;-])"%re.escape(w), r"\1<em>\2</em>\3", t, flags=re.IGNORECASE|re.MULTILINE)
                if not re.search(r"<em>[^<>]+</em>", t, flags=re.IGNORECASE|re.MULTILINE):
                    continue
                txts.append(t)
            ans[d["_id"]] = "...".join(txts) if txts else "...".join([a for a in list(hlts.items())[0][1]])

        return ans

    def getAggregation(self, res, fieldnm: str):
        agg_field = "aggs_" + fieldnm
        if "aggregations" not in res or agg_field not in res["aggregations"]:
            return list()
        bkts = res["aggregations"][agg_field]["buckets"]
        return [(b["key"], b["doc_count"]) for b in bkts]


    """
    SQL
    """
    def sql(self, sql: str, fetch_size: int, format: str):
        logger.info(f"ESConnection.sql get sql: {sql}")
        sql = re.sub(r"[ `]+", " ", sql)
        sql = sql.replace("%", "")
        replaces = []
        for r in re.finditer(r" ([a-z_]+_l?tks)( like | ?= ?)'([^']+)'", sql):
            fld, v = r.group(1), r.group(3)
            match = " MATCH({}, '{}', 'operator=OR;minimum_should_match=30%') ".format(
                fld, rag_tokenizer.fine_grained_tokenize(rag_tokenizer.tokenize(v)))
            replaces.append(
                ("{}{}'{}'".format(
                    r.group(1),
                    r.group(2),
                    r.group(3)),
                    match))

        for p, r in replaces:
            sql = sql.replace(p, r, 1)
        logger.info(f"ESConnection.sql to es: {sql}")

        for i in range(3):
            try:
                res = self.es.sql.query(body={"query": sql, "fetch_size": fetch_size}, format=format, request_timeout="2s")
                return res
            except ConnectionTimeout:
                logger.exception("ESConnection.sql timeout [Q]: " + sql)
                continue
            except Exception:
                logger.exception("ESConnection.sql got exception [Q]: " + sql)
                return None
        logger.error("ESConnection.sql timeout for 3 times!")
        return None