Skip to content

Commit

Permalink
インデックスプロパティを変えて検索をいろいろ
Browse files Browse the repository at this point in the history
  • Loading branch information
penginn-net committed Jul 12, 2024
1 parent 53ac6ab commit c055c96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
36 changes: 30 additions & 6 deletions packages/backend/src/core/AdvancedSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { CacheService } from '@/core/CacheService.js';
import { QueryService } from '@/core/QueryService.js';
import { IdService } from '@/core/IdService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { isQuote, isRenote } from '@/misc/is-renote.js';
import { DriveService } from './DriveService.js';

type K = string;
type V = string | number | boolean;
Expand Down Expand Up @@ -81,6 +83,7 @@ export class AdvancedSearchService {
private queryService: QueryService,
private idService: IdService,
private loggerService: LoggerService,
private driveService: DriveService,
) {
if (opensearch && config.opensearch && config.opensearch.index) {
const indexname = `${config.opensearch.index}---notes`;
Expand All @@ -91,18 +94,21 @@ export class AdvancedSearchService {
}).then((indexExists) => {
if (!indexExists) [
this.opensearch?.indices.create({
index: indexname + `-${new Date().toISOString().slice(0, 7).split(/-/g).join('')}` + `-${randomUUID()}`,
index: indexname,
body: {
mappings: {
properties: {
text: { type: 'text' },
cw: { type: 'text' },
userId: { type: 'keyword' },
userHost: { type: 'keyword' },
channelId: { type: 'keyword' },
createdAt: { type: 'date' },
tags: { type: 'keyword' },
replyId: { type: 'keyword' },
fileId: { type: 'keyword' },
fileIds: { type: 'keyword' },
isQuote: { type: 'bool' },
sensitivefileCount: { type: 'byte' },
nonSensitivefileCount: { type: 'byte' },
},
},
settings: {
Expand Down Expand Up @@ -169,15 +175,26 @@ export class AdvancedSearchService {
if (!['home', 'public', 'followers'].includes(note.visibility)) return;

if (this.opensearch) {
let sensitiveCount = 0;
let nonSensitiveCount = 0;
if (note.fileIds) {
sensitiveCount = await this.driveService.getSensitiveFileCount(note.fileIds);
nonSensitiveCount = note.fileIds.length - sensitiveCount;
}
const Quote = isRenote(note) && isQuote(note);

const body = {
text: note.text,
cw: note.cw,
userId: note.userId,
userHost: note.userHost,
channelId: note.channelId,
createdAt: this.idService.parse(note.id).date.getTime(),
tags: note.tags,
replyId: note.replyId,
fileIds: note.fileIds,
isQuote: Quote,
SensitivefileCount: sensitiveCount,
nonSensitivefileCount: nonSensitiveCount,
};

await this.opensearch.index({
Expand Down Expand Up @@ -258,13 +275,20 @@ export class AdvancedSearchService {
osFilter.bool.must.push({ term: { userHost: opts.host } });
}
}
if (opts.origin) {
if (opts.origin === 'local') {
osFilter.bool.must.push({ term: { must_not: [{ exists: { field: 'userHost' } }] } });
} else if (opts.origin === 'remote') {
osFilter.bool.must.push({ term: { must: [{ exists: { field: 'userHost' } }] } });
}
}
if (opts.excludeReply) osFilter.bool.must.push({ term: { must_not: [{ exists: { field: 'replyId' } }] } });
if (opts.excludeNsfw) osFilter.bool.must.push({ term: { must_not: [{ exists: { field: 'cw' } }] } });
if (opts.fileOption) {
if (opts.fileOption === 'file-only') {
osFilter.bool.must.push({ term: { must: [{ exists: { field: 'fileId' } }] } });
osFilter.bool.must.push({ term: { must: [{ exists: { field: 'fileIds' } }] } });
} else if (opts.fileOption === 'no-file') {
osFilter.bool.must.push({ term: { must_not: [{ exists: { field: 'fileId' } }] } });
osFilter.bool.must.push({ term: { must_not: [{ exists: { field: 'fileIds' } }] } });
}
}

Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/core/DriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,4 +933,16 @@ export class DriveService {
cleanup();
}
}

@bindThis
public async getSensitiveFileCount(FileIds: string[]): Promise<number> {
let SensitiveCount = 0;

for (const FileId of FileIds) {
const file = await this.driveFilesRepository.findOneBy({ id: FileId });
if (file?.isSensitive) SensitiveCount++;
}

return SensitiveCount;
}
}

0 comments on commit c055c96

Please sign in to comment.