Skip to content
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
6 changes: 6 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ function buildFilterQuery(params) {
});
}

if (params.scope) {
filters.must.push({
term: { 'package.scope': params.scope },
});
}

if (params.keywords.include.length) {
filters.must.push({
terms: { 'package.keywords.raw': params.keywords.include }, // Operator is OR
Expand Down
6 changes: 4 additions & 2 deletions lib/util/parseSearchQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ const uniq = require('lodash/uniq');
const assignWith = require('lodash/assignWith');

const qualifiers = [
'author', 'maintainer', 'keywords', 'not', 'is', 'boost-exact',
'author', 'maintainer', 'scope', 'keywords', 'not', 'is', 'boost-exact',
'score-effect', 'popularity-weight', 'quality-weight', 'maintenance-weight',
];

const paramsDefaults = {
text: null,
author: null,
maintainer: null,
scope: null,
keywords: [],
not: [],
is: [],
Expand All @@ -31,6 +32,7 @@ const paramsDefaults = {
const paramsSchema = Joi.object({
text: Joi.string().trim().lowercase().min(1).max(250), // trim + lowercase so that the exact boost works correctly
author: Joi.string().trim().lowercase().min(1).max(250), // trim + lowercase to mimic the raw analyzer
scope: Joi.string().trim().lowercase().min(1).max(250), // trim + lowercase to mimic the raw analyzer
maintainer: Joi.string().trim().lowercase().min(1).max(250), // trim + lowercase to mimic the raw analyzer
keywords: Joi.array().items(Joi.string().trim().lowercase().min(1).max(50)).min(1).max(10).single(), // trim + lowercase to mimic the raw analyzer
not: Joi.array().items(Joi.string().trim().valid(['deprecated', 'unstable', 'insecure'])).single(),
Expand All @@ -41,7 +43,7 @@ const paramsSchema = Joi.object({
popularityWeight: Joi.number().min(0).max(100),
maintenanceWeight: Joi.number().min(0).max(100),
}).required();
const mandatoryParams = ['text', 'author', 'maintainer', 'keywords', 'not', 'is'];
const mandatoryParams = ['text', 'author', 'maintainer', 'scope', 'keywords', 'not', 'is'];

function normalizeWeights(params) {
const minMax = [0, params.qualityWeight + params.popularityWeight + params.maintenanceWeight];
Expand Down
246 changes: 246 additions & 0 deletions test/fixtures/search-scope-bcoe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
[
{
"scope": "http://127.0.0.1:9200",
"method": "POST",
"path": "/npms-current/score/_search",
"body": {
"size": 25,
"from": 0,
"query": {
"function_score": {
"boost_mode": "replace",
"query": {
"bool": {
"must": [
{
"term": {
"package.scope": "bcoe"
}
}
],
"must_not": [],
"should": [],
"minimum_should_match": 1
}
},
"script_score": {
"lang": "groovy",
"script": "doc[\"package.name.raw\"].value.equals(text) ? 100000 + (doc[\"score.detail.popularity\"].value * popularityWeight + doc[\"score.detail.quality\"].value * qualityWeight + doc[\"score.detail.maintenance\"].value * maintenanceWeight) : _score * pow((doc[\"score.detail.popularity\"].value * popularityWeight + doc[\"score.detail.quality\"].value * qualityWeight + doc[\"score.detail.maintenance\"].value * maintenanceWeight), scoreEffect)",
"params": {
"text": "",
"scoreEffect": 15.3,
"qualityWeight": 0.26027397260273977,
"popularityWeight": 0.452054794520548,
"maintenanceWeight": 0.28767123287671237
}
}
}
}
},
"status": 200,
"response": {
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 5.382989e-14,
"hits": [
{
"_index": "npms-1490562065235",
"_type": "score",
"_id": "@bcoe/scoped-search-3",
"_score": 5.382989e-14,
"_source": {
"package": {
"name": "@bcoe/scoped-search-3",
"version": "1.0.1",
"description": "testing a scoped package search (abc123)",
"date": "2017-03-26T21:08:11.992Z",
"links": {
"npm": "https://www.npmjs.com/package/%40bcoe%2Fscoped-search-3"
},
"author": {
"name": "Ben Coe",
"email": "ben@npmjs.com",
"username": "bcoe"
},
"publisher": {
"username": "bcoe",
"email": "ben@npmjs.com"
},
"maintainers": [
{
"username": "bcoe",
"email": "ben@npmjs.com"
}
],
"scope": "bcoe"
},
"evaluation": {
"quality": {
"carefulness": 0.41550000000000004,
"tests": 0,
"health": 1,
"branding": 0
},
"popularity": {
"communityInterest": 0,
"downloadsCount": 0,
"downloadsAcceleration": 0,
"dependentsCount": 0
},
"maintenance": {
"releasesFrequency": 0,
"commitsFrequency": 0,
"openIssues": 0,
"issuesDistribution": 0
}
},
"score": {
"final": 0.13258731824560602,
"detail": {
"quality": 0.44195772748535345,
"popularity": 0,
"maintenance": 0
}
}
}
},
{
"_index": "npms-1490562065235",
"_type": "score",
"_id": "@bcoe/scoped-search-2",
"_score": 5.382989e-14,
"_source": {
"package": {
"name": "@bcoe/scoped-search-2",
"version": "1.0.0",
"description": "testing a private package being published by an org",
"date": "2017-03-26T21:05:50.344Z",
"links": {
"npm": "https://www.npmjs.com/package/%40bcoe%2Fscoped-search-2"
},
"author": {
"name": "Ben Coe",
"email": "ben@npmjs.com",
"username": "bcoe"
},
"publisher": {
"username": "bcoe",
"email": "ben@npmjs.com"
},
"maintainers": [
{
"username": "bcoe",
"email": "ben@npmjs.com"
}
],
"scope": "bcoe"
},
"evaluation": {
"quality": {
"carefulness": 0.41550000000000004,
"tests": 0,
"health": 1,
"branding": 0
},
"popularity": {
"communityInterest": 0,
"downloadsCount": 0,
"downloadsAcceleration": 0,
"dependentsCount": 0
},
"maintenance": {
"releasesFrequency": 0,
"commitsFrequency": 0,
"openIssues": 0,
"issuesDistribution": 0
}
},
"score": {
"final": 0.13258731824560602,
"detail": {
"quality": 0.44195772748535345,
"popularity": 0,
"maintenance": 0
}
}
}
},
{
"_index": "npms-1490562065235",
"_type": "score",
"_id": "@bcoe/scoped-search-1",
"_score": 5.382989e-14,
"_source": {
"package": {
"name": "@bcoe/scoped-search-1",
"version": "1.0.0",
"description": "testing a private package being published by an org",
"date": "2017-03-26T21:05:26.130Z",
"links": {
"npm": "https://www.npmjs.com/package/%40bcoe%2Fscoped-search-1"
},
"author": {
"name": "Ben Coe",
"email": "ben@npmjs.com",
"username": "bcoe"
},
"publisher": {
"username": "bcoe",
"email": "ben@npmjs.com"
},
"maintainers": [
{
"username": "bcoe",
"email": "ben@npmjs.com"
}
],
"scope": "bcoe"
},
"evaluation": {
"quality": {
"carefulness": 0.41550000000000004,
"tests": 0,
"health": 1,
"branding": 0
},
"popularity": {
"communityInterest": 0,
"downloadsCount": 0,
"downloadsAcceleration": 0,
"dependentsCount": 0
},
"maintenance": {
"releasesFrequency": 0,
"commitsFrequency": 0,
"openIssues": 0,
"issuesDistribution": 0
}
},
"score": {
"final": 0.13258731824560602,
"detail": {
"quality": 0.44195772748535345,
"popularity": 0,
"maintenance": 0
}
}
}
}
]
}
},
"rawHeaders": [
"Content-Type",
"application/json; charset=UTF-8",
"Content-Length",
"2980"
]
}
]
13 changes: 13 additions & 0 deletions test/spec/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ describe('search()', () => {
});
});

it('should allow to search all packages by scope', () => {
let nockDone;

nockBack('search-scope-bcoe.json', (_nockDone) => { nockDone = _nockDone; });

return queries.search('scope:bcoe', localEsClient)
.then((res) => {
expect(res.results).to.have.length(3);
res.results.forEach((result) => expect(result.package.scope).to.equal('bcoe'));
nockDone();
});
});

it('should make use of options.from and options.size', () => {
nock('http://127.0.0.1:9200')
.post('/npms-current/score/_search', (post) => {
Expand Down
6 changes: 6 additions & 0 deletions test/spec/util/parseSearchQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('util/parseSearchQuery()', () => {
text: 'foo',
author: null,
maintainer: null,
scope: null,
keywords: { include: [], exclude: [] },
not: [],
is: [],
Expand All @@ -29,6 +30,7 @@ describe('util/parseSearchQuery()', () => {
text: null,
author: 'satazor',
maintainer: 'sindresorhus',
scope: null,
keywords: { include: [], exclude: [] },
not: [],
is: [],
Expand All @@ -45,6 +47,7 @@ describe('util/parseSearchQuery()', () => {
text: null,
author: null,
maintainer: null,
scope: null,
keywords: { include: ['gulpplugin', 'gulp'], exclude: ['jquery', 'jq'] },
not: [],
is: [],
Expand All @@ -61,6 +64,7 @@ describe('util/parseSearchQuery()', () => {
text: 'foo',
author: null,
maintainer: null,
scope: null,
keywords: { include: [], exclude: [] },
not: ['deprecated', 'unstable'],
is: ['insecure'],
Expand All @@ -77,6 +81,7 @@ describe('util/parseSearchQuery()', () => {
text: 'foo',
author: null,
maintainer: null,
scope: null,
keywords: { include: [], exclude: [] },
not: [],
is: [],
Expand All @@ -97,6 +102,7 @@ maintenance-weight:foo keywords:gulp foo');
keywords: { include: ['gulp'], exclude: [] },
author: null,
maintainer: null,
scope: null,
not: [],
is: [],
boostExact: true,
Expand Down