fix: es query script score syntax fix and add error handler for 404 error (#607)
closes https://github.com/cnpm/cnpmcore/issues/598
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { AccessLevel, Inject, SingletonProto } from '@eggjs/tegg';
|
||||
import type { estypes } from '@elastic/elasticsearch';
|
||||
import { estypes, errors } from '@elastic/elasticsearch';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { AbstractService } from '../../common/AbstractService';
|
||||
@@ -138,7 +138,16 @@ export class PackageSearchService extends AbstractService {
|
||||
}
|
||||
|
||||
async removePackage(fullname: string) {
|
||||
return await this.searchRepository.removePackage(fullname);
|
||||
try {
|
||||
return await this.searchRepository.removePackage(fullname);
|
||||
} catch (error) {
|
||||
// if the package does not exist, returns success
|
||||
if (error instanceof errors.ResponseError && error?.statusCode === 404) {
|
||||
this.logger.warn('[PackageSearchService.removePackage] remove package:%s not found', fullname);
|
||||
return fullname;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/npms-io/queries/blob/master/lib/search.js#L8C1-L78C2
|
||||
@@ -213,7 +222,7 @@ export class PackageSearchService extends AbstractService {
|
||||
private _buildScriptScore(params: { text: string | undefined, scoreEffect: number }) {
|
||||
// keep search simple, only download(popularity)
|
||||
const downloads = 'doc["downloads.all"].value';
|
||||
const source = `doc["package.name.raw"].value.equals("${params.text}") ? 100000 + ${downloads} : _score * Math.pow(${downloads}, ${params.scoreEffect})`;
|
||||
const source = `doc["package.name.raw"].value.equals(params.text) ? 100000 + ${downloads} : _score * Math.pow(${downloads}, params.scoreEffect)`;
|
||||
return {
|
||||
script: {
|
||||
source,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import assert from 'assert';
|
||||
import { app, mock } from 'egg-mock/bootstrap';
|
||||
import { errors } from '@elastic/elasticsearch';
|
||||
|
||||
import { mockES } from '../../../../config/config.unittest';
|
||||
import { TestUtil } from '../../../TestUtil';
|
||||
@@ -123,5 +124,42 @@ describe('test/port/controller/package/SearchPackageController.test.ts', () => {
|
||||
.set('authorization', admin.authorization);
|
||||
assert.equal(res.body.package, name);
|
||||
});
|
||||
|
||||
it('should delete a non existent package', async () => {
|
||||
const name = 'non-existent-search-package';
|
||||
mockES.add({
|
||||
method: 'DELETE',
|
||||
path: `/${app.config.cnpmcore.elasticsearchIndex}/_doc/:id`,
|
||||
}, () => {
|
||||
return new errors.ResponseError({
|
||||
body: { errors: {}, status: 404 },
|
||||
statusCode: 404,
|
||||
warnings: null,
|
||||
meta: {
|
||||
name: '',
|
||||
context: '',
|
||||
request: {
|
||||
params: {
|
||||
method: 'delete',
|
||||
path: `/${app.config.cnpmcore.elasticsearchIndex}/_doc/:id`,
|
||||
},
|
||||
options: {},
|
||||
id: '',
|
||||
},
|
||||
connection: null,
|
||||
attempts: 1,
|
||||
aborted: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true);
|
||||
mock(app.config.cnpmcore, 'enableElasticsearch', true);
|
||||
mock(app.config.cnpmcore, 'registry', 'https://registry.example.com');
|
||||
|
||||
const res = await app.httpRequest()
|
||||
.delete(`/-/v1/search/sync/${name}`)
|
||||
.set('authorization', admin.authorization);
|
||||
assert.equal(res.body.package, name);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user