Skip to content

Commit

Permalink
feat: Search authors by username or display name
Browse files Browse the repository at this point in the history
  • Loading branch information
baumandm committed Apr 6, 2023
1 parent 8438b81 commit 687bf1f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const SearchSyntax = () => {
</Tooltip>
</Box>
</PopoverTrigger>
<PopoverContent zIndex={9000} {...useBreakpointValue({ base: {}, lg: { width: 'auto', maxWidth: '3xl' } })}>
<PopoverContent zIndex={9000} {...useBreakpointValue({ base: {}, lg: { width: 'auto', maxWidth: '4xl' } })}>
<PopoverArrow />
<PopoverCloseButton />
<PopoverHeader>
Expand Down Expand Up @@ -110,6 +110,8 @@ export const SearchSyntax = () => {
<HStack>
<Code>author:username</Code>
<Text>/</Text>
<Code whiteSpace="nowrap">author:"Full Name"</Code>
<Text>/</Text>
<Code>@username</Code>
</HStack>
<Text>Insight author</Text>
Expand Down
32 changes: 31 additions & 1 deletion packages/shared/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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:
Expand Down
25 changes: 23 additions & 2 deletions packages/shared/test/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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' } } }
]
}
}
]
}
});
});
Expand Down Expand Up @@ -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"');
});
});
});

0 comments on commit 687bf1f

Please sign in to comment.