@imranbarbhuiya/mongoose-fuzzy-searching
Version:
Mongoose fuzzy searching plugin
165 lines (164 loc) • 6.02 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryFuzzySearch = exports.StaticFuzzySearch = exports.sort = exports.confidenceScore = void 0;
var ngrams_1 = require("../ngrams");
var utils_1 = require("../utils");
exports.confidenceScore = { confidenceScore: { $meta: "textScore" } };
exports.sort = { sort: { confidenceScore: { $meta: "textScore" } } };
var FuzzySearch = /** @class */ (function () {
function FuzzySearch() {
this._query = "";
this._options = {};
}
FuzzySearch.prototype.getQueryArgs = function () {
if ((0, utils_1.isString)(this.query)) {
return (0, utils_1.prepareQuery)(this.query);
}
var queryObj = this.query;
return (0, utils_1.prepareQuery)(queryObj.query, queryObj);
};
FuzzySearch.prototype.buildQuery = function (query) {
var $text = {
$search: query,
};
if (!(0, utils_1.isObject)(this.options)) {
return { $text: $text };
}
return {
$and: [{ $text: $text }, this.options],
};
};
FuzzySearch.prototype.find = function (model, options) {
return model.find.apply(model, options);
};
Object.defineProperty(FuzzySearch.prototype, "searchQuery", {
get: function () {
var _a = this.getQueryArgs(), prefixOnly = _a.prefixOnly, exact = _a.exact, minSize = _a.minSize, query = _a.query;
if (!query) {
return {};
}
var generatedQuery = exact
? "\"".concat(query, "\"")
: (0, ngrams_1.makeNGrams)((0, utils_1.prepareNgrams)({
text: query,
escapeSpecialCharacters: false,
minSize: minSize,
prefixOnly: prefixOnly,
})).join(" ");
return this.buildQuery(generatedQuery);
},
enumerable: false,
configurable: true
});
Object.defineProperty(FuzzySearch.prototype, "query", {
get: function () {
return this._query;
},
set: function (query) {
this._query = query;
},
enumerable: false,
configurable: true
});
Object.defineProperty(FuzzySearch.prototype, "options", {
get: function () {
return this._options;
},
set: function (options) {
this._options = options;
},
enumerable: false,
configurable: true
});
return FuzzySearch;
}());
var StaticFuzzySearch = /** @class */ (function (_super) {
__extends(StaticFuzzySearch, _super);
function StaticFuzzySearch() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.call(this) || this;
var _a = _this.initializeParameter.apply(_this, args), callback = _a.callback, options = _a.options, query = _a.query;
_this.query = query;
_this.options = options;
_this.callback = callback;
return _this;
}
StaticFuzzySearch.prototype.initializeParameter = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var query = args[0];
var options = {};
var callback;
if (args[1] && (0, utils_1.isFunction)(args[1])) {
callback = args[1];
}
else if (args[1] && (0, utils_1.isObject)(args[1])) {
options = args[1];
}
if (callback === undefined && args[2] && (0, utils_1.isFunction)(args[2])) {
callback = args[2];
}
return { query: query, options: options, callback: callback };
};
StaticFuzzySearch.prototype.search = function (model) {
return this.find(model, [
this.searchQuery,
exports.confidenceScore,
exports.sort,
this.callback,
]);
};
return StaticFuzzySearch;
}(FuzzySearch));
exports.StaticFuzzySearch = StaticFuzzySearch;
var QueryFuzzySearch = /** @class */ (function (_super) {
__extends(QueryFuzzySearch, _super);
function QueryFuzzySearch() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var _this = _super.call(this) || this;
var _a = _this.initializeParameter.apply(_this, args), options = _a.options, query = _a.query;
_this.query = query;
_this.options = options;
return _this;
}
QueryFuzzySearch.prototype.initializeParameter = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var query = args[0];
var options = {};
if (args[1] && (0, utils_1.isObject)(args[1])) {
options = args[1];
}
return { query: query, options: options };
};
QueryFuzzySearch.prototype.search = function (model) {
return this.find(model, [this.searchQuery]);
};
return QueryFuzzySearch;
}(FuzzySearch));
exports.QueryFuzzySearch = QueryFuzzySearch;