mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-13 19:58:09 +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>> {
|
): Promise<FindResult<T>> {
|
||||||
console.log('search', query)
|
console.log('search', query)
|
||||||
const { _id, $search, ...mainQuery } = query
|
const { _id, $search, ...mainQuery } = query
|
||||||
|
if ($search === undefined) return []
|
||||||
const docs = await this.adapter.search($search)
|
const docs = await this.adapter.search($search)
|
||||||
console.log(docs)
|
console.log(docs)
|
||||||
const ids: Ref<Doc>[] = []
|
const ids: Ref<Doc>[] = []
|
||||||
|
@ -164,7 +164,7 @@ class TServerStorage implements ServerStorage {
|
|||||||
): Promise<FindResult<T>> {
|
): Promise<FindResult<T>> {
|
||||||
return await ctx.with('find-all', {}, (ctx) => {
|
return await ctx.with('find-all', {}, (ctx) => {
|
||||||
const domain = this.hierarchy.getDomain(clazz)
|
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('full-text-find-all', {}, (ctx) => this.fulltext.findAll(ctx, clazz, query, options))
|
||||||
}
|
}
|
||||||
return ctx.with('db-find-all', { _class: clazz, domain }, () =>
|
return ctx.with('db-find-all', { _class: clazz, domain }, () =>
|
||||||
|
@ -58,18 +58,13 @@ export interface IndexedDoc {
|
|||||||
data?: string
|
data?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
export type SearchQuery = any // TODO: replace with DocumentQuery
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export interface FullTextAdapter {
|
export interface FullTextAdapter {
|
||||||
index: (doc: IndexedDoc) => Promise<TxResult>
|
index: (doc: IndexedDoc) => Promise<TxResult>
|
||||||
update: (id: Ref<Doc>, update: Record<string, any>) => 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!'
|
content0: 'hey there!'
|
||||||
}
|
}
|
||||||
await adapter.index(doc)
|
await adapter.index(doc)
|
||||||
const hits = await adapter.search({})
|
const hits = await adapter.search('')
|
||||||
console.log(hits)
|
console.log(hits)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import type { Doc, Ref, TxResult } from '@anticrm/core'
|
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'
|
import { Client } from '@elastic/elasticsearch'
|
||||||
|
|
||||||
@ -27,15 +27,16 @@ class ElasticAdapter implements FullTextAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async search (
|
async search (
|
||||||
query: SearchQuery
|
search: string
|
||||||
): Promise<IndexedDoc[]> {
|
): Promise<IndexedDoc[]> {
|
||||||
|
const query = search.replace(/[\\/+\-=&><!()|{}^"~*&:[\]]/g, '\\$&')
|
||||||
try {
|
try {
|
||||||
const result = await this.client.search({
|
const result = await this.client.search({
|
||||||
index: this.db,
|
index: this.db,
|
||||||
body: {
|
body: {
|
||||||
query: {
|
query: {
|
||||||
multi_match: {
|
multi_match: {
|
||||||
query: query.$search,
|
query: query,
|
||||||
fields: [
|
fields: [
|
||||||
'content0',
|
'content0',
|
||||||
'content1',
|
'content1',
|
||||||
|
Loading…
Reference in New Issue
Block a user