Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[HotFix] Elasticsearch 데이터가 불완전할 때 crossref API call #104

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions backend/src/search/search.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,34 @@ export class SearchController {
if (keywordHasSet) this.batchService.doiBatcher.pushToQueue(0, 0, -1, false, doi);

const paper = await this.searchService.getPaper(doi);
if (paper) {
const origin = new PaperInfoDetail(paper._source);
// 기존에 넣어놨던 데이터에 referenceList라는 key가 없을 수 있다..
if (!origin.referenceList?.length) {
this.batchService.doiBatcher.pushToQueue(0, 1, -1, false, origin.doi || origin.key);
return { ...origin, referenceList: [] };
}
try {
if (paper) {
const origin = new PaperInfoDetail(paper._source);
// 기존에 넣어놨던 데이터에 referenceList라는 key가 없을 수 있다..
if (!origin.referenceList?.length) {
this.batchService.doiBatcher.pushToQueue(0, 1, -1, false, origin.doi || origin.key);
throw new Error('SHOULD_CALL_CROSSREF');
// return { ...origin, referenceList: [] };
}

const references = await this.searchService.multiGet(
origin.referenceList.map((ref) => ref.key).filter(Boolean),
);

// Is it N+1 Problem?
const references = await this.searchService.multiGet(origin.referenceList.map((ref) => ref.key).filter(Boolean));
const referenceList = references.docs.map((doc) => {
const _source = (doc as GetGetResult<PaperInfoDetail>)._source;
return { key: doc._id, ..._source };
});
return { ...origin, referenceList };
const referenceList = references.docs.map((doc) => {
const _source = (doc as GetGetResult<PaperInfoDetail>)._source;
if (!!_source.title || !!_source.authors?.length) throw new Error('SHOULD_CALL_CROSSREF');
return { key: doc._id, ..._source };
});
if (referenceList.length !== origin.references) throw new Error('SHOULD_CALL_CROSSREF');
return { ...origin, referenceList };
}
throw new Error('SHOULD_CALL_CROSSREF');
} catch (err) {
const res = await this.searchService.getPaperFromCrossref(doi);
return res;
// throw new NotFoundException('해당 doi는 존재하지 않습니다. 정보를 수집중입니다.');
}
throw new NotFoundException('해당 doi는 존재하지 않습니다. 정보를 수집중입니다.');
}

@Get('stat')
Expand Down
18 changes: 15 additions & 3 deletions backend/src/search/search.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { CrossRefItem, PaperInfoExtended, PaperInfo, PaperInfoDetail } from './entities/crossRef.entity';
import {
CrossRefItem,
PaperInfoExtended,
PaperInfo,
PaperInfoDetail,
CrossRefPaperResponse,
} from './entities/crossRef.entity';
import { ElasticsearchService } from '@nestjs/elasticsearch';
import { MgetOperation, SearchHit } from '@elastic/elasticsearch/lib/api/types';
import { HttpService } from '@nestjs/axios';
import { CROSSREF_API_PAPER_URL } from '../util';

@Injectable()
export class SearchService {
constructor(private readonly esService: ElasticsearchService) {}
constructor(private readonly esService: ElasticsearchService, private readonly httpService: HttpService) {}

parsePaperInfo = (item: CrossRefItem) => {
const data = {
Expand Down Expand Up @@ -43,6 +51,7 @@ export class SearchService {
reference['volume-title'] ||
reference.unstructured,
doi: reference['DOI'],
authors: reference['author'] ? [reference['author']] : undefined,
};
}) || [];
const data = {
Expand All @@ -52,7 +61,10 @@ export class SearchService {

return new PaperInfoDetail(data);
};

async getPaperFromCrossref(doi: string) {
const item = await this.httpService.axiosRef.get<CrossRefPaperResponse>(CROSSREF_API_PAPER_URL(doi));
return this.parsePaperInfoDetail(item.data.message);
}
async getPaper(doi: string) {
try {
const paper = await this.esService.get<PaperInfoDetail>({ index: process.env.ELASTIC_INDEX, id: doi });
Expand Down