Text Index Support (#4081)

* add text index support

* additional validation

* multiple text index error

* rename function

* Delete text indexes message
This commit is contained in:
Diamond Lewis
2017-09-02 11:31:19 -05:00
committed by Florent Vilmart
parent 0bace67bb1
commit 5aafc93476
2 changed files with 28 additions and 8 deletions

View File

@@ -343,8 +343,10 @@ export class MongoStorageAdapter {
memo[transformKey(className, key, schema)] = 1;
return memo;
}, {});
readPreference = this._parseReadPreference(readPreference);
return this._adaptiveCollection(className)
return this.createTextIndexesIfNeeded(className, query)
.then(() => this._adaptiveCollection(className))
.then(collection => collection.find(mongoWhere, {
skip,
limit,
@@ -441,6 +443,27 @@ export class MongoStorageAdapter {
return Promise.resolve();
}
createTextIndexesIfNeeded(className, query) {
for(const fieldName in query) {
if (!query[fieldName] || !query[fieldName].$text) {
continue;
}
const index = {
[fieldName]: 'text'
};
return this.createIndex(className, index)
.catch((error) => {
if (error.code === 85) {
throw new Parse.Error(
Parse.Error.INTERNAL_SERVER_ERROR,
'Only one text index is supported, please delete all text indexes to use new field.');
}
throw error;
});
}
return Promise.resolve();
}
getIndexes(className) {
return this._adaptiveCollection(className)
.then(collection => collection._mongoCollection.indexes());