mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-12 19:30:52 +00:00
Elastic search query escaping (#706)
Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
parent
d5c2c07b9e
commit
40500e6eda
@ -116,6 +116,7 @@ export class FullTextIndex implements WithFind {
|
||||
): Promise<FindResult<T>> {
|
||||
console.log('search', query)
|
||||
const { _id, $search, ...mainQuery } = query
|
||||
if ($search === undefined) return []
|
||||
const docs = await this.adapter.search($search)
|
||||
console.log(docs)
|
||||
const ids: Ref<Doc>[] = []
|
||||
|
@ -164,7 +164,7 @@ class TServerStorage implements ServerStorage {
|
||||
): Promise<FindResult<T>> {
|
||||
return await ctx.with('find-all', {}, (ctx) => {
|
||||
const domain = this.hierarchy.getDomain(clazz)
|
||||
if (Object.keys(query)[0] === '$search') {
|
||||
if (query.$search !== undefined && query.$search.length > 0) {
|
||||
return ctx.with('full-text-find-all', {}, (ctx) => this.fulltext.findAll(ctx, clazz, query, options))
|
||||
}
|
||||
return ctx.with('db-find-all', { _class: clazz, domain }, () =>
|
||||
|
@ -58,18 +58,13 @@ export interface IndexedDoc {
|
||||
data?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export type SearchQuery = any // TODO: replace with DocumentQuery
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface FullTextAdapter {
|
||||
index: (doc: IndexedDoc) => Promise<TxResult>
|
||||
update: (id: Ref<Doc>, update: Record<string, any>) => Promise<TxResult>
|
||||
search: (query: SearchQuery) => Promise<IndexedDoc[]>
|
||||
search: (search: string) => Promise<IndexedDoc[]>
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,7 +30,7 @@ describe('client', () => {
|
||||
content0: 'hey there!'
|
||||
}
|
||||
await adapter.index(doc)
|
||||
const hits = await adapter.search({})
|
||||
const hits = await adapter.search('')
|
||||
console.log(hits)
|
||||
})
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
//
|
||||
|
||||
import type { Doc, Ref, TxResult } from '@anticrm/core'
|
||||
import type { FullTextAdapter, IndexedDoc, SearchQuery } from '@anticrm/server-core'
|
||||
import type { FullTextAdapter, IndexedDoc } from '@anticrm/server-core'
|
||||
|
||||
import { Client } from '@elastic/elasticsearch'
|
||||
|
||||
@ -27,15 +27,16 @@ class ElasticAdapter implements FullTextAdapter {
|
||||
}
|
||||
|
||||
async search (
|
||||
query: SearchQuery
|
||||
search: string
|
||||
): Promise<IndexedDoc[]> {
|
||||
const query = search.replace(/[\\/+\-=&><!()|{}^"~*&:[\]]/g, '\\$&')
|
||||
try {
|
||||
const result = await this.client.search({
|
||||
index: this.db,
|
||||
body: {
|
||||
query: {
|
||||
multi_match: {
|
||||
query: query.$search,
|
||||
query: query,
|
||||
fields: [
|
||||
'content0',
|
||||
'content1',
|
||||
|
Loading…
Reference in New Issue
Block a user