Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ElasticSearchなしで検索可能 Resolves #12 #794

Merged
merged 1 commit into from
Sep 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 74 additions & 63 deletions src/server/api/endpoints/notes/search.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import $ from 'cafy';
import es from '../../../../db/elasticsearch';
import define from '../../define';
import { ApiError } from '../../error';
import { Notes } from '../../../../models';
import { In } from 'typeorm';
import { ID } from '../../../../misc/cafy-id';
import config from '../../../../config';
import { makePaginationQuery } from '../../common/make-pagination-query';
import { generateVisibilityQuery } from '../../common/generate-visibility-query';
import { generateMuteQuery } from '../../common/generate-mute-query';

export const meta = {
desc: {
Expand All @@ -22,16 +24,19 @@ export const meta = {
validator: $.str
},

sinceId: {
validator: $.optional.type(ID),
},

untilId: {
validator: $.optional.type(ID),
},

limit: {
validator: $.optional.num.range(1, 100),
default: 10
},

offset: {
validator: $.optional.num.min(0),
default: 0
},

host: {
validator: $.optional.nullable.str,
default: undefined
Expand All @@ -54,74 +59,80 @@ export const meta = {
},

errors: {
searchingNotAvailable: {
message: 'Searching not available.',
code: 'SEARCHING_NOT_AVAILABLE',
id: '7ee9c119-16a1-479f-a6fd-6fab00ed946f'
}
}
};

export default define(meta, async (ps, me) => {
if (es == null) throw new ApiError(meta.errors.searchingNotAvailable);
if (es == null) {
const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId)
.andWhere('note.text ILIKE :q', { q: `%${ps.query}%` })
.leftJoinAndSelect('note.user', 'user');

const userQuery = ps.userId != null ? [{
term: {
userId: ps.userId
}
}] : [];

const hostQuery = ps.userId == null ?
ps.host === null ? [{
bool: {
must_not: {
exists: {
field: 'userHost'
}
}
}
}] : ps.host !== undefined ? [{
generateVisibilityQuery(query, me);
if (me) generateMuteQuery(query, me);

const notes = await query.take(ps.limit!).getMany();

return await Notes.packMany(notes, me);
} else {
const userQuery = ps.userId != null ? [{
term: {
userHost: ps.host
userId: ps.userId
}
}] : []
: [];

const result = await es.search({
index: config.elasticsearch.index || 'misskey_note',
body: {
size: ps.limit!,
from: ps.offset,
query: {
}] : [];

const hostQuery = ps.userId == null ?
ps.host === null ? [{
bool: {
must: [{
simple_query_string: {
fields: ['text'],
query: ps.query.toLowerCase(),
default_operator: 'and'
},
}, ...hostQuery, ...userQuery]
must_not: {
exists: {
field: 'userHost'
}
}
}
},
sort: [{
_doc: 'desc'
}]
}
});
}] : ps.host !== undefined ? [{
term: {
userHost: ps.host
}
}] : []
: [];

const result = await es.search({
index: config.elasticsearch.index || 'misskey_note',
body: {
size: ps.limit!,
from: ps.offset,
query: {
bool: {
must: [{
simple_query_string: {
fields: ['text'],
query: ps.query.toLowerCase(),
default_operator: 'and'
},
}, ...hostQuery, ...userQuery]
}
},
sort: [{
_doc: 'desc'
}]
}
});

const hits = result.body.hits.hits.map((hit: any) => hit._id);
const hits = result.body.hits.hits.map((hit: any) => hit._id);

if (hits.length === 0) return [];
if (hits.length === 0) return [];

// Fetch found notes
const notes = await Notes.find({
where: {
id: In(hits)
},
order: {
id: -1
}
});
// Fetch found notes
const notes = await Notes.find({
where: {
id: In(hits)
},
order: {
id: -1
}
});

return await Notes.packMany(notes, me);
return await Notes.packMany(notes, me);
}
});