1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-03 01:08:15 +09:00

fix(SearchService): lint

This commit is contained in:
オスカー、 2024-07-21 22:49:24 +09:00
parent e849be8ea7
commit 2ab5fdcb88
Signed by: SWREI
GPG Key ID: 139D6573F92DA9F7

View File

@ -278,13 +278,13 @@ export class SearchService {
k: 'createdAt', k: 'createdAt',
v: this.idService.parse(pagination.sinceId).date.getTime() v: this.idService.parse(pagination.sinceId).date.getTime()
}); });
if (opts.userId) filter.qs.push({op: '=', k: 'userId', v: opts.userId}); if (opts.userId) filter.qs.push({ op: '=', k: 'userId', v: opts.userId });
if (opts.channelId) filter.qs.push({op: '=', k: 'channelId', v: opts.channelId}); if (opts.channelId) filter.qs.push({ op: '=', k: 'channelId', v: opts.channelId });
if (opts.host) { if (opts.host) {
if (opts.host === '.') { if (opts.host === '.') {
filter.qs.push({op: 'is null', k: 'userHost'}); filter.qs.push({ op: 'is null', k: 'userHost' });
} else { } else {
filter.qs.push({op: '=', k: 'userHost', v: opts.host}); filter.qs.push({ op: '=', k: 'userHost', v: opts.host });
} }
} }
const res = await this.meilisearchNoteIndex!.search(q, { const res = await this.meilisearchNoteIndex!.search(q, {
@ -299,65 +299,65 @@ export class SearchService {
const notes = await this.notesRepository.findBy({ const notes = await this.notesRepository.findBy({
id: In(res.hits.map(x => x.id)), id: In(res.hits.map(x => x.id)),
}); });
const promises = notes.map(async note => ({note: note, result: (await this.filter(me, note))})); const promises = notes.map(async note => ({ note: note, result: (await this.filter(me, note)) }));
const data = await Promise.all(promises); const data = await Promise.all(promises);
const dataFilter = data.filter(d => d.result); const dataFilter = data.filter(d => d.result);
const filteredNotes = dataFilter.map(d => d.note); const filteredNotes = dataFilter.map(d => d.note);
return filteredNotes.sort((a, b) => a.id > b.id ? -1 : 1); return filteredNotes.sort((a, b) => a.id > b.id ? -1 : 1);
} else if (this.elasticsearch) { } else if (this.elasticsearch) {
const esFilter: any = { const esFilter: any = {
bool: {
must: [],
},
};
if (pagination.untilId) esFilter.bool.must.push({ range: { createdAt: { lt: this.idService.parse(pagination.untilId).date.getTime() } } });
if (pagination.sinceId) esFilter.bool.must.push({ range: { createdAt: { gt: this.idService.parse(pagination.sinceId).date.getTime() } } });
if (opts.userId) esFilter.bool.must.push({ term: { userId: opts.userId } });
if (opts.channelId) esFilter.bool.must.push({ term: { channelId: opts.channelId } });
if (opts.host) {
if (opts.host === '.') {
esFilter.bool.must.push({ bool: { must_not: [{ exists: { field: 'userHost' } }] } });
} else {
esFilter.bool.must.push({ term: { userHost: opts.host } });
}
}
if (q !== '') {
esFilter.bool.must.push({
bool: { bool: {
must: [], should: [
{ wildcard: { 'text': { value: q } } },
{ simple_query_string: { fields: ['text'], 'query': q, default_operator: 'and' } },
{ wildcard: { 'cw': { value: q } } },
{ simple_query_string: { fields: ['cw'], 'query': q, default_operator: 'and' } },
],
minimum_should_match: 1,
}, },
};
if (pagination.untilId) esFilter.bool.must.push({ range: { createdAt: { lt: this.idService.parse(pagination.untilId).date.getTime() } } });
if (pagination.sinceId) esFilter.bool.must.push({ range: { createdAt: { gt: this.idService.parse(pagination.sinceId).date.getTime() } } });
if (opts.userId) esFilter.bool.must.push({ term: { userId: opts.userId } });
if (opts.channelId) esFilter.bool.must.push({ term: { channelId: opts.channelId } });
if (opts.host) {
if (opts.host === '.') {
esFilter.bool.must.push({ bool: { must_not: [{ exists: { field: 'userHost' } }] } });
} else {
esFilter.bool.must.push({ term: { userHost: opts.host } });
}
}
if (q !== '') {
esFilter.bool.must.push({
bool: {
should: [
{ wildcard: { 'text': { value: q } } },
{ simple_query_string: { fields: ['text'], 'query': q, default_operator: 'and' } },
{ wildcard: { 'cw': { value: q } } },
{ simple_query_string: { fields: ['cw'], 'query': q, default_operator: 'and' } },
],
minimum_should_match: 1,
},
});
}
const res = await (this.elasticsearch.search)({
index: this.elasticsearchNoteIndex + '*' as string,
body: {
query: esFilter,
sort: [{ createdAt: { order: 'desc' } }],
},
_source: ['id', 'createdAt'],
size: pagination.limit,
}); });
}
const noteIds = res.hits.hits.map((hit: any) => hit._id); const res = await (this.elasticsearch.search)({
if (noteIds.length === 0) return []; index: this.elasticsearchNoteIndex + '*' as string,
const notes = await this.notesRepository.findBy({ body: {
id: In(noteIds), query: esFilter,
}); sort: [{ createdAt: { order: 'desc' } }],
const promises = notes.map(async note => ({ note: note, result: (await this.filter(me, note)) })); },
const data = await Promise.all(promises); _source: ['id', 'createdAt'],
const dataFilter = data.filter(d => d.result); size: pagination.limit,
const filteredNotes = dataFilter.map(d => d.note); });
return filteredNotes.sort((a, b) => a.id > b.id ? -1 : 1);
} else { const noteIds = res.hits.hits.map((hit: any) => hit._id);
if (noteIds.length === 0) return [];
const notes = await this.notesRepository.findBy({
id: In(noteIds),
});
const promises = notes.map(async note => ({ note: note, result: (await this.filter(me, note)) }));
const data = await Promise.all(promises);
const dataFilter = data.filter(d => d.result);
const filteredNotes = dataFilter.map(d => d.note);
return filteredNotes.sort((a, b) => a.id > b.id ? -1 : 1);
} else {
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), pagination.sinceId, pagination.untilId); const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), pagination.sinceId, pagination.untilId);
if (opts.userId) { if (opts.userId) {