| 1 |
import { strict as assert } from 'assert'; |
| 2 |
import { RedisSearchLanguages, SchemaFieldTypes } from '.'; |
| 3 |
import testUtils, { GLOBAL } from '../test-utils'; |
| 4 |
import { transformArguments } from './SEARCH'; |
| 5 |
|
| 6 |
describe('SEARCH', () => { |
| 7 |
describe('transformArguments', () => { |
| 8 |
it('without options', () => { |
| 9 |
assert.deepEqual( |
| 10 |
transformArguments('index', 'query'), |
| 11 |
['FT.SEARCH', 'index', 'query'] |
| 12 |
); |
| 13 |
}); |
| 14 |
|
| 15 |
it('with VERBATIM', () => { |
| 16 |
assert.deepEqual( |
| 17 |
transformArguments('index', 'query', { VERBATIM: true }), |
| 18 |
['FT.SEARCH', 'index', 'query', 'VERBATIM'] |
| 19 |
); |
| 20 |
}); |
| 21 |
|
| 22 |
it('with NOSTOPWORDS', () => { |
| 23 |
assert.deepEqual( |
| 24 |
transformArguments('index', 'query', { NOSTOPWORDS: true }), |
| 25 |
['FT.SEARCH', 'index', 'query', 'NOSTOPWORDS'] |
| 26 |
); |
| 27 |
}); |
| 28 |
|
| 29 |
it('with INKEYS', () => { |
| 30 |
assert.deepEqual( |
| 31 |
transformArguments('index', 'query', { INKEYS: 'key' }), |
| 32 |
['FT.SEARCH', 'index', 'query', 'INKEYS', '1', 'key'] |
| 33 |
); |
| 34 |
}); |
| 35 |
|
| 36 |
it('with INFIELDS', () => { |
| 37 |
assert.deepEqual( |
| 38 |
transformArguments('index', 'query', { INFIELDS: 'field' }), |
| 39 |
['FT.SEARCH', 'index', 'query', 'INFIELDS', '1', 'field'] |
| 40 |
); |
| 41 |
}); |
| 42 |
|
| 43 |
it('with RETURN', () => { |
| 44 |
assert.deepEqual( |
| 45 |
transformArguments('index', 'query', { RETURN: 'return' }), |
| 46 |
['FT.SEARCH', 'index', 'query', 'RETURN', '1', 'return'] |
| 47 |
); |
| 48 |
}); |
| 49 |
|
| 50 |
describe('with SUMMARIZE', () => { |
| 51 |
it('true', () => { |
| 52 |
assert.deepEqual( |
| 53 |
transformArguments('index', 'query', { SUMMARIZE: true }), |
| 54 |
['FT.SEARCH', 'index', 'query', 'SUMMARIZE'] |
| 55 |
); |
| 56 |
}); |
| 57 |
|
| 58 |
describe('with FIELDS', () => { |
| 59 |
it('string', () => { |
| 60 |
assert.deepEqual( |
| 61 |
transformArguments('index', 'query', { |
| 62 |
SUMMARIZE: { |
| 63 |
FIELDS: ['@field'] |
| 64 |
} |
| 65 |
}), |
| 66 |
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FIELDS', '1', '@field'] |
| 67 |
); |
| 68 |
}); |
| 69 |
|
| 70 |
it('Array', () => { |
| 71 |
assert.deepEqual( |
| 72 |
transformArguments('index', 'query', { |
| 73 |
SUMMARIZE: { |
| 74 |
FIELDS: ['@1', '@2'] |
| 75 |
} |
| 76 |
}), |
| 77 |
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FIELDS', '2', '@1', '@2'] |
| 78 |
); |
| 79 |
}); |
| 80 |
}); |
| 81 |
|
| 82 |
it('with FRAGS', () => { |
| 83 |
assert.deepEqual( |
| 84 |
transformArguments('index', 'query', { |
| 85 |
SUMMARIZE: { |
| 86 |
FRAGS: 1 |
| 87 |
} |
| 88 |
}), |
| 89 |
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'FRAGS', '1'] |
| 90 |
); |
| 91 |
}); |
| 92 |
|
| 93 |
it('with LEN', () => { |
| 94 |
assert.deepEqual( |
| 95 |
transformArguments('index', 'query', { |
| 96 |
SUMMARIZE: { |
| 97 |
LEN: 1 |
| 98 |
} |
| 99 |
}), |
| 100 |
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'LEN', '1'] |
| 101 |
); |
| 102 |
}); |
| 103 |
|
| 104 |
it('with SEPARATOR', () => { |
| 105 |
assert.deepEqual( |
| 106 |
transformArguments('index', 'query', { |
| 107 |
SUMMARIZE: { |
| 108 |
SEPARATOR: 'separator' |
| 109 |
} |
| 110 |
}), |
| 111 |
['FT.SEARCH', 'index', 'query', 'SUMMARIZE', 'SEPARATOR', 'separator'] |
| 112 |
); |
| 113 |
}); |
| 114 |
}); |
| 115 |
|
| 116 |
describe('with HIGHLIGHT', () => { |
| 117 |
it('true', () => { |
| 118 |
assert.deepEqual( |
| 119 |
transformArguments('index', 'query', { HIGHLIGHT: true }), |
| 120 |
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT'] |
| 121 |
); |
| 122 |
}); |
| 123 |
|
| 124 |
describe('with FIELDS', () => { |
| 125 |
it('string', () => { |
| 126 |
assert.deepEqual( |
| 127 |
transformArguments('index', 'query', { |
| 128 |
HIGHLIGHT: { |
| 129 |
FIELDS: ['@field'] |
| 130 |
} |
| 131 |
}), |
| 132 |
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'FIELDS', '1', '@field'] |
| 133 |
); |
| 134 |
}); |
| 135 |
|
| 136 |
it('Array', () => { |
| 137 |
assert.deepEqual( |
| 138 |
transformArguments('index', 'query', { |
| 139 |
HIGHLIGHT: { |
| 140 |
FIELDS: ['@1', '@2'] |
| 141 |
} |
| 142 |
}), |
| 143 |
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'FIELDS', '2', '@1', '@2'] |
| 144 |
); |
| 145 |
}); |
| 146 |
}); |
| 147 |
|
| 148 |
it('with TAGS', () => { |
| 149 |
assert.deepEqual( |
| 150 |
transformArguments('index', 'query', { |
| 151 |
HIGHLIGHT: { |
| 152 |
TAGS: { |
| 153 |
open: 'open', |
| 154 |
close: 'close' |
| 155 |
} |
| 156 |
} |
| 157 |
}), |
| 158 |
['FT.SEARCH', 'index', 'query', 'HIGHLIGHT', 'TAGS', 'open', 'close'] |
| 159 |
); |
| 160 |
}); |
| 161 |
}); |
| 162 |
|
| 163 |
it('with SLOP', () => { |
| 164 |
assert.deepEqual( |
| 165 |
transformArguments('index', 'query', { SLOP: 1 }), |
| 166 |
['FT.SEARCH', 'index', 'query', 'SLOP', '1'] |
| 167 |
); |
| 168 |
}); |
| 169 |
|
| 170 |
it('with INORDER', () => { |
| 171 |
assert.deepEqual( |
| 172 |
transformArguments('index', 'query', { INORDER: true }), |
| 173 |
['FT.SEARCH', 'index', 'query', 'INORDER'] |
| 174 |
); |
| 175 |
}); |
| 176 |
|
| 177 |
it('with LANGUAGE', () => { |
| 178 |
assert.deepEqual( |
| 179 |
transformArguments('index', 'query', { LANGUAGE: RedisSearchLanguages.ARABIC }), |
| 180 |
['FT.SEARCH', 'index', 'query', 'LANGUAGE', RedisSearchLanguages.ARABIC] |
| 181 |
); |
| 182 |
}); |
| 183 |
|
| 184 |
it('with EXPANDER', () => { |
| 185 |
assert.deepEqual( |
| 186 |
transformArguments('index', 'query', { EXPANDER: 'expender' }), |
| 187 |
['FT.SEARCH', 'index', 'query', 'EXPANDER', 'expender'] |
| 188 |
); |
| 189 |
}); |
| 190 |
|
| 191 |
it('with SCORER', () => { |
| 192 |
assert.deepEqual( |
| 193 |
transformArguments('index', 'query', { SCORER: 'scorer' }), |
| 194 |
['FT.SEARCH', 'index', 'query', 'SCORER', 'scorer'] |
| 195 |
); |
| 196 |
}); |
| 197 |
|
| 198 |
it('with SORTBY', () => { |
| 199 |
assert.deepEqual( |
| 200 |
transformArguments('index', 'query', { SORTBY: '@by' }), |
| 201 |
['FT.SEARCH', 'index', 'query', 'SORTBY', '@by'] |
| 202 |
); |
| 203 |
}); |
| 204 |
|
| 205 |
it('with LIMIT', () => { |
| 206 |
assert.deepEqual( |
| 207 |
transformArguments('index', 'query', { |
| 208 |
LIMIT: { |
| 209 |
from: 0, |
| 210 |
size: 1 |
| 211 |
} |
| 212 |
}), |
| 213 |
['FT.SEARCH', 'index', 'query', 'LIMIT', '0', '1'] |
| 214 |
); |
| 215 |
}); |
| 216 |
|
| 217 |
it('with PARAMS', () => { |
| 218 |
assert.deepEqual( |
| 219 |
transformArguments('index', 'query', { |
| 220 |
PARAMS: { |
| 221 |
param: 'value' |
| 222 |
} |
| 223 |
}), |
| 224 |
['FT.SEARCH', 'index', 'query', 'PARAMS', '2', 'param', 'value'] |
| 225 |
); |
| 226 |
}); |
| 227 |
|
| 228 |
it('with DIALECT', () => { |
| 229 |
assert.deepEqual( |
| 230 |
transformArguments('index', 'query', { |
| 231 |
DIALECT: 1 |
| 232 |
}), |
| 233 |
['FT.SEARCH', 'index', 'query', 'DIALECT', '1'] |
| 234 |
); |
| 235 |
}); |
| 236 |
|
| 237 |
it('with TIMEOUT', () => { |
| 238 |
assert.deepEqual( |
| 239 |
transformArguments('index', 'query', { |
| 240 |
TIMEOUT: 5 |
| 241 |
}), |
| 242 |
['FT.SEARCH', 'index', 'query', 'TIMEOUT', '5'] |
| 243 |
); |
| 244 |
}); |
| 245 |
}); |
| 246 |
|
| 247 |
describe('client.ft.search', () => { |
| 248 |
testUtils.testWithClient('without optional options', async client => { |
| 249 |
await Promise.all([ |
| 250 |
client.ft.create('index', { |
| 251 |
field: SchemaFieldTypes.NUMERIC |
| 252 |
}), |
| 253 |
client.hSet('1', 'field', '1') |
| 254 |
]); |
| 255 |
|
| 256 |
assert.deepEqual( |
| 257 |
await client.ft.search('index', '*'), |
| 258 |
{ |
| 259 |
total: 1, |
| 260 |
documents: [{ |
| 261 |
id: '1', |
| 262 |
value: Object.create(null, { |
| 263 |
field: { |
| 264 |
value: '1', |
| 265 |
configurable: true, |
| 266 |
enumerable: true |
| 267 |
} |
| 268 |
}) |
| 269 |
}] |
| 270 |
} |
| 271 |
); |
| 272 |
}, GLOBAL.SERVERS.OPEN); |
| 273 |
|
| 274 |
testUtils.testWithClient('RETURN []', async client => { |
| 275 |
await Promise.all([ |
| 276 |
client.ft.create('index', { |
| 277 |
field: SchemaFieldTypes.NUMERIC |
| 278 |
}), |
| 279 |
client.hSet('1', 'field', '1'), |
| 280 |
client.hSet('2', 'field', '2') |
| 281 |
]); |
| 282 |
|
| 283 |
assert.deepEqual( |
| 284 |
await client.ft.search('index', '*', { |
| 285 |
RETURN: [] |
| 286 |
}), |
| 287 |
{ |
| 288 |
total: 2, |
| 289 |
documents: [{ |
| 290 |
id: '1', |
| 291 |
value: Object.create(null) |
| 292 |
}, { |
| 293 |
id: '2', |
| 294 |
value: Object.create(null) |
| 295 |
}] |
| 296 |
} |
| 297 |
); |
| 298 |
}, GLOBAL.SERVERS.OPEN); |
| 299 |
}); |
| 300 |
}); |