2018-07-04 13:21:30 +09:00
|
|
|
import $ from 'cafy';
|
|
|
|
import es from '../../../../db/elasticsearch';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../define';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { ApiError } from '../../error';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Notes } from '../../../../models';
|
|
|
|
import { In } from 'typeorm';
|
2019-04-25 07:46:39 +09:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
2019-08-09 13:04:35 +09:00
|
|
|
import config from '../../../../config';
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '投稿を検索します。',
|
|
|
|
'en-US': 'Search notes.'
|
|
|
|
},
|
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
query: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
limit: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-02 12:49:08 +09:00
|
|
|
default: 10
|
|
|
|
},
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
offset: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.min(0),
|
2018-11-02 12:49:08 +09:00
|
|
|
default: 0
|
2019-04-25 07:46:39 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
host: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
default: undefined
|
|
|
|
},
|
|
|
|
|
|
|
|
userId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
default: null
|
|
|
|
},
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
res: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-23 11:20:58 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Note',
|
|
|
|
}
|
2019-02-23 11:20:58 +09:00
|
|
|
},
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
searchingNotAvailable: {
|
|
|
|
message: 'Searching not available.',
|
|
|
|
code: 'SEARCHING_NOT_AVAILABLE',
|
|
|
|
id: '7ee9c119-16a1-479f-a6fd-6fab00ed946f'
|
|
|
|
}
|
2018-11-02 12:49:08 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, me) => {
|
|
|
|
if (es == null) throw new ApiError(meta.errors.searchingNotAvailable);
|
2018-07-19 08:24:03 +09:00
|
|
|
|
2019-04-25 07:46:39 +09:00
|
|
|
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 ? [{
|
|
|
|
term: {
|
|
|
|
userHost: ps.host
|
|
|
|
}
|
|
|
|
}] : []
|
|
|
|
: [];
|
|
|
|
|
|
|
|
const result = await es.search({
|
2019-08-09 13:04:35 +09:00
|
|
|
index: config.elasticsearch.index || 'misskey_note',
|
2018-07-04 13:21:30 +09:00
|
|
|
body: {
|
2019-04-13 01:43:22 +09:00
|
|
|
size: ps.limit!,
|
2018-11-02 12:49:08 +09:00
|
|
|
from: ps.offset,
|
2018-07-04 13:21:30 +09:00
|
|
|
query: {
|
2019-04-25 07:46:39 +09:00
|
|
|
bool: {
|
|
|
|
must: [{
|
|
|
|
simple_query_string: {
|
|
|
|
fields: ['text'],
|
|
|
|
query: ps.query.toLowerCase(),
|
|
|
|
default_operator: 'and'
|
|
|
|
},
|
|
|
|
}, ...hostQuery, ...userQuery]
|
2018-07-04 13:21:30 +09:00
|
|
|
}
|
|
|
|
},
|
2019-04-25 07:46:39 +09:00
|
|
|
sort: [{
|
|
|
|
_doc: 'desc'
|
|
|
|
}]
|
2018-07-04 13:21:30 +09:00
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2019-04-25 07:46:39 +09:00
|
|
|
const hits = result.body.hits.hits.map((hit: any) => hit._id);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
if (hits.length === 0) return [];
|
2018-07-04 13:21:30 +09:00
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
// Fetch found notes
|
2019-04-07 21:50:36 +09:00
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
id: In(hits)
|
|
|
|
},
|
|
|
|
order: {
|
|
|
|
id: -1
|
2019-02-22 11:46:58 +09:00
|
|
|
}
|
2018-07-04 13:21:30 +09:00
|
|
|
});
|
2019-02-22 11:46:58 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return await Notes.packMany(notes, me);
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|