From 08176404493b82b9d66d7d15ccada77e2c8f70d5 Mon Sep 17 00:00:00 2001 From: genja Date: Mon, 29 Jun 2026 00:45:16 +0200 Subject: [PATCH] added patent linking from projects to patents page --- service/src/main/inse/patents/all/logic.py | 51 +++++----- service/src/main/inse/patents/all/service.py | 4 +- .../elastic_search_all_patents_client.py | 94 ++++++++++++------- .../patents/test_elastic_search_client.py | 11 +++ webapp/openapitools.json | 6 +- webapp/src/app/api/api/patents.service.ts | 37 ++++++-- .../patent-detail/patent-detail.component.ts | 18 +++- .../patents-cards.component.html | 2 +- .../patents-cards/patents-cards.component.ts | 6 +- 9 files changed, 151 insertions(+), 78 deletions(-) diff --git a/service/src/main/inse/patents/all/logic.py b/service/src/main/inse/patents/all/logic.py index dd5f0aa9..c3e54ed4 100644 --- a/service/src/main/inse/patents/all/logic.py +++ b/service/src/main/inse/patents/all/logic.py @@ -1,6 +1,6 @@ """Business logic for global patent listing.""" import asyncio - +from datetime import datetime import httpx from fastapi import HTTPException from starlette.status import HTTP_404_NOT_FOUND @@ -19,6 +19,10 @@ from inse.patents.ipc_search_client import IpcSearchClient from inse.patents.search_client import PatentSearchClient + +def normalize_patent_date(date_str: str) -> str: + return datetime.strptime(date_str.strip(), "%Y-%m-%d").strftime("%Y%m%d") + async def get_all_patents( page: int = 0, size: int = 10, @@ -85,58 +89,49 @@ async def get_all_patents( patents=patents, ) - -def _parse_patent_id(patent_id: str) -> tuple[str, str]: - normalized = patent_id.strip().upper() - if normalized.startswith("EP"): - return "EPO", normalized.removeprefix("EP") - if normalized.startswith("US"): - return "USPTO", normalized.removeprefix("US") - raise HTTPException( - status_code=HTTP_404_NOT_FOUND, - detail="Patent not found.", - ) - - async def _search_by_id( client: AllPatentsSearchClient, patent_id: str, + prefix: str | None = None, + filling_date: str | None = None ) -> list[model.PatentEntry]: - try: - source, number = _parse_patent_id(patent_id) - except HTTPException: - return [] - if source == "EPO": - data = await client.search_epo(client.epo_detail_query(number)) - return parse_epo_hits(data) - data = await client.search_uspto(client.uspto_detail_query(number)) - return parse_uspto_hits(data) + if prefix == "US": + data = await client.search_uspto(client.uspto_detail_query(patent_id, filling_date=filling_date)) + return parse_uspto_hits(data) + data = await client.search_epo(client.epo_detail_query(patent_id, filling_date=filling_date)) + return parse_epo_hits(data) async def _search_by_title( client: AllPatentsSearchClient, title: str, + prefix: str | None = None, + filling_date: str | None = None ) -> list[model.PatentEntry]: epo_data, uspto_data = await asyncio.gather( - client.search_epo(client.epo_title_query(title)), - client.search_uspto(client.uspto_title_query(title)), + client.search_epo(client.epo_title_query(title, filling_date=filling_date)), + client.search_uspto(client.uspto_title_query(title,filling_date=filling_date)), ) return parse_epo_hits(epo_data) + parse_uspto_hits(uspto_data) async def get_patent_by_id( patent_id: str, + title: str | None = None, + prefix: str | None = None, + filling_date: str | None = None, ipc_client: IpcSearchClient | None = None, ) -> model.PatentEntry: all_patents_client = AllPatentsSearchClient() ipc_search_client = ipc_client or IpcSearchClient() - + if filling_date: + filling_date = normalize_patent_date(filling_date) try: - patents = await _search_by_id(all_patents_client, patent_id) + patents = await _search_by_id(all_patents_client, patent_id,prefix=prefix, filling_date=filling_date) if not patents: - patents = await _search_by_title(all_patents_client, patent_id) + patents = await _search_by_title(all_patents_client, title,prefix=prefix,filling_date=filling_date) if title else [] if not patents: raise HTTPException( diff --git a/service/src/main/inse/patents/all/service.py b/service/src/main/inse/patents/all/service.py index 002cc94c..4716251c 100644 --- a/service/src/main/inse/patents/all/service.py +++ b/service/src/main/inse/patents/all/service.py @@ -41,5 +41,5 @@ async def get_all_patents( response_model=model.PatentEntry, summary="Get a single patent by its identifier.", ) -async def get_patent_by_id(patent_id: str): - return await logic.get_patent_by_id(patent_id) \ No newline at end of file +async def get_patent_by_id(patent_id: str, title: str | None = None, prefix: str | None = None, filling_date: str | None = None): + return await logic.get_patent_by_id(patent_id, title=title, prefix=prefix, filling_date=filling_date) \ No newline at end of file diff --git a/service/src/main/inse/patents/elastic_search_all_patents_client.py b/service/src/main/inse/patents/elastic_search_all_patents_client.py index d97eadbc..9ba2fcda 100644 --- a/service/src/main/inse/patents/elastic_search_all_patents_client.py +++ b/service/src/main/inse/patents/elastic_search_all_patents_client.py @@ -94,7 +94,10 @@ class AllPatentsSearchClient: ], "query": query, } + + def remove_year(self, patent_id: str) -> str: + return patent_id[4:].strip() def uspto_query( self, page: int = 0, @@ -154,7 +157,21 @@ class AllPatentsSearchClient: "query": query, } - def epo_detail_query(self, doc_number: str) -> dict[str, Any]: + def epo_detail_query(self, doc_number: str,filling_date: str | None = None) -> dict[str, Any]: + query: dict[str, Any] = { + "bool": { + "should": [ + {"term": {"doc_number": doc_number}}, + {"term": {"doc_number": self.remove_year(doc_number)}}, + {"term": {"application_number": self.remove_year(doc_number)}}, + {"term": {"application_number": doc_number}}, + ], + "minimum_should_match": 1, + } + } + if filling_date: + query["bool"]["must"] = [{"term": {"application_filing_date": filling_date}}] + return { "size": 1, "_source": [ @@ -163,18 +180,24 @@ class AllPatentsSearchClient: "application_filing_date", "title_en", "title_de", "abstract", "inventors_name", "applicants_name", "ipc", "cpc", ], - "query": { - "bool": { - "should": [ - {"term": {"doc_number": doc_number}}, - {"term": {"application_number": doc_number}}, - ], - "minimum_should_match": 1, - } - }, + "query": query, } - def uspto_detail_query(self, number: str) -> dict[str, Any]: + def uspto_detail_query(self, number: str,filling_date: str | None = None) -> dict[str, Any]: + query: dict[str, Any] = { + "bool": { + "should": [ + {"term": {"publication_number": number}}, + {"term": {"publication_number": self.remove_year(number)}}, + {"term": {"application_number": number}}, + {"term": {"application_number": self.remove_year(number)}}, + ], + "minimum_should_match": 1, + } + } + if filling_date: + query["bool"]["must"] = [{"term": {"application_date": filling_date}}] + return { "size": 1, "_source": [ @@ -183,18 +206,22 @@ class AllPatentsSearchClient: "application_type", "title", "abstract", "inventors_names", "assignees_orgs", "ipcr", "cpc", "number_of_claims", "status", ], - "query": { - "bool": { - "should": [ - {"term": {"publication_number": number}}, - {"term": {"application_number": number}}, - ], - "minimum_should_match": 1, - } - }, + "query": query, } - def epo_title_query(self, title: str) -> dict[str, Any]: + def epo_title_query(self, title: str,filling_date: str | None = None) -> dict[str, Any]: + query: dict[str, Any] = { + "bool": { + "should": [ + {"match_phrase": {"title_en": title}}, + {"match_phrase": {"title_de": title}}, + ], + "minimum_should_match": 1, + } + } + if filling_date: + query["bool"]["must"] = [{"term": {"application_filing_date": filling_date}}] + return { "size": 1, "_source": [ @@ -203,18 +230,19 @@ class AllPatentsSearchClient: "application_filing_date", "title_en", "title_de", "abstract", "inventors_name", "applicants_name", "ipc", "cpc", ], - "query": { - "bool": { - "should": [ - {"match_phrase": {"title_en": title}}, - {"match_phrase": {"title_de": title}}, - ], - "minimum_should_match": 1, - } - }, + "query": query, } - def uspto_title_query(self, title: str) -> dict[str, Any]: + def uspto_title_query(self, title: str,filling_date: str | None = None) -> dict[str, Any]: + query: dict[str, Any] = { + "bool": { + "should": [{"match_phrase": {"title": title}}], + "minimum_should_match": 1, + } + } + if filling_date: + query["bool"]["must"] = [{"term": {"application_date": filling_date}}] + return { "size": 1, "_source": [ @@ -223,9 +251,9 @@ class AllPatentsSearchClient: "application_type", "title", "abstract", "inventors_names", "assignees_orgs", "ipcr", "cpc", "number_of_claims", "status", ], - "query": {"match_phrase": {"title": title}}, + "query": query, } - + async def search_epo(self, payload: dict[str, Any]) -> dict[str, Any]: return await self._search(EPO_INDEX, payload) diff --git a/service/src/main/inse/patents/test_elastic_search_client.py b/service/src/main/inse/patents/test_elastic_search_client.py index 9e12f5dd..f383c03d 100644 --- a/service/src/main/inse/patents/test_elastic_search_client.py +++ b/service/src/main/inse/patents/test_elastic_search_client.py @@ -3,6 +3,7 @@ import json import httpx import pytest from inse.model import ESIndex +from inse.patents.elastic_search_all_patents_client import AllPatentsSearchClient from inse.patents.elastic_search_client import ( DEFAULT_SEARCH_SIZE, PatentSearchClient, @@ -50,6 +51,16 @@ def test_uspto_query_by_assignee_uses_assignees_orgs_field(): assert "assignees_orgs" in str(payload["query"]) +def test_detail_and_title_queries_skip_empty_date_filters(): + client = AllPatentsSearchClient(api_key="test-key") + + epo_payload = client.epo_title_query("method for generating and using a map") + uspto_payload = client.uspto_detail_query("US12508845") + + assert "must" not in epo_payload["query"]["bool"] + assert "must" not in uspto_payload["query"]["bool"] + + @pytest.mark.anyio async def test_search_epo_posts_to_correct_index(): captured: list[dict] = [] diff --git a/webapp/openapitools.json b/webapp/openapitools.json index 7af6e36f..6e1605ed 100644 --- a/webapp/openapitools.json +++ b/webapp/openapitools.json @@ -12,11 +12,11 @@ "ngVersion": "21.2.14", "providedInRoot": true, "serviceSuffix": "Service", - "withInterfaces": true, + "withInterfaces": false, "enumPropertyNaming": "original", - "fileNaming": "kebab-case" + "fileNaming": "camelCase" } } } } -} +} \ No newline at end of file diff --git a/webapp/src/app/api/api/patents.service.ts b/webapp/src/app/api/api/patents.service.ts index ca79ad52..a0eb3f9f 100644 --- a/webapp/src/app/api/api/patents.service.ts +++ b/webapp/src/app/api/api/patents.service.ts @@ -14,7 +14,10 @@ import { BaseService } from '../api.base.service'; import { PatentEntry } from '../model/patentEntry'; import { PatentsByAuthorResponse } from '../model/patentsByAuthorResponse'; import { PatentsByInstitutionResponse } from '../model/patentsByInstitutionResponse'; -import { OrgPatentsResponse, OrgPatentMatchTypeFilter } from '../model/orgPatentsResponse'; +import { + OrgPatentsResponse, + OrgPatentMatchTypeFilter, +} from '../model/orgPatentsResponse'; export interface AllPatentsResponse { total: number; @@ -265,16 +268,28 @@ export class PatentsService extends BaseService { let localVarQueryParameters = new HttpParams(); if (options?.matchType) { - localVarQueryParameters = localVarQueryParameters.set('match_type', options.matchType); + localVarQueryParameters = localVarQueryParameters.set( + 'match_type', + options.matchType, + ); } if (options?.page !== undefined) { - localVarQueryParameters = localVarQueryParameters.set('page', String(options.page)); + localVarQueryParameters = localVarQueryParameters.set( + 'page', + String(options.page), + ); } if (options?.size !== undefined) { - localVarQueryParameters = localVarQueryParameters.set('size', String(options.size)); + localVarQueryParameters = localVarQueryParameters.set( + 'size', + String(options.size), + ); } if (options?.source) { - localVarQueryParameters = localVarQueryParameters.set('source', options.source); + localVarQueryParameters = localVarQueryParameters.set( + 'source', + options.source, + ); } let localVarHeaders = this.defaultHeaders; @@ -371,9 +386,18 @@ export class PatentsService extends BaseService { ); } - public getPatentById(patentId: string): Observable { + public getPatentById( + patentId: string, + title?: string, + prefix?: string, + fillingDate?: string, + ): Observable { const localVarHeaders = this.defaultHeaders; const { basePath, withCredentials } = this.configuration; + let params = new HttpParams(); + if (title) params = params.set('title', title); + if (prefix) params = params.set('prefix', prefix); + if (fillingDate) params = params.set('filling_date', fillingDate); const localVarPath = `/api/inse-service/patents/${this.configuration.encodeParam( { name: 'patentId', @@ -387,6 +411,7 @@ export class PatentsService extends BaseService { )}`; return this.httpClient.get(`${basePath}${localVarPath}`, { + params, ...(withCredentials ? { withCredentials } : {}), headers: localVarHeaders, }); diff --git a/webapp/src/app/modules/patent-detail/patent-detail.component.ts b/webapp/src/app/modules/patent-detail/patent-detail.component.ts index 53b4dfc7..891b2965 100644 --- a/webapp/src/app/modules/patent-detail/patent-detail.component.ts +++ b/webapp/src/app/modules/patent-detail/patent-detail.component.ts @@ -55,7 +55,14 @@ export class PatentDetailComponent implements OnInit { return; } - this.loadPatent(patentId); + const title = + this.route.snapshot.queryParamMap.get('title') ?? undefined; + const prefix = + this.route.snapshot.queryParamMap.get('prefix') ?? undefined; + const fillingDate = + this.route.snapshot.queryParamMap.get('fillingDate') ?? undefined; + + this.loadPatent(patentId, title, prefix, fillingDate); }); } @@ -106,14 +113,19 @@ export class PatentDetailComponent implements OnInit { return getDescriptionPreview(text); } - private loadPatent(patentId: string): void { + private loadPatent( + patentId: string, + title?: string, + prefix?: string, + fillingDate?: string, + ): void { this.loading = true; this.notFound = false; this.patent = null; this.expandedClassificationKeys.clear(); this.patentsService - .getPatentById(patentId) + .getPatentById(patentId, title, prefix, fillingDate) .pipe(finalize(() => (this.loading = false))) .subscribe({ next: (patent) => { diff --git a/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.html b/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.html index 8da21925..f5df4652 100644 --- a/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.html +++ b/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.html @@ -5,7 +5,7 @@

Intellectual Property Rights ({{ patents.length }})

- @for (ipr of patents; track ipr.title) { + @for (ipr of patents; track ipr.applicationIdentifier) {
@if (ipr.type === 'PATENT') { diff --git a/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.ts b/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.ts index 1d8f46ac..248c91a4 100644 --- a/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.ts +++ b/webapp/src/app/modules/projects/eu-cordis-details/patents-cards/patents-cards.component.ts @@ -16,9 +16,11 @@ export class PatentsCardsComponent { @Input() headerTitle: string = 'IPRs'; private router = inject(Router); searchForPatent(patent: Fp7ProjectIPR): void { - this.router.navigate(['/patents'], { + this.router.navigate([`/patent/${patent.applicationIdentifier}`], { queryParams: { - search: patent.title, + prefix: patent.applicationPrefix, + title: patent.title, + fillingDate: patent.applicationDate, }, }); }