-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
5,189 additions
and
2,021 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
testMatch: ['**/*.spec.js'], | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const Snippet = require('../model/snippet'); | ||
|
||
const searchUtils = require('./search.utils'); | ||
|
||
let findItems = async function (type, isPublic, userId, query, page, limit, searchInclude) { | ||
//split in text and tags | ||
const searchedTermsAndTags = searchUtils.splitSearchQuery(query); | ||
let searchedTerms = searchedTermsAndTags.terms; | ||
const searchedTags = searchedTermsAndTags.tags; | ||
let snippets = []; | ||
|
||
const {specialSearchFilters, fulltextSearchTerms} = searchUtils.extractFulltextAndSpecialSearchTerms(searchedTerms); | ||
|
||
if ( searchedTerms.length > 0 && searchedTags.length > 0 ) { | ||
snippets = await getItemsForTagsAndTerms(type, isPublic, userId, searchedTags, fulltextSearchTerms, page, limit, specialSearchFilters, searchInclude); | ||
} else if ( searchedTerms.length > 0 ) { | ||
snippets = await getItemsForSearchedTerms(type, isPublic, userId, fulltextSearchTerms, page, limit, specialSearchFilters, searchInclude); | ||
} else { | ||
snippets = await getItemsForSearchedTags(type, isPublic, userId, searchedTags, page, limit, specialSearchFilters); | ||
} | ||
|
||
return snippets; | ||
} | ||
|
||
let getItemsForTagsAndTerms = async function (type, isPublic, userId, searchedTags, fulltextSearchTerms, page, limit, specialSearchFilters, searchInclude) { | ||
let filter = { | ||
tags: | ||
{ | ||
$all: searchedTags | ||
} | ||
} | ||
if ( userId ){ | ||
filter['userId'] = userId; | ||
} | ||
if (isPublic) { | ||
filter['public'] = true; | ||
} | ||
|
||
filter = searchUtils.includeFulltextSearchTermsInFilter(fulltextSearchTerms, filter); | ||
|
||
addSpecialSearchFiltersToMongoFilter(specialSearchFilters, filter); | ||
|
||
let snippets = await Snippet.find( | ||
filter, | ||
{ | ||
score: {$meta: "textScore"} | ||
} | ||
) | ||
.sort({score: {$meta: "textScore"}}) | ||
.skip((page - 1) * limit) | ||
.limit(limit) | ||
.lean() | ||
.exec(); | ||
|
||
return snippets; | ||
} | ||
|
||
|
||
let getItemsForSearchedTerms = async function (type, isPublic, userId, fulltextSearchTerms, page, limit, specialSearchFilters, searchInclude) { | ||
|
||
let filter = {}; | ||
if ( userId ){ | ||
filter['userId'] = userId; | ||
} else { | ||
filter['public'] = true; | ||
} | ||
|
||
if ( fulltextSearchTerms.length > 0 ) { | ||
if ( searchInclude === 'any' ) { | ||
filter.$text = {$search: fulltextSearchTerms.join(' ')} | ||
} else { | ||
filter.$text = {$search: searchUtils.generateFullSearchText(fulltextSearchTerms)}; | ||
} | ||
} | ||
|
||
addSpecialSearchFiltersToMongoFilter(specialSearchFilters, filter); | ||
|
||
let snippets = await Snippet.find( | ||
filter, | ||
{ | ||
score: {$meta: "textScore"} | ||
} | ||
) | ||
.sort({score: {$meta: "textScore"}}) | ||
.skip((page - 1) * limit) | ||
.limit(limit) | ||
.lean() | ||
.exec(); | ||
|
||
return snippets; | ||
} | ||
|
||
|
||
let getItemsForSearchedTags = async function (type, isPublic, userId, searchedTags, page, limit, specialSearchFilters) { | ||
let filter = { | ||
tags: | ||
{ | ||
$all: searchedTags | ||
} | ||
} | ||
if ( userId ) { | ||
filter['userId'] = userId; | ||
} else { | ||
filter['public'] = true; | ||
} | ||
|
||
addSpecialSearchFiltersToMongoFilter(specialSearchFilters, filter); | ||
|
||
let snippets = await Snippet.find(filter) | ||
.sort({createdAt: -1}) | ||
.skip((page - 1) * limit) | ||
.limit(limit) | ||
.lean() | ||
.exec(); | ||
|
||
return snippets; | ||
} | ||
|
||
let addSpecialSearchFiltersToMongoFilter = function (specialSearchFilters, filter) { | ||
if ( specialSearchFilters.userId ) { | ||
filter.userId = specialSearchFilters.userId; | ||
} else if ( specialSearchFilters.privateOnly ) { | ||
filter.public = false; | ||
} | ||
|
||
if ( specialSearchFilters.site ) { | ||
filter.sourceUrl = new RegExp(specialSearchFilters.site, 'i'); //TODO when performance becomes an issue extract domains from URLs and make a direct comparison with the domain | ||
} | ||
}; | ||
|
||
|
||
module.exports = { | ||
findSnippets: findItems | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const searchUtils = require('./search.utils'); | ||
|
||
describe('splitSearchQuery', () => { | ||
it('should split search query into terms and tags', () => { | ||
const query = 'term1 [tag1] term2 [tag2]'; | ||
const expectedResult = { | ||
terms: ['term1', 'term2'], | ||
tags: ['tag1', 'tag2'] | ||
}; | ||
expect(searchUtils.splitSearchQuery(query)).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('extractFulltextAndSpecialSearchTerms', () => { | ||
it('should extract special search terms and filters from searched terms', () => { | ||
const searchedTerms = [ 'lang:en', 'site:github.com', 'private:only', 'term1', 'user:12345678-abcd-1234-abcd-123456789abc' ]; | ||
const expectedResult = { | ||
"fulltextSearchTerms": [ | ||
"term1" | ||
], | ||
"specialSearchFilters": { | ||
"lang": "en", | ||
"privateOnly": true, | ||
"site": "github.com", | ||
"userId": "12345678-abcd-1234-abcd-123456789abc" | ||
} | ||
} | ||
expect(searchUtils.extractFulltextAndSpecialSearchTerms(searchedTerms)).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('includeFulltextSearchTermsInFilter', () => { | ||
test('returns filter with $text when fulltextSearchTerms is not empty', () => { | ||
const fulltextSearchTerms = ['test']; | ||
const filter = {}; | ||
const searchInclude = 'any'; | ||
const expected = { | ||
...filter, | ||
$text: {$search: fulltextSearchTerms.join(' ')} | ||
}; | ||
expect(searchUtils.includeFulltextSearchTermsInFilter(fulltextSearchTerms, filter, searchInclude)).toEqual(expected); | ||
}); | ||
|
||
test('returns filter without $text when fulltextSearchTerms is empty', () => { | ||
const fulltextSearchTerms = []; | ||
const filter = {}; | ||
const searchInclude = 'any'; | ||
expect(searchUtils.includeFulltextSearchTermsInFilter(fulltextSearchTerms, filter, searchInclude)).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('generateFullSearchText', () => { | ||
it('should generate the correct full search text for given fulltext search terms', () => { | ||
const fulltextSearchTerms = ['apple', '-banana', 'cherry']; | ||
const expectedResult = '"apple" -banana "cherry"'; | ||
|
||
expect(searchUtils.generateFullSearchText(fulltextSearchTerms)).toBe(expectedResult); | ||
}); | ||
}); | ||
|
Oops, something went wrong.