diff --git a/server/core/src/fulltext.ts b/server/core/src/fulltext.ts index 54c26c25ba..026c987993 100644 --- a/server/core/src/fulltext.ts +++ b/server/core/src/fulltext.ts @@ -117,16 +117,15 @@ export class FullTextIndex implements WithFind { console.log('search', query) const { _id, $search, ...mainQuery } = query if ($search === undefined) return [] - const docs = await this.adapter.search($search) + const docs = await this.adapter.search(_class, query, options?.limit) console.log(docs) - const ids: Ref[] = [] + const ids: Set> = new Set>(docs.map(p => p.id)) for (const doc of docs) { - ids.push(doc.id) if (doc.attachedTo !== undefined) { - ids.push(doc.attachedTo) + ids.add(doc.attachedTo) } } - return await this.dbStorage.findAll(ctx, _class, { _id: { $in: ids as any }, ...mainQuery }, options) // TODO: remove `as any` + return await this.dbStorage.findAll(ctx, _class, { _id: { $in: Array.from(ids) as any }, ...mainQuery }, options) // TODO: remove `as any` } private getFullTextAttributes (clazz: Ref>): AnyAttribute[] | undefined { @@ -203,6 +202,10 @@ export class FullTextIndex implements WithFind { } i++ } + if (tx.operations.space !== undefined) { + update.space = tx.operations.space + shouldUpdate = true + } if (shouldUpdate) { result = await this.adapter.update(tx.objectId, update) await this.updateAttachedDocs(ctx, tx, update) @@ -229,7 +232,9 @@ export class FullTextIndex implements WithFind { const docUpdate: any = {} for (const key in update) { const index = Number.parseInt(key.replace('content', '')) - docUpdate[`content${index + shift}`] = update[key] + if (!isNaN(index)) { + docUpdate[`content${index + shift}`] = update[key] + } } for (const attached of allAttached) { await this.adapter.update(attached._id, docUpdate) diff --git a/server/core/src/types.ts b/server/core/src/types.ts index 74a6221782..70d224c074 100644 --- a/server/core/src/types.ts +++ b/server/core/src/types.ts @@ -64,7 +64,7 @@ export interface IndexedDoc { export interface FullTextAdapter { index: (doc: IndexedDoc) => Promise update: (id: Ref, update: Record) => Promise - search: (search: string) => Promise + search: (_class: Ref>, search: DocumentQuery, size: number | undefined) => Promise } /** diff --git a/server/elastic/src/__tests__/adapter.test.ts b/server/elastic/src/__tests__/adapter.test.ts index eb31f9520d..03812b6204 100644 --- a/server/elastic/src/__tests__/adapter.test.ts +++ b/server/elastic/src/__tests__/adapter.test.ts @@ -14,7 +14,7 @@ // limitations under the License. // -import type { Ref, Doc, Class, Obj, Account, Space } from '@anticrm/core' +import type { Ref, Doc, Class, Account, Space } from '@anticrm/core' import { createElasticAdapter } from '../adapter' import type { IndexedDoc } from '@anticrm/server-core' @@ -23,14 +23,14 @@ describe('client', () => { const adapter = await createElasticAdapter('http://localhost:9200/', 'ws1') const doc: IndexedDoc = { id: 'doc1' as Ref, - _class: 'class1' as Ref>, + _class: 'class1' as Ref>, modifiedBy: 'andrey' as Ref, modifiedOn: 0, space: 'space1' as Ref, content0: 'hey there!' } await adapter.index(doc) - const hits = await adapter.search('') + const hits = await adapter.search('class1' as Ref>, {}, 1) console.log(hits) }) diff --git a/server/elastic/src/adapter.ts b/server/elastic/src/adapter.ts index 88055ac9be..9b31ee515d 100644 --- a/server/elastic/src/adapter.ts +++ b/server/elastic/src/adapter.ts @@ -14,33 +14,28 @@ // limitations under the License. // -import type { Doc, Ref, TxResult } from '@anticrm/core' +import type { Class, Doc, DocumentQuery, Ref, TxResult } from '@anticrm/core' import type { FullTextAdapter, IndexedDoc } from '@anticrm/server-core' import { Client } from '@elastic/elasticsearch' class ElasticAdapter implements FullTextAdapter { - constructor ( - private readonly client: Client, - private readonly db: string - ) { - } + constructor (private readonly client: Client, private readonly db: string) {} async close (): Promise { await this.client.close() } - async search ( - search: string - ): Promise { - const query = search.replace(/[\\/+\-=&>>, query: DocumentQuery, size: number | undefined): Promise { + if (query.$search === undefined) return [] + const search = query.$search.replace(/[\\/+\-=&> hit._source) + return hits.map((hit) => hit._source) } catch (err) { console.error(JSON.stringify(err, null, 2)) return [] @@ -115,7 +141,10 @@ class ElasticAdapter implements FullTextAdapter { /** * @public */ -export async function createElasticAdapter (url: string, dbName: string): Promise Promise}> { +export async function createElasticAdapter ( + url: string, + dbName: string +): Promise Promise }> { const client = new Client({ node: url })