From 0db54386cdef3f444f1afb4f3b8bfcaeab7ac68d Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 25 Apr 2019 07:46:39 +0900 Subject: Resolve #3119 --- src/server/api/endpoints/notes/search.ts | 64 ++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'src/server/api/endpoints') diff --git a/src/server/api/endpoints/notes/search.ts b/src/server/api/endpoints/notes/search.ts index daf992b639..65ce20074a 100644 --- a/src/server/api/endpoints/notes/search.ts +++ b/src/server/api/endpoints/notes/search.ts @@ -5,6 +5,7 @@ import { ApiError } from '../../error'; import { Notes } from '../../../../models'; import { In } from 'typeorm'; import { types, bool } from '../../../../misc/schema'; +import { ID } from '../../../../misc/cafy-id'; export const meta = { desc: { @@ -29,7 +30,17 @@ export const meta = { offset: { validator: $.optional.num.min(0), default: 0 - } + }, + + host: { + validator: $.optional.nullable.str, + default: undefined + }, + + userId: { + validator: $.optional.nullable.type(ID), + default: null + }, }, res: { @@ -54,30 +65,51 @@ export const meta = { export default define(meta, async (ps, me) => { if (es == null) throw new ApiError(meta.errors.searchingNotAvailable); - const response = await es.search({ - index: 'misskey', - type: 'note', + 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({ + index: 'misskey_note', body: { size: ps.limit!, from: ps.offset, query: { - simple_query_string: { - fields: ['text'], - query: ps.query, - default_operator: 'and' + bool: { + must: [{ + simple_query_string: { + fields: ['text'], + query: ps.query.toLowerCase(), + default_operator: 'and' + }, + }, ...hostQuery, ...userQuery] } }, - sort: [ - { _doc: 'desc' } - ] + sort: [{ + _doc: 'desc' + }] } }); - if (response.hits.total === 0) { - return []; - } - - const hits = response.hits.hits.map((hit: any) => hit.id); + const hits = result.body.hits.hits.map((hit: any) => hit._id); if (hits.length === 0) return []; -- cgit v1.2.3-freya