Skip to content

Commit

Permalink
feat: thread featureScore, neuralScore and featureScoreWeight through…
Browse files Browse the repository at this point in the history
… from query responses
  • Loading branch information
Wesley Walser committed Dec 15, 2021
1 parent afb79c2 commit 08fc09a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sajari/sdk-js",
"description": "Sajari JavaScript SDK",
"version": "2.7.0",
"version": "2.8.0",
"main": "dist/index.js",
"umd:main": "dist/sajarisdk.umd.production.js",
"module": "dist/sajarisdk.esm.production.js",
Expand Down
23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ class QueryPipeline extends EventEmitter {
}

const results: Result[] = (jsonProto.searchResponse?.results || []).map(
({ indexScore, score, values }, index) => {
({ indexScore, score, values, neuralScore, featureScore }, index) => {
let t: Token | undefined = undefined;
const token = (jsonProto.tokens || [])[index];
if (token !== undefined) {
Expand Down Expand Up @@ -472,6 +472,8 @@ class QueryPipeline extends EventEmitter {
values: processProtoValues(values),
token: t,
promotionPinned,
...(neuralScore && { neuralScore }),
...(featureScore && { featureScore }),
};
}
);
Expand Down Expand Up @@ -501,6 +503,7 @@ class QueryPipeline extends EventEmitter {
aggregateFilters: aggregateFilters,
redirects: redirects,
activePromotions: activePromotions,
featureScoreWeight: jsonProto.searchResponse?.featureScoreWeight || 0,
},
jsonProto.values || {},
];
Expand Down Expand Up @@ -542,17 +545,30 @@ export interface SearchResponse {
* All Promotions activated by the current query (see [[ActivePromotion]]).
*/
activePromotions: ActivePromotion[];

/**
* Feature score weight determines the weighting of featureScore vs neural and index scores.
*/
featureScoreWeight: number;
}

export interface Result {
/**
* indexScore is the index-matched score of this Result.
* indexScore is the index-matched score of this [[Result]].
*/
indexScore: number;
/**
* score is the overall score of this [[Result]].
*/
score: number;
/**
* neuralScore is the neural score of this [[Result]].
*/
neuralScore?: number;
/**
* featureScore is the feature based search score of this [[Result]].
*/
featureScore?: number;
/**
* values is an object of field-value pairs.
*/
Expand Down Expand Up @@ -653,6 +669,7 @@ export interface SearchResponseProto {
results: ResultProto[];
aggregates: AggregatesProto;
aggregateFilters: AggregatesProto;
featureScoreWeight?: number;
}>;
tokens?: TokenProto[];
values?: Record<string, string>;
Expand Down Expand Up @@ -721,6 +738,8 @@ function valueFromProto(value: ValueProto): string | string[] | null {
*/
interface ResultProto {
indexScore: number;
neuralScore?: number;
featureScore?: number;
score: number;
values: Record<string, ValueProto>;
}
Expand Down
2 changes: 1 addition & 1 deletion src/user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ if (scriptTag) {
* user agent of sdk
* @hidden
*/
export const USER_AGENT = ["sajari-sdk-js/2.7.0", suffix]
export const USER_AGENT = ["sajari-sdk-js/2.8.0", suffix]
.filter(Boolean)
.join(" ");
63 changes: 63 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe("Pipeline", () => {
aggregateFilters: {},
redirects: {},
activePromotions: [],
featureScoreWeight: 0,
});

expect(fetchMock.mock.calls.length).toEqual(1);
Expand Down Expand Up @@ -109,6 +110,7 @@ describe("Pipeline", () => {
},
},
activePromotions: [],
featureScoreWeight: 0,
});

expect(fetchMock.mock.calls.length).toEqual(1);
Expand Down Expand Up @@ -161,6 +163,7 @@ describe("Pipeline", () => {
aggregateFilters: {},
redirects: {},
activePromotions: [],
featureScoreWeight: 0,
});
});

Expand Down Expand Up @@ -236,6 +239,7 @@ describe("Pipeline", () => {
aggregates: {},
aggregateFilters: {},
redirects: {},
featureScoreWeight: 0,
activePromotions: [
{
activeExclusions: [],
Expand Down Expand Up @@ -379,6 +383,7 @@ describe("Pipeline", () => {
aggregates: {},
aggregateFilters: {},
redirects: {},
featureScoreWeight: 0,
activePromotions: [
{
activeExclusions: [],
Expand Down Expand Up @@ -421,4 +426,62 @@ describe("Pipeline", () => {
"test.com/sajari.api.pipeline.v1.Query/Search"
);
});

it("search with neural and feature scores", async () => {
const responseObj: SearchResponseProto = {
searchResponse: {
time: "0.003s",
totalResults: "1",
featureScoreWeight: 0.2,
results: [
{
indexScore: 1,
score: 0.4649218,
neuralScore: 0.333,
featureScore: 0.6666,
values: {
title: {
single: "the result",
},
},
},
],
},
};
fetchMock.mockResponseOnce(JSON.stringify(responseObj));

const session = new DefaultSession(TrackingType.None, "url");
const [response, values] = await client
.pipeline("test", "test")
.search({ q: "hello" }, session.next());

expect(values).toEqual({});
expect(response).toStrictEqual({
time: 0.003,
totalResults: 1,
results: [
{
indexScore: 1,
score: 0.4649218,
promotionPinned: false,
token: undefined,
neuralScore: 0.333,
featureScore: 0.6666,
values: {
title: "the result",
},
},
],
aggregates: {},
aggregateFilters: {},
activePromotions: [],
redirects: {},
featureScoreWeight: 0.2,
});

expect(fetchMock.mock.calls.length).toEqual(1);
expect(fetchMock.mock.calls[0][0]).toEqual(
"test.com/sajari.api.pipeline.v1.Query/Search"
);
});
});

0 comments on commit 08fc09a

Please sign in to comment.