Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 63bad26

Browse files
authored
Added some features in model and query (#24)
* Added some features in model and query - query min score - results item score - select fields in query * Updated version
1 parent 3f94903 commit 63bad26

File tree

6 files changed

+174
-6
lines changed

6 files changed

+174
-6
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apisearch",
3-
"version": "0.2.12",
3+
"version": "0.2.13",
44
"description": "Javascript client for Apisearch.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/Model/Item.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class Item {
1717
private suggest: string[] = [];
1818
private highlights: any = {};
1919
private promoted: boolean = false;
20+
private score: number;
2021

2122
/**
2223
* Constructor
@@ -325,6 +326,28 @@ export class Item {
325326
return this.promoted;
326327
}
327328

329+
/**
330+
* Set score
331+
*
332+
* @param score
333+
*
334+
* @return {Item}
335+
*/
336+
public setScore(score: number) : Item {
337+
this.score = score;
338+
339+
return this;
340+
}
341+
342+
/**
343+
* Get score
344+
*
345+
* @return {number}
346+
*/
347+
public getScore() : number {
348+
return this.score;
349+
}
350+
328351
/**
329352
* To array
330353
*/
@@ -339,6 +362,7 @@ export class Item {
339362
highlights?: {},
340363
is_promoted?: boolean,
341364
distance?: number,
365+
score?: number
342366
} {
343367
const itemAsArray: {
344368
uuid: {},
@@ -351,6 +375,7 @@ export class Item {
351375
highlights?: {},
352376
is_promoted?: boolean,
353377
distance?: number,
378+
score?: number
354379
} = {
355380
uuid: this.uuid.toArray(),
356381
};
@@ -391,6 +416,10 @@ export class Item {
391416
itemAsArray.distance = this.distance;
392417
}
393418

419+
if (typeof this.score != "undefined") {
420+
itemAsArray.score = this.score;
421+
}
422+
394423
return itemAsArray;
395424
}
396425

@@ -448,8 +477,18 @@ export class Item {
448477
item.highlights = array.highlights;
449478
}
450479

451-
if (array.is_promoted) {
452-
item.promoted = true;
480+
if (
481+
typeof array.is_promoted != "undefined" &&
482+
array.is_promoted != null
483+
) {
484+
item.promoted = array.is_promoted;
485+
}
486+
487+
if (
488+
typeof array.score != "undefined" &&
489+
array.score != null
490+
) {
491+
item.score = array.score;
453492
}
454493

455494
return item;

src/Query/Query.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ export const QUERY_DEFAULT_FROM = 0;
3131
export const QUERY_DEFAULT_PAGE = 1;
3232
export const QUERY_DEFAULT_SIZE = 10;
3333
export const QUERY_INFINITE_SIZE = 1000;
34+
export const NO_MIN_SCORE = 0.0;
3435

3536
/**
3637
* Query class
3738
*/
3839
export class Query {
3940

4041
private coordinate: Coordinate;
42+
private fields: string[] = [];
4143
private universeFilters: any = {};
4244
private filters: any = {};
4345
private itemsPromoted: ItemUUID[] = [];
@@ -53,6 +55,7 @@ export class Query {
5355
private filterFields: string[] = [];
5456
private scoreStrategy: ScoreStrategy;
5557
private fuzziness: any;
58+
private minScore: number = NO_MIN_SCORE;
5659
private user: User;
5760

5861
/**
@@ -167,6 +170,28 @@ export class Query {
167170
return query;
168171
}
169172

173+
/**
174+
* set fields
175+
*
176+
* @param fields
177+
*
178+
* @return {Query}
179+
*/
180+
public setFields(fields: string[]) : Query {
181+
this.fields = fields;
182+
183+
return this;
184+
}
185+
186+
/**
187+
* get fields
188+
*
189+
* @return {string[]}
190+
*/
191+
public getFields() : string[] {
192+
return this.fields;
193+
}
194+
170195
/**
171196
* Filter universe by types
172197
*
@@ -1045,6 +1070,28 @@ export class Query {
10451070
return this;
10461071
}
10471072

1073+
/**
1074+
* Get min score
1075+
*
1076+
* @return any
1077+
*/
1078+
public getMinScore():any {
1079+
return this.minScore;
1080+
}
1081+
1082+
/**
1083+
* Set min score
1084+
*
1085+
* @param minScore
1086+
*
1087+
* @return {Query}
1088+
*/
1089+
public setMinScore(minScore: number) : Query {
1090+
this.minScore = minScore;
1091+
1092+
return this;
1093+
}
1094+
10481095
/**
10491096
* By user
10501097
*
@@ -1094,6 +1141,16 @@ export class Query {
10941141
array.coordinate = this.coordinate.toArray();
10951142
}
10961143

1144+
/**
1145+
* Fields
1146+
*/
1147+
if (
1148+
this.fields instanceof Array &&
1149+
this.fields.length > 0
1150+
) {
1151+
array.fields = this.fields;
1152+
}
1153+
10971154
/**
10981155
* Universe Filters
10991156
*/
@@ -1202,6 +1259,14 @@ export class Query {
12021259
array.fuzziness = this.fuzziness;
12031260
}
12041261

1262+
/**
1263+
* Min score
1264+
*/
1265+
const minScore = this.minScore;
1266+
if (minScore !== NO_MIN_SCORE) {
1267+
array.min_score = minScore;
1268+
}
1269+
12051270
/**
12061271
* User
12071272
*/
@@ -1248,6 +1313,13 @@ export class Query {
12481313
array.size ? array.size : QUERY_DEFAULT_SIZE,
12491314
);
12501315

1316+
/**
1317+
* Fields
1318+
*/
1319+
query.fields = array.fields instanceof Array
1320+
? array.fields
1321+
: [];
1322+
12511323
/**
12521324
* Aggregations
12531325
*/
@@ -1312,6 +1384,7 @@ export class Query {
13121384
: false;
13131385

13141386
query.fuzziness = array.fuzziness;
1387+
query.minScore = array.min_score ? array.min_score : NO_MIN_SCORE;
13151388

13161389
/**
13171390
* Items promoted

test/Model/Item.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Model/', () => {
2424
expect(item.getExactMatchingMetadata()).to.be.deep.equal([]);
2525
expect(item.getSuggest()).to.be.deep.equal([]);
2626
expect(item.getDistance()).to.be.equal(undefined);
27+
expect(item.getScore()).to.be.equal(undefined);
2728
});
2829
});
2930

@@ -203,6 +204,31 @@ describe('Model/', () => {
203204
expect(Item.createFromArray(itemAsArray).toArray()).to.be.deep.equal(itemAsArray);
204205
});
205206

207+
describe('-> Test score', () => {
208+
it('should work properly', () => {
209+
let item = Item.createFromArray({
210+
'uuid': {
211+
'id': '1',
212+
'type': 'product'
213+
}
214+
});
215+
expect(item.getScore()).to.be.equals(undefined);
216+
expect(item.toArray().score).to.be.equals(undefined);
217+
218+
item = Item.createFromArray({
219+
'uuid': {
220+
'id': '1',
221+
'type': 'product'
222+
},
223+
'score': 2.4
224+
});
225+
expect(item.getScore()).to.be.equals(2.4);
226+
expect(item.toArray().score).to.be.equals(2.4);
227+
expect(item.setScore(3.3).getScore()).to.be.equals(3.3);
228+
229+
});
230+
});
231+
206232
describe('.composedUUID() should work', () => {
207233
let composedId = '1~product';
208234
expect(Item.create(ItemUUID.createByComposedUUID(composedId)).composeUUID())

test/Query/Query.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { expect } from 'chai';
55
import {ItemUUID} from "../../src/Model/ItemUUID";
66
import {User} from "../../src/Model/User";
7-
import {Query} from '../../src/Query/Query';
7+
import {Query, NO_MIN_SCORE} from '../../src/Query/Query';
88
import {Polygon, Square, CoordinateAndDistance} from "../../src/Geo/LocationRange";
99
import {Coordinate} from "../../src/Model/Coordinate";
1010

@@ -455,5 +455,35 @@ describe('Query()', () => {
455455
expect(Query.createMatchAll().setAutoFuzziness().getFuzziness()).to.be.equals('AUTO');
456456
expect(query.setAutoFuzziness()).to.be.instanceOf(Query);
457457
});
458-
})
458+
});
459+
460+
describe('-> Test fields', () => {
461+
it('should work properly', () => {
462+
let query = Query.createMatchAll();
463+
expect(query.getFields()).to.be.deep.equals([]);
464+
expect(query.toArray().fields).to.be.undefined;
465+
expect(Query.createFromArray({}).getFields()).to.be.deep.equals([]);
466+
query = Query.createMatchAll().setFields(['a', 'b']);
467+
expect(query.getFields()).to.be.deep.equals(['a', 'b']);
468+
expect(query.toArray().fields).to.be.deep.equals(['a', 'b']);
469+
expect(Query.createFromArray({
470+
fields: ['a', 'b']
471+
}).getFields()).to.be.deep.equals(['a', 'b']);
472+
});
473+
});
474+
475+
describe('-> Test min score', () => {
476+
it('should work properly', () => {
477+
let query = Query.createMatchAll();
478+
expect(query.getMinScore()).to.be.deep.equals(NO_MIN_SCORE);
479+
expect(query.toArray().min_score).to.be.undefined;
480+
expect(Query.createFromArray({}).getMinScore()).to.be.deep.equals(NO_MIN_SCORE);
481+
query = Query.createMatchAll().setMinScore(4.5);
482+
expect(query.getMinScore()).to.be.deep.equals(4.5);
483+
expect(query.toArray().min_score).to.be.deep.equals(4.5);
484+
expect(Query.createFromArray({
485+
min_score: 4.5
486+
}).getMinScore()).to.be.deep.equals(4.5);
487+
});
488+
});
459489
});

0 commit comments

Comments
 (0)