From 687bf1f0f1ec3868449dc441652645c91e591d78 Mon Sep 17 00:00:00 2001 From: Dave Bauman Date: Thu, 6 Apr 2023 14:15:24 -0400 Subject: [PATCH] feat: Search authors by username or display name --- .../components/search-bar/search-syntax.tsx | 4 ++- packages/shared/src/search.ts | 32 ++++++++++++++++++- packages/shared/test/search.test.ts | 25 +++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/packages/frontend/src/pages/search-page/components/search-bar/search-syntax.tsx b/packages/frontend/src/pages/search-page/components/search-bar/search-syntax.tsx index 0b77283a4..30ad8ebcb 100644 --- a/packages/frontend/src/pages/search-page/components/search-bar/search-syntax.tsx +++ b/packages/frontend/src/pages/search-page/components/search-bar/search-syntax.tsx @@ -47,7 +47,7 @@ export const SearchSyntax = () => { - + @@ -110,6 +110,8 @@ export const SearchSyntax = () => { author:username / + author:"Full Name" + / @username Insight author diff --git a/packages/shared/src/search.ts b/packages/shared/src/search.ts index 7f00d3df1..e6df0a147 100644 --- a/packages/shared/src/search.ts +++ b/packages/shared/src/search.ts @@ -151,6 +151,36 @@ export class SearchTerm implements SearchClause { return {}; } + if (this.key === 'author') { + // Special case for supporting either username or display name + return { + bool: { + filter: [ + { + bool: { + should: [ + { + term: { + 'contributors.userName.keyword': { + value: this.value + } + } + }, + { + term: { + 'contributors.displayName.keyword': { + value: this.value + } + } + } + ] + } + } + ] + } + }; + } + return { bool: { filter: [ @@ -169,7 +199,7 @@ export class SearchTerm implements SearchClause { toString(): string { switch (this.key) { case 'author': - return `@${this.value}`; + return this.value.includes(' ') ? `author:"${this.value}"` : `@${this.value}`; case 'tag': return `#${this.value}`; default: diff --git a/packages/shared/test/search.test.ts b/packages/shared/test/search.test.ts index ae972423d..5ee86bc0b 100644 --- a/packages/shared/test/search.test.ts +++ b/packages/shared/test/search.test.ts @@ -342,7 +342,14 @@ describe('search', () => { bool: { filter: [ { term: { 'tags.keyword': { value: 'demo' } } }, - { term: { 'contributors.userName.keyword': { value: 'username' } } } + { + bool: { + should: [ + { term: { 'contributors.userName.keyword': { value: 'username' } } }, + { term: { 'contributors.displayName.keyword': { value: 'username' } } } + ] + } + } ], should: [ { @@ -394,7 +401,16 @@ describe('search', () => { const es = parseToElasticsearch('author:username'); expect(es).toMatchObject({ bool: { - filter: [{ term: { 'contributors.userName.keyword': { value: 'username' } } }] + filter: [ + { + bool: { + should: [ + { term: { 'contributors.userName.keyword': { value: 'username' } } }, + { term: { 'contributors.displayName.keyword': { value: 'username' } } } + ] + } + } + ] } }); }); @@ -451,5 +467,10 @@ describe('search', () => { const query = toSearchQuery(clauses); expect(query).toBe('updatedDate:>=2020-03-01 updatedDate:<=2020-10-01'); }); + test('author full name', () => { + const clauses: any[] = parseSearchQuery('author:"John Doe"'); + const query = toSearchQuery(clauses); + expect(query).toBe('author:"John Doe"'); + }); }); });