Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Hotfix snippets search #3434

Merged
merged 5 commits into from
Jan 15, 2021
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
2 changes: 1 addition & 1 deletion app/api/search/deprecatedRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default app => {
),
(req, res, next) =>
search
.searchSnippets(req.query.searchTerm, req.query.id, req.language)
.searchSnippets(req.query.searchTerm, req.query.id, req.language, req.user)
.then(results => res.json(results))
.catch(next)
);
Expand Down
12 changes: 7 additions & 5 deletions app/api/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ const instanceSearch = elasticIndex => ({
return entities.get({ user: user._id, language, published: false });
},

async searchSnippets(searchTerm, sharedId, language) {
async searchSnippets(searchTerm, sharedId, language, user) {
const templates = await templatesModel.get();

const searchTextType = searchTerm
Expand All @@ -663,14 +663,16 @@ const instanceSearch = elasticIndex => ({
.concat(['title', 'fullText']);
const query = documentQueryBuilder()
.fullTextSearch(searchTerm, searchFields, 9999, searchTextType)
.includeUnpublished()
.filterById(sharedId)
.language(language)
.query();
.language(language);

if (user) {
query.includeUnpublished();
}

const response = await elastic.search({
index: elasticIndex || getCurrentTenantIndex(),
body: query,
body: query.query(),
});
if (response.body.hits.hits.length === 0) {
return {
Expand Down
1 change: 1 addition & 0 deletions app/api/search/specs/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('index (search)', () => {
},
],
language: 'en',
published: true,
};

await search.bulkIndex([entity], 'index', elasticIndex);
Expand Down
8 changes: 6 additions & 2 deletions app/api/search/specs/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,17 @@ describe('search routes', () => {

it('should search', done => {
spyOn(search, 'searchSnippets').and.returnValue(new Promise(resolve => resolve('results')));
const req = { query: { searchTerm: 'test', id: 'id' }, language: 'es' };
const req = {
query: { searchTerm: 'test', id: 'id' },
language: 'es',
user: { _id: 'userId' },
};

routes
.get('/api/search_snippets', req)
.then(response => {
expect(response).toEqual('results');
expect(search.searchSnippets).toHaveBeenCalledWith('test', 'id', 'es');
expect(search.searchSnippets).toHaveBeenCalledWith('test', 'id', 'es', req.user);
done();
})
.catch(catchErrors(done));
Expand Down
60 changes: 34 additions & 26 deletions app/api/search/specs/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,42 @@ describe('search', () => {
expect(snippets.fullText[0].text).not.toMatch('[[34]]');
});

it('perform a search on metadata and fullText and return the snippets', done => {
search
.searchSnippets('gargoyles', ids.metadataSnippets, 'en')
.then(snippets => {
const titleSnippet = snippets.metadata.find(snippet => snippet.field === 'title');
const fieldSnippet = snippets.metadata.find(
snippet => snippet.field === 'metadata.field1.value'
);
expect(snippets.count).toBe(3);
expect(snippets.metadata.length).toEqual(2);
expect(titleSnippet.texts.length).toBe(1);
expect(titleSnippet.texts[0]).toMatch('gargoyles');
expect(fieldSnippet.texts.length).toBe(1);
expect(fieldSnippet.texts[0]).toMatch('gargoyles');
expect(snippets.fullText.length).toBe(1);
done();
})
.catch(catchErrors(done));
it('perform a search on metadata and fullText and return the snippets', async () => {
const user = { _id: 'userId' };
const snippets = await search.searchSnippets('gargoyles', ids.metadataSnippets, 'en', user);

const titleSnippet = snippets.metadata.find(snippet => snippet.field === 'title');
const fieldSnippet = snippets.metadata.find(
snippet => snippet.field === 'metadata.field1.value'
);
expect(snippets.count).toBe(3);
expect(snippets.metadata.length).toEqual(2);
expect(titleSnippet.texts.length).toBe(1);
expect(titleSnippet.texts[0]).toMatch('gargoyles');
expect(fieldSnippet.texts.length).toBe(1);
expect(fieldSnippet.texts[0]).toMatch('gargoyles');
expect(snippets.fullText.length).toBe(1);
});

it('should perform the search on unpublished documents also', done => {
search
.searchSnippets('unpublished', 'unpublishedSharedId', 'en')
.then(snippets => {
expect(snippets.fullText.length).toBe(1);
done();
})
.catch(catchErrors(done));
it('should include unpublished documents if logged in', async () => {
const user = { _id: 'userId' };
const snippets = await search.searchSnippets(
'unpublished',
'unpublishedSharedId',
'en',
user
);
expect(snippets.fullText.length).toBe(1);
});

it('should not include unpublished if not logged in', async () => {
const snippets = await search.searchSnippets(
'unpublished',
'unpublishedSharedId',
'en',
undefined
);
expect(snippets.fullText.length).toBe(0);
});

describe('when document is not matched', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.19.3",
"version": "1.19.4",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down