Skip to content

Commit 5aafc93

Browse files
dplewisflovilmart
authored andcommitted
Text Index Support (#4081)
* add text index support * additional validation * multiple text index error * rename function * Delete text indexes message
1 parent 0bace67 commit 5aafc93

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

spec/ParseQuery.FullTextSearch.spec.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ const fullTextHelper = () => {
4242
restAPIKey: 'test',
4343
publicServerURL: 'http://localhost:8378/1',
4444
databaseAdapter
45-
}).then(() => {
46-
if (process.env.PARSE_SERVER_TEST_DB === 'postgres') {
47-
return Parse.Promise.as();
48-
}
49-
return databaseAdapter.createIndex('TestObject', {subject: 'text'});
5045
}).then(() => {
5146
return rp.post({
5247
url: 'http://localhost:8378/1/batch',
@@ -285,7 +280,7 @@ describe('Parse.Query Full Text Search testing', () => {
285280
});
286281

287282
describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
288-
it('fullTextSearch: $search, index not exist', (done) => {
283+
it('fullTextSearch: $search, only one text index', (done) => {
289284
return reconfigureServer({
290285
appId: 'test',
291286
restAPIKey: 'test',
@@ -318,6 +313,8 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
318313
'X-Parse-REST-API-Key': 'test'
319314
}
320315
});
316+
}).then(() => {
317+
return databaseAdapter.createIndex('TestObject', {random: 'text'});
321318
}).then(() => {
322319
const where = {
323320
subject: {
@@ -337,7 +334,7 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
337334
}
338335
});
339336
}).then((resp) => {
340-
fail(`Text Index should not exist: ${JSON.stringify(resp)}`);
337+
fail(`Should not be more than one text index: ${JSON.stringify(resp)}`);
341338
done();
342339
}).catch((err) => {
343340
expect(err.error.code).toEqual(Parse.Error.INTERNAL_SERVER_ERROR);

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,10 @@ export class MongoStorageAdapter {
343343
memo[transformKey(className, key, schema)] = 1;
344344
return memo;
345345
}, {});
346+
346347
readPreference = this._parseReadPreference(readPreference);
347-
return this._adaptiveCollection(className)
348+
return this.createTextIndexesIfNeeded(className, query)
349+
.then(() => this._adaptiveCollection(className))
348350
.then(collection => collection.find(mongoWhere, {
349351
skip,
350352
limit,
@@ -441,6 +443,27 @@ export class MongoStorageAdapter {
441443
return Promise.resolve();
442444
}
443445

446+
createTextIndexesIfNeeded(className, query) {
447+
for(const fieldName in query) {
448+
if (!query[fieldName] || !query[fieldName].$text) {
449+
continue;
450+
}
451+
const index = {
452+
[fieldName]: 'text'
453+
};
454+
return this.createIndex(className, index)
455+
.catch((error) => {
456+
if (error.code === 85) {
457+
throw new Parse.Error(
458+
Parse.Error.INTERNAL_SERVER_ERROR,
459+
'Only one text index is supported, please delete all text indexes to use new field.');
460+
}
461+
throw error;
462+
});
463+
}
464+
return Promise.resolve();
465+
}
466+
444467
getIndexes(className) {
445468
return this._adaptiveCollection(className)
446469
.then(collection => collection._mongoCollection.indexes());

0 commit comments

Comments
 (0)