Skip to content
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

Add the new fullTextSearch method with the associated tests. #470

Closed
wants to merge 2 commits into from
Closed
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
42 changes: 42 additions & 0 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,37 @@ export default class ParseQuery {
return this._addCondition(key, '$geoIntersects', { '$point': point });
}

/**
* Method to find by full text.
* The key and the search fields are required the others are optionals.
* @method fullTextSearch
* @param {String} key The key to structure the where query
* @param {String} search The string to search
* @param {String} language Determine the list of stop words
* @param {Boolean} caseSensitive Dis/en-able the case sensitive search
* @param {Boolean} diacriticSensitive Dis/en-able diacritic sensitive search
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
fullTextSearch(key: string, search: string, language: string, caseSensitive: boolean, diacriticSensitive: boolean): ParseQuery {
if (!key) {
throw new Error('A key is required.');
}
if (typeof search !== 'string') {
throw new Error('The value being searched for must be a string.');
}
var options = { '$term': search };
if (typeof language === 'string') {
options['$language'] = language;
}
if (typeof caseSensitive === "boolean") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should wrap "boolean" with single quotes like the ones above.

options['$caseSensitive'] = caseSensitive;
}
if (typeof diacriticSensitive === "boolean") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here as above

options['$diacriticSensitive'] = diacriticSensitive;
}
return this._addCondition(key, '$text', { '$search': options });
}

/** Query Orderings **/

/**
Expand Down Expand Up @@ -1110,6 +1141,17 @@ export default class ParseQuery {
return this;
}

/**
* Method to sort the full text search by text score
* @method sortByTextScore
* @return {Parse.Query} Returns the query, so you can chain this call.
*/
sortByTextScore() {
this.ascending('$score');
this.select(['$score']);
return this;
}

/** Query Options **/

/**
Expand Down
169 changes: 116 additions & 53 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ describe('ParseQuery', () => {
});



it('overrides cached object with query results', (done) => {
jest.dontMock("../ParseObject");
jest.resetModules();
Expand All @@ -1347,12 +1347,12 @@ describe('ParseQuery', () => {
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();
var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
className:"Thing",

var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
className:"Thing",
createdAt: '2017-01-10T10:00:00Z'
};

Expand All @@ -1368,10 +1368,10 @@ describe('ParseQuery', () => {
var testObject;
q.find().then((results) => {
testObject = results[0];

expect(testObject.get("name")).toBe("Name");
expect(testObject.get("other")).toBe("other");

objectToReturn = { objectId: 'T01', name: 'Name2'};
var q2 = new ParseQuery("Thing");
return q2.find();
Expand All @@ -1393,13 +1393,13 @@ describe('ParseQuery', () => {
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();
var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
tbd: 'exists',
className:"Thing",

var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
tbd: 'exists',
className:"Thing",
createdAt: '2017-01-10T10:00:00Z',
subObject: {key1:"value", key2:"value2", key3:"thisWillGoAway"}
};
Expand All @@ -1416,14 +1416,14 @@ describe('ParseQuery', () => {
var testObject;
return q.find().then((results) => {
testObject = results[0];

expect(testObject.get("name")).toBe("Name");
expect(testObject.get("other")).toBe("other");
expect(testObject.has("tbd")).toBe(true);
expect(testObject.get("subObject").key1).toBe("value");
expect(testObject.get("subObject").key2).toBe("value2");
expect(testObject.get("subObject").key3).toBe("thisWillGoAway");

var q2 = new ParseQuery("Thing");
q2.select("other", "tbd", "subObject.key1", "subObject.key3");
objectToReturn = { objectId: 'T01', other: 'other2', subObject:{key1:"updatedValue"}};
Expand All @@ -1442,7 +1442,7 @@ describe('ParseQuery', () => {
expect(testObject.has("tbd")).toBe(false);
expect(testObject.get("subObject").key1).toBe("updatedValue");
expect(testObject.get("subObject").key2).toBe("value2");
expect(testObject.get("subObject").key3).toBeUndefined();
expect(testObject.get("subObject").key3).toBeUndefined();
done();
}, (error) => {
done.fail(error);
Expand All @@ -1457,12 +1457,12 @@ describe('ParseQuery', () => {
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();
var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
className:"Thing",

var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
className:"Thing",
createdAt: '2017-01-10T10:00:00Z'
};

Expand All @@ -1478,10 +1478,10 @@ describe('ParseQuery', () => {
var testObject;
q.first().then((result) => {
testObject = result;

expect(testObject.get("name")).toBe("Name");
expect(testObject.get("other")).toBe("other");

objectToReturn = { objectId: 'T01', name: 'Name2'};
var q2 = new ParseQuery("Thing");
return q2.first();
Expand All @@ -1503,13 +1503,13 @@ describe('ParseQuery', () => {
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();
var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
tbd: 'exists',
className:"Thing",

var objectToReturn = {
objectId: 'T01',
name: 'Name',
other: 'other',
tbd: 'exists',
className:"Thing",
subObject: {key1:"value", key2:"value2", key3:"thisWillGoAway"},
createdAt: '2017-01-10T10:00:00Z',
};
Expand All @@ -1526,11 +1526,11 @@ describe('ParseQuery', () => {
var testObject;
return q.first().then((result) => {
testObject = result;

expect(testObject.get("name")).toBe("Name");
expect(testObject.get("other")).toBe("other");
expect(testObject.has("tbd")).toBe(true);

var q2 = new ParseQuery("Thing");
q2.select("other", "tbd", "subObject.key1", "subObject.key3");
objectToReturn = { objectId: 'T01', other: 'other2', subObject:{key1:"updatedValue"}};
Expand All @@ -1546,10 +1546,10 @@ describe('ParseQuery', () => {
}).then(() => {
expect(testObject.get("name")).toBe("Name");
expect(testObject.get("other")).toBe("other2");
expect(testObject.has("tbd")).toBe(false);
expect(testObject.has("tbd")).toBe(false);
expect(testObject.get("subObject").key1).toBe("updatedValue");
expect(testObject.get("subObject").key2).toBe("value2");
expect(testObject.get("subObject").key3).toBeUndefined();
expect(testObject.get("subObject").key3).toBeUndefined();
done();
}, (error) => {
done.fail(error);
Expand Down Expand Up @@ -1593,12 +1593,12 @@ describe('ParseQuery', () => {
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();
var objectToReturn = {
objectId: 'T01',
name: 'Name',
tbd: 'exists',
className:"Thing",

var objectToReturn = {
objectId: 'T01',
name: 'Name',
tbd: 'exists',
className:"Thing",
createdAt: '2017-01-10T10:00:00Z'
};

Expand All @@ -1615,7 +1615,7 @@ describe('ParseQuery', () => {
var testObject;
return q.find().then((results) => {
testObject = results[0];

expect(testObject.get("name")).toBe("Name");
expect(testObject.has("other")).toBe(false);
expect(testObject.has("subObject")).toBe(false);
Expand All @@ -1635,12 +1635,12 @@ describe('ParseQuery', () => {
ParseQuery = require('../ParseQuery').default;

ParseObject.enableSingleInstance();
var objectToReturn = {
objectId: 'T01',
name: 'Name',
tbd: 'exists',
className:"Thing",

var objectToReturn = {
objectId: 'T01',
name: 'Name',
tbd: 'exists',
className:"Thing",
subObject1: {foo:"bar"},
subObject2: {foo:"bar"},
subObject3: {foo:"bar"},
Expand All @@ -1660,7 +1660,7 @@ describe('ParseQuery', () => {
var testObject;
return q.find().then((results) => {
testObject = results[0];

expect(testObject.has("subObject1")).toBe(true);
expect(testObject.has("subObject2")).toBe(true);
expect(testObject.has("subObject3")).toBe(true);
Expand All @@ -1675,7 +1675,7 @@ describe('ParseQuery', () => {
expect(testObject.has("subObject2")).toBe(false); //selected and not returned
expect(testObject.has("subObject3")).toBe(true); //not selected, so should still be there
expect(testObject.has("subObject4")).toBe(true); //selected and just added
expect(testObject.has("subObject5")).toBe(true);
expect(testObject.has("subObject5")).toBe(true);
expect(testObject.get("subObject5").subSubObject).toBeDefined();
expect(testObject.get("subObject5").subSubObject.bar).toBeDefined(); //not selected but a sibiling was, so should still be there
}).then(() => {
Expand All @@ -1685,4 +1685,67 @@ describe('ParseQuery', () => {
});
});

it('full text search with one parameter', () => {
let query = new ParseQuery('Item');

query.fullTextSearch('size', 'small');

expect(query.toJSON()).toEqual({
where: {
size: {
$text: {
$search: {
$term: "small"
}
}
}
}
});
});

it('full text search with all parameters', () => {
let query = new ParseQuery('Item');

query.fullTextSearch('size', 'medium', 'en', false, true);

expect(query.toJSON()).toEqual({
where: {
size: {
$text: {
$search: {
$term: "medium",
$language: "en",
$caseSensitive: false,
$diacriticSensitive: true
}
}
}
}
});

});

it('add the score for the full text search', () => {
let query = new ParseQuery('Item');

query.fullTextSearch('size', 'medium', 'fr');
query.sortByTextScore();

expect(query.toJSON()).toEqual({
where: {
size: {
$text: {
$search: {
$term: "medium",
$language: "fr"
}
}
}
},
keys : "$score",
order : "$score"
});

});

});