Elastic search query escaping (#706)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov 2021-12-22 15:21:15 +06:00 committed by GitHub
parent d5c2c07b9e
commit 40500e6eda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 11 deletions

View File

@ -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>[] = []

View File

@ -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 }, () =>

View File

@ -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[]>
}
/**

View File

@ -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)
})

View File

@ -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',