-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSEARCH.ts
112 lines (99 loc) · 2.72 KB
/
SEARCH.ts
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
import { RedisCommandArguments } from '@redis/client/dist/lib/commands';
import { pushSearchOptions, RedisSearchLanguages, Params, PropertyName, SortByProperty, SearchReply } from '.';
export const FIRST_KEY_INDEX = 1;
export const IS_READ_ONLY = true;
export interface SearchOptions {
VERBATIM?: true;
NOSTOPWORDS?: true;
// WITHSCORES?: true;
// WITHPAYLOADS?: true;
WITHSORTKEYS?: true;
// FILTER?: {
// field: string;
// min: number | string;
// max: number | string;
// };
// GEOFILTER?: {
// field: string;
// lon: number;
// lat: number;
// radius: number;
// unit: 'm' | 'km' | 'mi' | 'ft';
// };
INKEYS?: string | Array<string>;
INFIELDS?: string | Array<string>;
RETURN?: string | Array<string>;
SUMMARIZE?: true | {
FIELDS?: PropertyName | Array<PropertyName>;
FRAGS?: number;
LEN?: number;
SEPARATOR?: string;
};
HIGHLIGHT?: true | {
FIELDS?: PropertyName | Array<PropertyName>;
TAGS?: {
open: string;
close: string;
};
};
SLOP?: number;
INORDER?: true;
LANGUAGE?: RedisSearchLanguages;
EXPANDER?: string;
SCORER?: string;
// EXPLAINSCORE?: true; // TODO: WITHSCORES
// PAYLOAD?: ;
SORTBY?: SortByProperty;
// MSORTBY?: SortByProperty | Array<SortByProperty>;
LIMIT?: {
from: number | string;
size: number | string;
};
PARAMS?: Params;
DIALECT?: number;
TIMEOUT?: number;
}
export function transformArguments(
index: string,
query: string,
options?: SearchOptions
): RedisCommandArguments {
return pushSearchOptions(
['FT.SEARCH', index, query],
options
);
}
export type SearchRawReply = Array<any>;
export function transformReply(reply: SearchRawReply, withoutDocuments: boolean): SearchReply {
const documents = [];
let i = 1;
while (i < reply.length) {
documents.push({
id: reply[i],
value: withoutDocuments || reply[i+1] == null ? Object.create(null) : documentValue(reply[i+1])
});
i += 2;
}
return {
total: reply[0],
documents
};
}
function documentValue(tuples: any) {
const message = Object.create(null);
let i = 0;
while (i < tuples.length) {
const key = tuples[i++],
value = tuples[i++];
if (key === '$') { // might be a JSON reply
try {
Object.assign(message, JSON.parse(value));
continue;
} catch {
// set as a regular property if not a valid JSON
}
}
message[key] = value;
}
return message;
}