@internetarchive/search-service
Advanced tools
Comparing version 1.2.5-alpha.6 to 1.2.5-alpha.7
@@ -14,12 +14,32 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
constructor(body, schema) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
this.schema = schema; | ||
const schemaHitType = schema === null || schema === void 0 ? void 0 : schema.hit_type; | ||
this.totalResults = (_b = (_a = body === null || body === void 0 ? void 0 : body.hits) === null || _a === void 0 ? void 0 : _a.total) !== null && _b !== void 0 ? _b : 0; | ||
this.returnedCount = (_d = (_c = body === null || body === void 0 ? void 0 : body.hits) === null || _c === void 0 ? void 0 : _c.returned) !== null && _d !== void 0 ? _d : 0; | ||
this.results = | ||
(_g = (_f = (_e = body === null || body === void 0 ? void 0 : body.hits) === null || _e === void 0 ? void 0 : _e.hits) === null || _f === void 0 ? void 0 : _f.map((hit) => { var _a; return SearchResponseDetails.createResult((_a = hit.hit_type) !== null && _a !== void 0 ? _a : schemaHitType, hit); })) !== null && _g !== void 0 ? _g : []; | ||
let firstPageElement; | ||
if (body === null || body === void 0 ? void 0 : body.page_elements) { | ||
this.pageElements = body.page_elements; | ||
if (this.pageElements) | ||
firstPageElement = Object.values(this.pageElements)[0]; | ||
} | ||
// Use hits directly from the body if available. | ||
// Otherwise, try extracting them from the first page_element | ||
let hits = (_a = body === null || body === void 0 ? void 0 : body.hits) === null || _a === void 0 ? void 0 : _a.hits; | ||
if (!(hits === null || hits === void 0 ? void 0 : hits.length) && this.pageElements) { | ||
if ((_b = firstPageElement === null || firstPageElement === void 0 ? void 0 : firstPageElement.hits) === null || _b === void 0 ? void 0 : _b.hits) { | ||
hits = firstPageElement.hits.hits; | ||
} | ||
} | ||
// Use aggregations directly from the body if available. | ||
// Otherwise, try extracting them from the first page_element. | ||
let aggregations = body === null || body === void 0 ? void 0 : body.aggregations; | ||
const bodyHasAggregations = this.aggregations && Object.keys(this.aggregations).length > 0; | ||
if (!bodyHasAggregations && this.pageElements && (firstPageElement === null || firstPageElement === void 0 ? void 0 : firstPageElement.aggregations)) { | ||
aggregations = firstPageElement.aggregations; | ||
} | ||
this.totalResults = (_d = (_c = body === null || body === void 0 ? void 0 : body.hits) === null || _c === void 0 ? void 0 : _c.total) !== null && _d !== void 0 ? _d : 0; | ||
this.returnedCount = (_f = (_e = body === null || body === void 0 ? void 0 : body.hits) === null || _e === void 0 ? void 0 : _e.returned) !== null && _f !== void 0 ? _f : 0; | ||
this.results = (_g = hits === null || hits === void 0 ? void 0 : hits.map((hit) => { var _a; return SearchResponseDetails.createResult((_a = hit.hit_type) !== null && _a !== void 0 ? _a : schemaHitType, hit); })) !== null && _g !== void 0 ? _g : []; | ||
// Construct Aggregation objects | ||
if (body === null || body === void 0 ? void 0 : body.aggregations) { | ||
this.aggregations = Object.entries(body.aggregations).reduce((acc, [key, val]) => { | ||
if (aggregations) { | ||
this.aggregations = Object.entries(aggregations).reduce((acc, [key, val]) => { | ||
acc[key] = new Aggregation(val); | ||
@@ -38,21 +58,2 @@ return acc; | ||
} | ||
if (body === null || body === void 0 ? void 0 : body.page_elements) { | ||
this.pageElements = body.page_elements; | ||
// If we didn't receive any hits/aggregations on the response body, | ||
// try finding them in the first page element instead | ||
const firstPageElement = Object.values(this.pageElements)[0]; | ||
if (firstPageElement) { | ||
if (!((_h = this.results) === null || _h === void 0 ? void 0 : _h.length) && firstPageElement.hits) { | ||
this.results = (_k = (_j = firstPageElement.hits.hits) === null || _j === void 0 ? void 0 : _j.map((hit) => { var _a; return SearchResponseDetails.createResult((_a = hit.hit_type) !== null && _a !== void 0 ? _a : schemaHitType, hit); })) !== null && _k !== void 0 ? _k : []; | ||
} | ||
const alreadyHaveAggregations = this.aggregations && Object.keys(this.aggregations).length > 0; | ||
if (!alreadyHaveAggregations && firstPageElement.aggregations) { | ||
const pageElementAggregations = firstPageElement.aggregations; | ||
this.aggregations = Object.entries(pageElementAggregations).reduce((acc, [key, val]) => { | ||
acc[key] = new Aggregation(val); | ||
return acc; | ||
}, {}); | ||
} | ||
} | ||
} | ||
} | ||
@@ -59,0 +60,0 @@ /** |
{ | ||
"name": "@internetarchive/search-service", | ||
"version": "1.2.5-alpha.6", | ||
"version": "1.2.5-alpha.7", | ||
"description": "A search service for the Internet Archive", | ||
@@ -5,0 +5,0 @@ "license": "AGPL-3.0-only", |
@@ -86,12 +86,34 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
let firstPageElement; | ||
if (body?.page_elements) { | ||
this.pageElements = body.page_elements; | ||
if (this.pageElements) firstPageElement = Object.values(this.pageElements)[0]; | ||
} | ||
// Use hits directly from the body if available. | ||
// Otherwise, try extracting them from the first page_element | ||
let hits = body?.hits?.hits; | ||
if (!hits?.length && this.pageElements) { | ||
if (firstPageElement?.hits?.hits) { | ||
hits = firstPageElement.hits.hits; | ||
} | ||
} | ||
// Use aggregations directly from the body if available. | ||
// Otherwise, try extracting them from the first page_element. | ||
let aggregations = body?.aggregations; | ||
const bodyHasAggregations = this.aggregations && Object.keys(this.aggregations).length > 0; | ||
if (!bodyHasAggregations && this.pageElements && firstPageElement?.aggregations) { | ||
aggregations = firstPageElement.aggregations; | ||
} | ||
this.totalResults = body?.hits?.total ?? 0; | ||
this.returnedCount = body?.hits?.returned ?? 0; | ||
this.results = | ||
body?.hits?.hits?.map((hit: SearchResult) => | ||
SearchResponseDetails.createResult(hit.hit_type ?? schemaHitType, hit) | ||
) ?? []; | ||
this.results = hits?.map((hit: SearchResult) => | ||
SearchResponseDetails.createResult(hit.hit_type ?? schemaHitType, hit) | ||
) ?? []; | ||
// Construct Aggregation objects | ||
if (body?.aggregations) { | ||
this.aggregations = Object.entries(body.aggregations).reduce( | ||
if (aggregations) { | ||
this.aggregations = Object.entries(aggregations).reduce( | ||
(acc, [key, val]) => { | ||
@@ -116,29 +138,2 @@ acc[key] = new Aggregation(val); | ||
} | ||
if (body?.page_elements) { | ||
this.pageElements = body.page_elements; | ||
// If we didn't receive any hits/aggregations on the response body, | ||
// try finding them in the first page element instead | ||
const firstPageElement = Object.values(this.pageElements)[0]; | ||
if (firstPageElement) { | ||
if (!this.results?.length && firstPageElement.hits) { | ||
this.results = firstPageElement.hits.hits?.map((hit: SearchResult) => | ||
SearchResponseDetails.createResult(hit.hit_type ?? schemaHitType, hit) | ||
) ?? []; | ||
} | ||
const alreadyHaveAggregations = this.aggregations && Object.keys(this.aggregations).length > 0; | ||
if (!alreadyHaveAggregations && firstPageElement.aggregations) { | ||
const pageElementAggregations = firstPageElement.aggregations as Record<string, Aggregation>; | ||
this.aggregations = Object.entries(pageElementAggregations).reduce( | ||
(acc, [key, val]) => { | ||
acc[key] = new Aggregation(val); | ||
return acc; | ||
}, | ||
{} as Record<string, Aggregation> | ||
); | ||
} | ||
} | ||
} | ||
} | ||
@@ -145,0 +140,0 @@ |
Sorry, the diff of this file is not supported yet
919017
13092