Skip to content

Text Index Support #4081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions spec/ParseQuery.FullTextSearch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ const fullTextHelper = () => {
restAPIKey: 'test',
publicServerURL: 'http://localhost:8378/1',
databaseAdapter
}).then(() => {
if (process.env.PARSE_SERVER_TEST_DB === 'postgres') {
return Parse.Promise.as();
}
return databaseAdapter.createIndex('TestObject', {subject: 'text'});
}).then(() => {
return rp.post({
url: 'http://localhost:8378/1/batch',
Expand Down Expand Up @@ -285,7 +280,7 @@ describe('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({
appId: 'test',
restAPIKey: 'test',
Expand Down Expand Up @@ -318,6 +313,8 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
'X-Parse-REST-API-Key': 'test'
}
});
}).then(() => {
return databaseAdapter.createIndex('TestObject', {random: 'text'});
}).then(() => {
const where = {
subject: {
Expand All @@ -337,7 +334,7 @@ describe_only_db('mongo')('Parse.Query Full Text Search testing', () => {
}
});
}).then((resp) => {
fail(`Text Index should not exist: ${JSON.stringify(resp)}`);
fail(`Should not be more than one text index: ${JSON.stringify(resp)}`);
done();
}).catch((err) => {
expect(err.error.code).toEqual(Parse.Error.INTERNAL_SERVER_ERROR);
Expand Down
25 changes: 24 additions & 1 deletion src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing error I could delete all current text indexes, add the new one and retry.

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());
Expand Down