Skip to content

Commit

Permalink
feat(next/api): phrase query
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd committed Jan 5, 2024
1 parent d63b2d1 commit e4d4c23
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions next/api/src/service/search-ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { Ticket } from '@/model/Ticket';
import { TicketFieldValue } from '@/model/TicketFieldValue';
import { createQueue } from '@/queue';

interface Term {
value: string;
isPhrase?: true;
}

export class SearchTicketService {
private esClient?: Client;
private indexName: string;
Expand Down Expand Up @@ -285,9 +290,16 @@ export class SearchTicketService {
);
}
if (filters.keyword) {
boolQuery.filter(
esb.multiMatchQuery(['title', 'content', 'fields.value'], filters.keyword).operator('and')
);
const terms = this.parseTerms(filters.keyword);
terms.forEach((term) => {
const matchQuery = esb
.multiMatchQuery(['title', 'content', 'fields.value'], term.value)
.operator('and');
if (term.isPhrase) {
matchQuery.type('phrase');
}
boolQuery.filter(matchQuery);
});
}

const body = esb
Expand All @@ -308,6 +320,24 @@ export class SearchTicketService {
const totalCount = res.body.hits.total.value as number;
return { ids, totalCount };
}

parseTerms(input: string) {
const regex = /"((?:\\.|[^"\\])*)"|(\S+)/g;
const terms: Term[] = [];

for (const [, phrase, term] of input.matchAll(regex)) {
if (phrase) {
const value = phrase.trim().replace(/\\(.)/g, '$1');
if (value) {
terms.push({ value, isPhrase: true });
}
} else if (term) {
terms.push({ value: term });
}
}

return terms;
}
}

export const searchTicketService = new SearchTicketService();

0 comments on commit e4d4c23

Please sign in to comment.