Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apisearch",
"version": "0.2.12",
"version": "0.2.13",
"description": "Javascript client for Apisearch.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
43 changes: 41 additions & 2 deletions src/Model/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Item {
private suggest: string[] = [];
private highlights: any = {};
private promoted: boolean = false;
private score: number;

/**
* Constructor
Expand Down Expand Up @@ -325,6 +326,28 @@ export class Item {
return this.promoted;
}

/**
* Set score
*
* @param score
*
* @return {Item}
*/
public setScore(score: number) : Item {
this.score = score;

return this;
}

/**
* Get score
*
* @return {number}
*/
public getScore() : number {
return this.score;
}

/**
* To array
*/
Expand All @@ -339,6 +362,7 @@ export class Item {
highlights?: {},
is_promoted?: boolean,
distance?: number,
score?: number
} {
const itemAsArray: {
uuid: {},
Expand All @@ -351,6 +375,7 @@ export class Item {
highlights?: {},
is_promoted?: boolean,
distance?: number,
score?: number
} = {
uuid: this.uuid.toArray(),
};
Expand Down Expand Up @@ -391,6 +416,10 @@ export class Item {
itemAsArray.distance = this.distance;
}

if (typeof this.score != "undefined") {
itemAsArray.score = this.score;
}

return itemAsArray;
}

Expand Down Expand Up @@ -448,8 +477,18 @@ export class Item {
item.highlights = array.highlights;
}

if (array.is_promoted) {
item.promoted = true;
if (
typeof array.is_promoted != "undefined" &&
array.is_promoted != null
) {
item.promoted = array.is_promoted;
}

if (
typeof array.score != "undefined" &&
array.score != null
) {
item.score = array.score;
}

return item;
Expand Down
73 changes: 73 additions & 0 deletions src/Query/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export const QUERY_DEFAULT_FROM = 0;
export const QUERY_DEFAULT_PAGE = 1;
export const QUERY_DEFAULT_SIZE = 10;
export const QUERY_INFINITE_SIZE = 1000;
export const NO_MIN_SCORE = 0.0;

/**
* Query class
*/
export class Query {

private coordinate: Coordinate;
private fields: string[] = [];
private universeFilters: any = {};
private filters: any = {};
private itemsPromoted: ItemUUID[] = [];
Expand All @@ -53,6 +55,7 @@ export class Query {
private filterFields: string[] = [];
private scoreStrategy: ScoreStrategy;
private fuzziness: any;
private minScore: number = NO_MIN_SCORE;
private user: User;

/**
Expand Down Expand Up @@ -167,6 +170,28 @@ export class Query {
return query;
}

/**
* set fields
*
* @param fields
*
* @return {Query}
*/
public setFields(fields: string[]) : Query {
this.fields = fields;

return this;
}

/**
* get fields
*
* @return {string[]}
*/
public getFields() : string[] {
return this.fields;
}

/**
* Filter universe by types
*
Expand Down Expand Up @@ -1045,6 +1070,28 @@ export class Query {
return this;
}

/**
* Get min score
*
* @return any
*/
public getMinScore():any {
return this.minScore;
}

/**
* Set min score
*
* @param minScore
*
* @return {Query}
*/
public setMinScore(minScore: number) : Query {
this.minScore = minScore;

return this;
}

/**
* By user
*
Expand Down Expand Up @@ -1094,6 +1141,16 @@ export class Query {
array.coordinate = this.coordinate.toArray();
}

/**
* Fields
*/
if (
this.fields instanceof Array &&
this.fields.length > 0
) {
array.fields = this.fields;
}

/**
* Universe Filters
*/
Expand Down Expand Up @@ -1202,6 +1259,14 @@ export class Query {
array.fuzziness = this.fuzziness;
}

/**
* Min score
*/
const minScore = this.minScore;
if (minScore !== NO_MIN_SCORE) {
array.min_score = minScore;
}

/**
* User
*/
Expand Down Expand Up @@ -1248,6 +1313,13 @@ export class Query {
array.size ? array.size : QUERY_DEFAULT_SIZE,
);

/**
* Fields
*/
query.fields = array.fields instanceof Array
? array.fields
: [];

/**
* Aggregations
*/
Expand Down Expand Up @@ -1312,6 +1384,7 @@ export class Query {
: false;

query.fuzziness = array.fuzziness;
query.minScore = array.min_score ? array.min_score : NO_MIN_SCORE;

/**
* Items promoted
Expand Down
26 changes: 26 additions & 0 deletions test/Model/Item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Model/', () => {
expect(item.getExactMatchingMetadata()).to.be.deep.equal([]);
expect(item.getSuggest()).to.be.deep.equal([]);
expect(item.getDistance()).to.be.equal(undefined);
expect(item.getScore()).to.be.equal(undefined);
});
});

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

describe('-> Test score', () => {
it('should work properly', () => {
let item = Item.createFromArray({
'uuid': {
'id': '1',
'type': 'product'
}
});
expect(item.getScore()).to.be.equals(undefined);
expect(item.toArray().score).to.be.equals(undefined);

item = Item.createFromArray({
'uuid': {
'id': '1',
'type': 'product'
},
'score': 2.4
});
expect(item.getScore()).to.be.equals(2.4);
expect(item.toArray().score).to.be.equals(2.4);
expect(item.setScore(3.3).getScore()).to.be.equals(3.3);

});
});

describe('.composedUUID() should work', () => {
let composedId = '1~product';
expect(Item.create(ItemUUID.createByComposedUUID(composedId)).composeUUID())
Expand Down
34 changes: 32 additions & 2 deletions test/Query/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { expect } from 'chai';
import {ItemUUID} from "../../src/Model/ItemUUID";
import {User} from "../../src/Model/User";
import {Query} from '../../src/Query/Query';
import {Query, NO_MIN_SCORE} from '../../src/Query/Query';
import {Polygon, Square, CoordinateAndDistance} from "../../src/Geo/LocationRange";
import {Coordinate} from "../../src/Model/Coordinate";

Expand Down Expand Up @@ -455,5 +455,35 @@ describe('Query()', () => {
expect(Query.createMatchAll().setAutoFuzziness().getFuzziness()).to.be.equals('AUTO');
expect(query.setAutoFuzziness()).to.be.instanceOf(Query);
});
})
});

describe('-> Test fields', () => {
it('should work properly', () => {
let query = Query.createMatchAll();
expect(query.getFields()).to.be.deep.equals([]);
expect(query.toArray().fields).to.be.undefined;
expect(Query.createFromArray({}).getFields()).to.be.deep.equals([]);
query = Query.createMatchAll().setFields(['a', 'b']);
expect(query.getFields()).to.be.deep.equals(['a', 'b']);
expect(query.toArray().fields).to.be.deep.equals(['a', 'b']);
expect(Query.createFromArray({
fields: ['a', 'b']
}).getFields()).to.be.deep.equals(['a', 'b']);
});
});

describe('-> Test min score', () => {
it('should work properly', () => {
let query = Query.createMatchAll();
expect(query.getMinScore()).to.be.deep.equals(NO_MIN_SCORE);
expect(query.toArray().min_score).to.be.undefined;
expect(Query.createFromArray({}).getMinScore()).to.be.deep.equals(NO_MIN_SCORE);
query = Query.createMatchAll().setMinScore(4.5);
expect(query.getMinScore()).to.be.deep.equals(4.5);
expect(query.toArray().min_score).to.be.deep.equals(4.5);
expect(Query.createFromArray({
min_score: 4.5
}).getMinScore()).to.be.deep.equals(4.5);
});
});
});