2021-08-26 10:13:30 +03:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2021-08-27 12:17:18 +03:00
|
|
|
import type { FullTextAdapter, IndexedDoc, SearchQuery } from '@anticrm/server-core'
|
2021-08-26 10:13:30 +03:00
|
|
|
|
|
|
|
import { Client } from '@elastic/elasticsearch'
|
|
|
|
|
2021-08-27 12:17:18 +03:00
|
|
|
class ElasticAdapter implements FullTextAdapter {
|
2021-08-26 10:13:30 +03:00
|
|
|
constructor (
|
|
|
|
private readonly client: Client,
|
2021-08-27 12:17:18 +03:00
|
|
|
private readonly db: string
|
2021-08-26 10:13:30 +03:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2021-08-27 12:17:18 +03:00
|
|
|
async search (
|
|
|
|
query: SearchQuery
|
|
|
|
): Promise<IndexedDoc[]> {
|
2021-08-26 19:58:45 +03:00
|
|
|
const result = await this.client.search({
|
2021-08-27 12:17:18 +03:00
|
|
|
index: this.db,
|
2021-08-26 10:13:30 +03:00
|
|
|
type: '_doc',
|
2021-08-26 19:58:45 +03:00
|
|
|
body: {
|
|
|
|
}
|
2021-08-26 10:13:30 +03:00
|
|
|
})
|
2021-08-27 14:11:02 +03:00
|
|
|
const hits = result.body.hits.hits as any[]
|
|
|
|
return hits.map(hit => hit._source)
|
2021-08-26 10:13:30 +03:00
|
|
|
}
|
|
|
|
|
2021-08-27 12:17:18 +03:00
|
|
|
async index (doc: IndexedDoc): Promise<void> {
|
2021-08-26 10:13:30 +03:00
|
|
|
await this.client.index({
|
2021-08-27 12:17:18 +03:00
|
|
|
index: this.db,
|
2021-08-26 10:13:30 +03:00
|
|
|
type: '_doc',
|
2021-08-27 12:17:18 +03:00
|
|
|
body: doc
|
2021-08-26 10:13:30 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-27 12:17:18 +03:00
|
|
|
export async function createElasticAdapter (url: string, dbName: string): Promise<FullTextAdapter> {
|
2021-08-26 10:13:30 +03:00
|
|
|
const client = new Client({
|
|
|
|
node: url
|
|
|
|
})
|
|
|
|
|
2021-08-27 12:17:18 +03:00
|
|
|
return new ElasticAdapter(client, dbName)
|
2021-08-26 10:13:30 +03:00
|
|
|
}
|