1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SearchExecutor = void 0;
|
4 |
|
5 | const bindingutilities_1 = require("./bindingutilities");
|
6 | const searchquery_1 = require("./searchquery");
|
7 | const searchtypes_1 = require("./searchtypes");
|
8 | const streamablepromises_1 = require("./streamablepromises");
|
9 |
|
10 |
|
11 |
|
12 | class SearchExecutor {
|
13 | |
14 |
|
15 |
|
16 | constructor(cluster, bucketName, scopeName) {
|
17 | this._cluster = cluster;
|
18 | this._bucketName = bucketName;
|
19 | this._scopeName = scopeName;
|
20 | }
|
21 | |
22 |
|
23 |
|
24 | query(indexName, query, options) {
|
25 | const emitter = new streamablepromises_1.StreamableRowPromise((rows, meta) => {
|
26 | return new searchtypes_1.SearchResult({
|
27 | rows: rows,
|
28 | meta: meta,
|
29 | });
|
30 | });
|
31 | const searchQuery = query instanceof searchquery_1.SearchQuery
|
32 | ? JSON.stringify(query)
|
33 | : query.searchQuery
|
34 | ? JSON.stringify(query.searchQuery)
|
35 | : JSON.stringify(new searchquery_1.MatchNoneSearchQuery());
|
36 | const timeout = options.timeout || this._cluster.searchTimeout;
|
37 | const request = {
|
38 | timeout,
|
39 | index_name: indexName,
|
40 | query: searchQuery,
|
41 | limit: options.limit,
|
42 | skip: options.skip,
|
43 | explain: options.explain || false,
|
44 | disable_scoring: options.disableScoring || false,
|
45 | include_locations: options.includeLocations || false,
|
46 | highlight_style: options.highlight
|
47 | ? (0, bindingutilities_1.searchHighlightStyleToCpp)(options.highlight.style)
|
48 | : undefined,
|
49 | highlight_fields: options.highlight && options.highlight.fields
|
50 | ? options.highlight.fields
|
51 | : [],
|
52 | fields: options.fields || [],
|
53 | collections: options.collections || [],
|
54 | scan_consistency: (0, bindingutilities_1.searchScanConsistencyToCpp)(options.consistency),
|
55 | mutation_state: (0, bindingutilities_1.mutationStateToCpp)(options.consistentWith).tokens,
|
56 | sort_specs: options.sort
|
57 | ? options.sort.map((sort) => JSON.stringify(sort))
|
58 | : [],
|
59 | facets: options.facets
|
60 | ? Object.fromEntries(Object.entries(options.facets)
|
61 | .filter(([, v]) => v !== undefined)
|
62 | .map(([k, v]) => [k, JSON.stringify(v)]))
|
63 | : {},
|
64 | raw: options.raw
|
65 | ? Object.fromEntries(Object.entries(options.raw)
|
66 | .filter(([, v]) => v !== undefined)
|
67 | .map(([k, v]) => [k, JSON.stringify(v)]))
|
68 | : {},
|
69 | body_str: '',
|
70 | show_request: options.showRequest || false,
|
71 | log_request: options.logRequest || false,
|
72 | log_response: options.logResponse || false,
|
73 | };
|
74 | if (query instanceof searchtypes_1.SearchRequest) {
|
75 | if (query.vectorSearch) {
|
76 | request.vector_search = JSON.stringify(query.vectorSearch.queries);
|
77 | if (query.vectorSearch.options &&
|
78 | query.vectorSearch.options.vectorQueryCombination) {
|
79 | request.vector_query_combination = (0, bindingutilities_1.vectorQueryCombinationToCpp)(query.vectorSearch.options.vectorQueryCombination);
|
80 | }
|
81 | }
|
82 | }
|
83 | if (this._bucketName && this._scopeName) {
|
84 | request.bucket_name = this._bucketName;
|
85 | request.scope_name = this._scopeName;
|
86 | }
|
87 | this._cluster.conn.search(request, (cppErr, resp) => {
|
88 | const err = (0, bindingutilities_1.errorFromCpp)(cppErr);
|
89 | if (err) {
|
90 | emitter.emit('error', err);
|
91 | emitter.emit('end');
|
92 | return;
|
93 | }
|
94 | resp.rows.forEach((row) => {
|
95 | row.fields = row.fields ? JSON.parse(row.fields) : undefined;
|
96 | row.explanation = row.explanation
|
97 | ? JSON.parse(row.explanation)
|
98 | : undefined;
|
99 | emitter.emit('row', row);
|
100 | });
|
101 | {
|
102 | const metaData = resp.meta;
|
103 | emitter.emit('meta', {
|
104 | facets: Object.fromEntries(Object.values(resp.facets).map((v) => [v.name, v])),
|
105 | ...metaData,
|
106 | });
|
107 | }
|
108 | emitter.emit('end');
|
109 | return;
|
110 | });
|
111 | return emitter;
|
112 | }
|
113 | }
|
114 | exports.SearchExecutor = SearchExecutor;
|