Skip to content

Commit c29f31e

Browse files
vinz51SebC99
authored andcommitted
Add the new fullTextSearch method with the associated tests.
1 parent 13438fe commit c29f31e

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

src/ParseQuery.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,37 @@ export default class ParseQuery {
10131013
return this._addCondition(key, '$geoWithin', { '$polygon': points });
10141014
}
10151015

1016+
/**
1017+
* Method to find by full text.
1018+
* The key and the search fields are required the others are optionals.
1019+
* @method fullTextSearch
1020+
* @param {String} key The key to structure the where query
1021+
* @param {String} search The string to search
1022+
* @param {String} language Determine the list of stop words
1023+
* @param {Boolean} caseSensitive Dis/en-able the case sensitive search
1024+
* @param {Boolean} diacriticSensitive Dis/en-able diacritic sensitive search
1025+
* @return {Parse.Query} Returns the query, so you can chain this call.
1026+
*/
1027+
fullTextSearch(key: string, search: string, language: string, caseSensitive: boolean, diacriticSensitive: boolean): ParseQuery {
1028+
if (typeof key === 'undefined' || !key) {
1029+
throw new Error('A key is required.');
1030+
}
1031+
if (typeof search === 'undefined' || !search) {
1032+
throw new Error('You have to add one string to search.');
1033+
}
1034+
var options = { '$term': search };
1035+
if (typeof language !== "undefined" || language !== null) {
1036+
options['$language'] = language;
1037+
}
1038+
if (typeof caseSensitive !== "undefined" || caseSensitive !== null) {
1039+
options['$caseSensitive'] = caseSensitive;
1040+
}
1041+
if (typeof diacriticSensitive !== "undefined" || diacriticSensitive !== null) {
1042+
options['$diacriticSensitive'] = diacriticSensitive;
1043+
}
1044+
return this._addCondition(key, '$text', { '$search': options });
1045+
}
1046+
10161047
/** Query Orderings **/
10171048

10181049
/**
@@ -1091,6 +1122,17 @@ export default class ParseQuery {
10911122
return this;
10921123
}
10931124

1125+
/**
1126+
* Method to sort the full text search by text score
1127+
* @method sortByTextScore
1128+
* @return {Parse.Query} Returns the query, so you can chain this call.
1129+
*/
1130+
sortByTextScore() {
1131+
this.ascending('$score');
1132+
this.select(['$score']);
1133+
return this;
1134+
}
1135+
10941136
/** Query Options **/
10951137

10961138
/**

src/__tests__/ParseQuery-test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,4 +1584,67 @@ describe('ParseQuery', () => {
15841584
});
15851585

15861586
});
1587+
1588+
it('full text search with one parameter', () => {
1589+
let query = new ParseQuery('Item');
1590+
1591+
query.fullTextSearch('size', 'small');
1592+
1593+
expect(query.toJSON()).toEqual({
1594+
where: {
1595+
size: {
1596+
$text: {
1597+
$search: {
1598+
$term: "small"
1599+
}
1600+
}
1601+
}
1602+
}
1603+
});
1604+
});
1605+
1606+
it('full text search with all parameters', () => {
1607+
let query = new ParseQuery('Item');
1608+
1609+
query.fullTextSearch('size', 'medium', 'en', false, true);
1610+
1611+
expect(query.toJSON()).toEqual({
1612+
where: {
1613+
size: {
1614+
$text: {
1615+
$search: {
1616+
$term: "medium",
1617+
$language: "en",
1618+
$caseSensitive: false,
1619+
$diacriticSensitive: true
1620+
}
1621+
}
1622+
}
1623+
}
1624+
});
1625+
1626+
});
1627+
1628+
it('add the score for the full text search', () => {
1629+
let query = new ParseQuery('Item');
1630+
1631+
query.fullTextSearch('size', 'medium', 'fr');
1632+
query.sortByTextScore();
1633+
1634+
expect(query.toJSON()).toEqual({
1635+
where: {
1636+
size: {
1637+
$text: {
1638+
$search: {
1639+
$term: "medium",
1640+
$language: "fr"
1641+
}
1642+
}
1643+
}
1644+
},
1645+
keys : "$score",
1646+
order : "$score"
1647+
});
1648+
1649+
});
15871650
});

0 commit comments

Comments
 (0)