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:
committed by
Florent Vilmart
parent
0bace67bb1
commit
5aafc93476
@@ -42,11 +42,6 @@ const fullTextHelper = () => {
|
|||||||
restAPIKey: 'test',
|
restAPIKey: 'test',
|
||||||
publicServerURL: 'http://localhost:8378/1',
|
publicServerURL: 'http://localhost:8378/1',
|
||||||
databaseAdapter
|
databaseAdapter
|
||||||
}).then(() => {
|
|
||||||
if (process.env.PARSE_SERVER_TEST_DB === 'postgres') {
|
|
||||||
return Parse.Promise.as();
|
|
||||||
}
|
|
||||||
return databaseAdapter.createIndex('TestObject', {subject: 'text'});
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return rp.post({
|
return rp.post({
|
||||||
url: 'http://localhost:8378/1/batch',
|
url: 'http://localhost:8378/1/batch',
|
||||||
@@ -285,7 +280,7 @@ describe('Parse.Query Full Text Search testing', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
|
describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
|
||||||
it('fullTextSearch: $search, index not exist', (done) => {
|
it('fullTextSearch: $search, only one text index', (done) => {
|
||||||
return reconfigureServer({
|
return reconfigureServer({
|
||||||
appId: 'test',
|
appId: 'test',
|
||||||
restAPIKey: 'test',
|
restAPIKey: 'test',
|
||||||
@@ -318,6 +313,8 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
|
|||||||
'X-Parse-REST-API-Key': 'test'
|
'X-Parse-REST-API-Key': 'test'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}).then(() => {
|
||||||
|
return databaseAdapter.createIndex('TestObject', {random: 'text'});
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
const where = {
|
const where = {
|
||||||
subject: {
|
subject: {
|
||||||
@@ -337,7 +334,7 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).then((resp) => {
|
}).then((resp) => {
|
||||||
fail(`Text Index should not exist: ${JSON.stringify(resp)}`);
|
fail(`Should not be more than one text index: ${JSON.stringify(resp)}`);
|
||||||
done();
|
done();
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
expect(err.error.code).toEqual(Parse.Error.INTERNAL_SERVER_ERROR);
|
expect(err.error.code).toEqual(Parse.Error.INTERNAL_SERVER_ERROR);
|
||||||
|
|||||||
@@ -343,8 +343,10 @@ export class MongoStorageAdapter {
|
|||||||
memo[transformKey(className, key, schema)] = 1;
|
memo[transformKey(className, key, schema)] = 1;
|
||||||
return memo;
|
return memo;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
readPreference = this._parseReadPreference(readPreference);
|
readPreference = this._parseReadPreference(readPreference);
|
||||||
return this._adaptiveCollection(className)
|
return this.createTextIndexesIfNeeded(className, query)
|
||||||
|
.then(() => this._adaptiveCollection(className))
|
||||||
.then(collection => collection.find(mongoWhere, {
|
.then(collection => collection.find(mongoWhere, {
|
||||||
skip,
|
skip,
|
||||||
limit,
|
limit,
|
||||||
@@ -441,6 +443,27 @@ export class MongoStorageAdapter {
|
|||||||
return Promise.resolve();
|
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) {
|
getIndexes(className) {
|
||||||
return this._adaptiveCollection(className)
|
return this._adaptiveCollection(className)
|
||||||
.then(collection => collection._mongoCollection.indexes());
|
.then(collection => collection._mongoCollection.indexes());
|
||||||
|
|||||||
Reference in New Issue
Block a user