From f5868ba34991fc10d60a3460f8bb2514a179e47f Mon Sep 17 00:00:00 2001 From: folland87 Date: Thu, 26 Sep 2024 09:46:37 +0200 Subject: [PATCH] fix(api): Fix query filters to get organization patents --- client/src/api/organizations/[id]/index.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/client/src/api/organizations/[id]/index.ts b/client/src/api/organizations/[id]/index.ts index 2b7c6b62..6bd8d4b2 100644 --- a/client/src/api/organizations/[id]/index.ts +++ b/client/src/api/organizations/[id]/index.ts @@ -1,4 +1,4 @@ -import { organizationsIndex, postHeaders, publicationsIndex, projectsIndex, patentsIndex } from "../../../config/api" +import { organizationsIndex, patentsIndex, postHeaders, projectsIndex, publicationsIndex } from "../../../config/api" import { Organization } from "../../../types/organization" import { publicationTypeMapping } from "../../../utils/string" import { fillWithMissingYears } from "../../utils/years" @@ -13,18 +13,18 @@ export async function getOrganizationById(id: string): Promise { filter: [{term: { "id.keyword": id }}] } }, - } + } const structureQuery = fetch(`${organizationsIndex}/_search`, { method: 'POST', body: JSON.stringify(body), headers: postHeaders }) .then(r => r.json()) const publicationsQuery = getStructurePublicationsById(id) const projectsQuery = getStructureProjectsById(id) const patentsQuery = getStructurePatentsById(id) const [structure, publications, projects, patents] = await Promise.all([structureQuery, publicationsQuery, projectsQuery, patentsQuery]) - + const structureData = structure?.hits?.hits?.[0]?._source if (!structureData) throw new Error('404') const { _id } = structure?.hits?.hits?.[0] || {} - + return { ...structureData, _id, publications, projects, patents } } @@ -72,7 +72,7 @@ async function getStructurePublicationsById(id: string): Promise { } const res = await fetch(`${publicationsIndex}/_search`, { method: 'POST', body: JSON.stringify(body), headers: postHeaders }) const data = await res.json() - + const aggregations = data?.aggregations || {} const publicationsCount = data?.hits?.total?.value || 0 const byWiki = aggregations?.byWiki?.wiki?.buckets?.map((element) => { @@ -118,7 +118,7 @@ async function getStructurePublicationsById(id: string): Promise { normalizedCount: element.doc_count * 100 / _100Authors, } }).filter(el => el) || []; - + return { publicationsCount, byYear, byType, bySource, byAuthors, byWiki } || {} } @@ -157,7 +157,7 @@ async function getStructureProjectsById(id: string): Promise { `${projectsIndex}/_search`, { method: 'POST', body: JSON.stringify(body), headers: postHeaders }) const result = await res.json() - + const { aggregations: data} = result; const projectsCount = result?.hits?.total?.value || 0 const _100Year = data?.byYear?.buckets && Math.max(...data.byYear.buckets.map((el) => el.doc_count)); @@ -193,7 +193,7 @@ async function getStructureProjectsById(id: string): Promise { async function getStructurePatentsById(id: string): Promise { const body: any = { size: 0, - query: { bool: { filter: [{ term: { "affiliations.id.keyword": id } }] } }, + query: { bool: { filter: [{ term: { "applicants.ids.id.keyword": id } }] } }, aggs: { byYear: { terms: { @@ -208,7 +208,7 @@ async function getStructurePatentsById(id: string): Promise { `${patentsIndex}/_search`, { method: 'POST', body: JSON.stringify(body), headers: postHeaders }) const result = await res.json() - + const { aggregations: data} = result; const patentsCount = result?.hits?.total?.value || 0 const _100Year = data?.byYear?.buckets && Math.max(...data.byYear.buckets.map((el) => el.doc_count)); @@ -221,4 +221,4 @@ async function getStructurePatentsById(id: string): Promise { } }).sort((a, b) => a.label - b.label).reduce(fillWithMissingYears, []) || []; return { byYear, patentsCount } -} \ No newline at end of file +}