Skip to content

Commit

Permalink
Adding ScoreStrategies feature (#34)
Browse files Browse the repository at this point in the history
- Added model
- Added model unit testing
- Added compilation of the library
  • Loading branch information
mmoreram authored Jan 2, 2019
1 parent 97b7cdd commit 0bc13f5
Show file tree
Hide file tree
Showing 33 changed files with 1,533 additions and 307 deletions.
405 changes: 335 additions & 70 deletions dist/apisearch.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/apisearch.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/apisearch.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/apisearch.min.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/Model/Item.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,12 @@ export declare class Item {
* @returns string
*/
composeUUID(): string;
/**
* Get path by field.
*
* @param field
*
* @returns {string}
*/
static getPathByField(field: string): string;
}
12 changes: 12 additions & 0 deletions lib/Model/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ var Item = /** @class */ (function () {
Item.prototype.composeUUID = function () {
return this.uuid.composedUUID();
};
/**
* Get path by field.
*
* @param field
*
* @returns {string}
*/
Item.getPathByField = function (field) {
return (["id", "type"].indexOf(field) > -1)
? "uuid." + field
: "indexed_metadata." + field;
};
return Item;
}());
exports.Item = Item;
8 changes: 0 additions & 8 deletions lib/Query/Filter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,4 @@ export declare class Filter {
* @returns {Filter}
*/
static createFromArray(array: any): Filter;
/**
* Get path by field.
*
* @param field
*
* @returns {string}
*/
static getFilterPathByField(field: string): string;
}
12 changes: 0 additions & 12 deletions lib/Query/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,6 @@ var Filter = /** @class */ (function () {
}
return Filter.create(array.field, array.values, array.application_type, array.filter_type, array.filter_terms);
};
/**
* Get path by field.
*
* @param field
*
* @returns {string}
*/
Filter.getFilterPathByField = function (field) {
return (["id", "type"].indexOf(field) > -1)
? "uuid." + field
: "indexed_metadata." + field;
};
return Filter;
}());
exports.Filter = Filter;
16 changes: 8 additions & 8 deletions lib/Query/Query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ItemUUID } from "../Model/ItemUUID";
import { User } from "../Model/User";
import { Aggregation } from "./Aggregation";
import { Filter } from "./Filter";
import { ScoreStrategy } from "./ScoreStrategy";
import { ScoreStrategies } from "./ScoreStrategies";
import { SortBy } from "./SortBy";
/**
* Query constants
Expand Down Expand Up @@ -33,7 +33,7 @@ export declare class Query {
private suggestionsEnabled;
private highlightsEnabled;
private filterFields;
private scoreStrategy;
private scoreStrategies;
private fuzziness;
private minScore;
private user;
Expand Down Expand Up @@ -467,17 +467,17 @@ export declare class Query {
*/
excludeUUIDs(...uuids: ItemUUID[]): Query;
/**
* Get score strategy
* Get score strategies
*
* @return {ScoreStrategy}
* @return {ScoreStrategies}
*/
getScoreStrategy(): ScoreStrategy;
getScoreStrategies(): ScoreStrategies;
/**
* Set score strategy
* Set score strategies
*
* @param scoreStrategy
* @param scoreStrategies
*/
setScoreStrategy(scoreStrategy: ScoreStrategy): Query;
setScoreStrategies(scoreStrategies: ScoreStrategies): Query;
/**
* Get fuzziness
*
Expand Down
59 changes: 32 additions & 27 deletions lib/Query/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ exports.__esModule = true;
var tslib_1 = require("tslib");
var Coordinate_1 = require("../Model/Coordinate");
var ItemUUID_1 = require("../Model/ItemUUID");
var Item_1 = require("../Model/Item");
var User_1 = require("../Model/User");
var Aggregation_1 = require("./Aggregation");
var Filter_1 = require("./Filter");
var Filter_2 = require("./Filter");
var Aggregation_2 = require("./Aggregation");
var InvalidFormatError_1 = require("../Error/InvalidFormatError");
var Filter_3 = require("./Filter");
var ScoreStrategy_1 = require("./ScoreStrategy");
var ScoreStrategies_1 = require("./ScoreStrategies");
var SortBy_1 = require("./SortBy");
/**
* Query constants
Expand All @@ -35,6 +36,10 @@ var Query = /** @class */ (function () {
this.filters = {};
this.itemsPromoted = [];
this.aggregations = {};
this.resultsEnabled = true;
this.aggregationsEnabled = true;
this.suggestionsEnabled = false;
this.highlightsEnabled = false;
this.filterFields = [];
this.minScore = exports.NO_MIN_SCORE;
this.sortByInstance = SortBy_1.SortBy.create();
Expand Down Expand Up @@ -144,7 +149,7 @@ var Query = /** @class */ (function () {
*/
Query.prototype.filterUniverseByTypes = function (values) {
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField("type");
var fieldPath = Item_1.Item.getPathByField("type");
if (values.length > 0) {
this.universeFilters = tslib_1.__assign({}, this.universeFilters, (_a = {}, _a["type"] = Filter_1.Filter.create(fieldPath, values, Filter_2.FILTER_AT_LEAST_ONE, Filter_2.FILTER_TYPE_FIELD), _a));
}
Expand All @@ -166,7 +171,7 @@ var Query = /** @class */ (function () {
if (aggregate === void 0) { aggregate = true; }
if (aggregationSort === void 0) { aggregationSort = Aggregation_2.AGGREGATION_SORT_BY_COUNT_DESC; }
var _a, _b;
var fieldPath = Filter_1.Filter.getFilterPathByField("type");
var fieldPath = Item_1.Item.getPathByField("type");
if (values.length > 0) {
this.filters = tslib_1.__assign({}, this.filters, (_a = {}, _a["type"] = Filter_1.Filter.create(fieldPath, values, Filter_2.FILTER_AT_LEAST_ONE, Filter_2.FILTER_TYPE_FIELD), _a));
}
Expand All @@ -187,7 +192,7 @@ var Query = /** @class */ (function () {
*/
Query.prototype.filterUniverseByIds = function (values) {
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField("id");
var fieldPath = Item_1.Item.getPathByField("id");
if (values.length > 0) {
this.universeFilters = tslib_1.__assign({}, this.universeFilters, (_a = {}, _a["id"] = Filter_1.Filter.create(fieldPath, values, Filter_2.FILTER_AT_LEAST_ONE, Filter_2.FILTER_TYPE_FIELD), _a));
}
Expand All @@ -205,7 +210,7 @@ var Query = /** @class */ (function () {
*/
Query.prototype.filterByIds = function (values) {
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField("id");
var fieldPath = Item_1.Item.getPathByField("id");
if (values.length > 0) {
this.filters = tslib_1.__assign({}, this.filters, (_a = {}, _a["id"] = Filter_1.Filter.create(fieldPath, values, Filter_2.FILTER_AT_LEAST_ONE, Filter_2.FILTER_TYPE_FIELD), _a));
}
Expand All @@ -226,7 +231,7 @@ var Query = /** @class */ (function () {
Query.prototype.filterUniverseBy = function (field, values, applicationType) {
if (applicationType === void 0) { applicationType = Filter_2.FILTER_AT_LEAST_ONE; }
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField(field);
var fieldPath = Item_1.Item.getPathByField(field);
if (values.length > 0) {
this.universeFilters = tslib_1.__assign({}, this.universeFilters, (_a = {}, _a[field] = Filter_1.Filter.create(fieldPath, values, applicationType, Filter_2.FILTER_TYPE_FIELD), _a));
}
Expand All @@ -252,7 +257,7 @@ var Query = /** @class */ (function () {
if (aggregate === void 0) { aggregate = true; }
if (aggregationSort === void 0) { aggregationSort = Aggregation_2.AGGREGATION_SORT_BY_COUNT_DESC; }
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField(field);
var fieldPath = Item_1.Item.getPathByField(field);
if (values.length > 0) {
this.filters = tslib_1.__assign({}, this.filters, (_a = {}, _a[filterName] = Filter_1.Filter.create(fieldPath, values, applicationType, Filter_2.FILTER_TYPE_FIELD), _a));
}
Expand All @@ -278,7 +283,7 @@ var Query = /** @class */ (function () {
if (applicationType === void 0) { applicationType = Filter_2.FILTER_AT_LEAST_ONE; }
if (rangeType === void 0) { rangeType = Filter_2.FILTER_TYPE_RANGE; }
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField(field);
var fieldPath = Item_1.Item.getPathByField(field);
if (values.length > 0) {
this.universeFilters = tslib_1.__assign({}, this.universeFilters, (_a = {}, _a[field] = Filter_1.Filter.create(fieldPath, values, applicationType, rangeType), _a));
}
Expand Down Expand Up @@ -320,7 +325,7 @@ var Query = /** @class */ (function () {
if (aggregate === void 0) { aggregate = true; }
if (aggregationSort === void 0) { aggregationSort = Aggregation_2.AGGREGATION_SORT_BY_COUNT_DESC; }
var _a;
var fieldPath = Filter_1.Filter.getFilterPathByField(field);
var fieldPath = Item_1.Item.getPathByField(field);
if (values.length !== 0) {
this.filters = tslib_1.__assign({}, this.filters, (_a = {}, _a[filterName] = Filter_1.Filter.create(fieldPath, values, applicationType, rangeType), _a));
}
Expand Down Expand Up @@ -414,7 +419,7 @@ var Query = /** @class */ (function () {
if (aggregationSort === void 0) { aggregationSort = Aggregation_2.AGGREGATION_SORT_BY_COUNT_DESC; }
if (limit === void 0) { limit = Aggregation_2.AGGREGATION_NO_LIMIT; }
var _a;
this.aggregations = tslib_1.__assign({}, this.aggregations, (_a = {}, _a[filterName] = Aggregation_1.Aggregation.create(filterName, Filter_1.Filter.getFilterPathByField(field), applicationType, Filter_2.FILTER_TYPE_FIELD, [], aggregationSort, limit), _a));
this.aggregations = tslib_1.__assign({}, this.aggregations, (_a = {}, _a[filterName] = Aggregation_1.Aggregation.create(filterName, Item_1.Item.getPathByField(field), applicationType, Filter_2.FILTER_TYPE_FIELD, [], aggregationSort, limit), _a));
return this;
};
/**
Expand All @@ -438,7 +443,7 @@ var Query = /** @class */ (function () {
if (options.length === 0) {
return this;
}
this.aggregations = tslib_1.__assign({}, this.aggregations, (_a = {}, _a[filterName] = Aggregation_1.Aggregation.create(filterName, Filter_1.Filter.getFilterPathByField(field), applicationType, rangeType, options, aggregationSort, limit), _a));
this.aggregations = tslib_1.__assign({}, this.aggregations, (_a = {}, _a[filterName] = Aggregation_1.Aggregation.create(filterName, Item_1.Item.getPathByField(field), applicationType, rangeType, options, aggregationSort, limit), _a));
return this;
};
/**
Expand Down Expand Up @@ -537,7 +542,7 @@ var Query = /** @class */ (function () {
* @return {Filter|null}
*/
Query.prototype.getFilterByField = function (fieldName) {
var fieldPath = Filter_1.Filter.getFilterPathByField(fieldName);
var fieldPath = Item_1.Item.getPathByField(fieldName);
for (var i in this.filters) {
if (this.filters[i].getField() == fieldPath) {
return this.filters[i];
Expand Down Expand Up @@ -745,20 +750,20 @@ var Query = /** @class */ (function () {
return this;
};
/**
* Get score strategy
* Get score strategies
*
* @return {ScoreStrategy}
* @return {ScoreStrategies}
*/
Query.prototype.getScoreStrategy = function () {
return this.scoreStrategy;
Query.prototype.getScoreStrategies = function () {
return this.scoreStrategies;
};
/**
* Set score strategy
* Set score strategies
*
* @param scoreStrategy
* @param scoreStrategies
*/
Query.prototype.setScoreStrategy = function (scoreStrategy) {
this.scoreStrategy = scoreStrategy;
Query.prototype.setScoreStrategies = function (scoreStrategies) {
this.scoreStrategies = scoreStrategies;
return this;
};
/**
Expand Down Expand Up @@ -934,12 +939,12 @@ var Query = /** @class */ (function () {
array.filter_fields = this.filterFields;
}
/**
* Score strategy
* Score strategies
*/
if (this.scoreStrategy instanceof ScoreStrategy_1.ScoreStrategy) {
var scoreStrategyAsArray = this.scoreStrategy.toArray();
if (Object.keys(scoreStrategyAsArray).length > 0) {
array.score_strategy = scoreStrategyAsArray;
if (this.scoreStrategies instanceof ScoreStrategies_1.ScoreStrategies) {
var scoreStrategiesAsArray = this.scoreStrategies.toArray();
if (Object.keys(scoreStrategiesAsArray).length > 0) {
array.score_strategies = scoreStrategiesAsArray;
}
}
if (this.fuzziness !== null) {
Expand Down Expand Up @@ -1061,8 +1066,8 @@ var Query = /** @class */ (function () {
query.filterFields = array.filter_fields instanceof Array
? array.filter_fields
: [];
query.scoreStrategy = array.score_strategy instanceof Object
? ScoreStrategy_1.ScoreStrategy.createFromArray(array.score_stategy)
query.scoreStrategies = array.score_strategies instanceof Object
? ScoreStrategies_1.ScoreStrategies.createFromArray(array.score_strategies)
: null;
query.user = array.user instanceof Object
? User_1.User.createFromArray(array.user)
Expand Down
64 changes: 64 additions & 0 deletions lib/Query/ScoreStrategies.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ScoreStrategy } from "./ScoreStrategy";
/**
* ScoreStrategies constants
*/
export declare const MULTIPLY = "multiply";
export declare const SUM = "sum";
export declare const AVG = "avg";
export declare const MAX = "max";
export declare const MIN = "min";
/**
* ScoreStrategies
*/
export declare class ScoreStrategies {
private scoreStrategies;
private scoreMode;
/**
* Create empty
*
* @param scoreMode
*
* @return {ScoreStrategies}
*/
static createEmpty(scoreMode?: string): ScoreStrategies;
/**
* Add score strategy
*
* @param scoreStrategy
*
* @return {ScoreStrategies}
*/
addScoreStrategy(scoreStrategy: ScoreStrategy): ScoreStrategies;
/**
* Get score strategies
*
* @return {ScoreStrategy[]}
*/
getScoreStrategies(): ScoreStrategy[];
/**
* Get score mode
*
* @return {string}
*/
getScoreMode(): string;
/**
* To array
*
* @return {{
* score_mode: string,
* score_strategies: any
* }}
*/
toArray(): {
score_mode: string;
score_strategies: any;
};
/**
* Create from array
*
* @param array
*
* @return {ScoreStrategies}
*/
static createFromArray(array: any): ScoreStrategies;
}
Loading

0 comments on commit 0bc13f5

Please sign in to comment.