2018-07-03 22:21:30 -06:00
|
|
|
import $ from 'cafy';
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import Note from '../../../../models/note';
|
2018-10-03 09:39:11 -06:00
|
|
|
import { packMany } from '../../../../models/note';
|
2018-07-03 22:21:30 -06:00
|
|
|
import es from '../../../../db/elasticsearch';
|
2018-11-01 22:47:44 -06:00
|
|
|
import define from '../../define';
|
2019-02-21 19:46:58 -07:00
|
|
|
import { ApiError } from '../../error';
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2018-11-01 21:49:08 -06:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '投稿を検索します。',
|
|
|
|
'en-US': 'Search notes.'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: false,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
query: {
|
|
|
|
validator: $.str
|
|
|
|
},
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2018-11-01 21:49:08 -06:00
|
|
|
limit: {
|
2019-02-13 00:33:07 -07:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-01 21:49:08 -06:00
|
|
|
default: 10
|
|
|
|
},
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2018-11-01 21:49:08 -06:00
|
|
|
offset: {
|
2019-02-13 00:33:07 -07:00
|
|
|
validator: $.optional.num.min(0),
|
2018-11-01 21:49:08 -06:00
|
|
|
default: 0
|
|
|
|
}
|
2019-02-21 19:46:58 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
searchingNotAvailable: {
|
|
|
|
message: 'Searching not available.',
|
|
|
|
code: 'SEARCHING_NOT_AVAILABLE',
|
|
|
|
id: '7ee9c119-16a1-479f-a6fd-6fab00ed946f'
|
|
|
|
}
|
2018-11-01 21:49:08 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-21 19:46:58 -07:00
|
|
|
export default define(meta, async (ps, me) => {
|
|
|
|
if (es == null) throw new ApiError(meta.errors.searchingNotAvailable);
|
2018-07-18 17:24:03 -06:00
|
|
|
|
2019-02-21 19:46:58 -07:00
|
|
|
const response = await es.search({
|
2018-07-03 22:21:30 -06:00
|
|
|
index: 'misskey',
|
|
|
|
type: 'note',
|
|
|
|
body: {
|
2018-11-01 21:49:08 -06:00
|
|
|
size: ps.limit,
|
|
|
|
from: ps.offset,
|
2018-07-03 22:21:30 -06:00
|
|
|
query: {
|
|
|
|
simple_query_string: {
|
|
|
|
fields: ['text'],
|
2018-11-01 21:49:08 -06:00
|
|
|
query: ps.query,
|
2018-07-03 22:21:30 -06:00
|
|
|
default_operator: 'and'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sort: [
|
|
|
|
{ _doc: 'desc' }
|
|
|
|
]
|
|
|
|
}
|
2019-02-21 19:46:58 -07:00
|
|
|
});
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2019-02-21 19:46:58 -07:00
|
|
|
if (response.hits.total === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2019-02-21 19:46:58 -07:00
|
|
|
const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id));
|
2018-07-03 22:21:30 -06:00
|
|
|
|
2019-02-21 19:46:58 -07:00
|
|
|
// Fetch found notes
|
|
|
|
const notes = await Note.find({
|
|
|
|
_id: {
|
|
|
|
$in: hits
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
2018-07-03 22:21:30 -06:00
|
|
|
});
|
2019-02-21 19:46:58 -07:00
|
|
|
|
|
|
|
return await packMany(notes, me);
|
|
|
|
});
|