From 7d605485b2b520a95943e9b1fff00483a2aa5ce0 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 14 Sep 2020 17:13:49 +0200 Subject: [PATCH] Updated examples --- dsl/examples/boolean-logic.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/dsl/examples/boolean-logic.ts b/dsl/examples/boolean-logic.ts index f6e7c677f..37c673757 100644 --- a/dsl/examples/boolean-logic.ts +++ b/dsl/examples/boolean-logic.ts @@ -18,15 +18,18 @@ */ import { Client } from '../../' -import { Q } from '../' +import { Q, F } from '../' -async function run () { +/** + * Pure function API + */ +async function run1 () { const client = new Client({ node: 'http://localhost:9200' }) // define the query clauses const fixDescription = Q.must(Q.match('description', 'fix')) const files = Q.should(Q.term('files', 'test'), Q.term('files', 'docs')) - const author = Q.filter(Q.term('author.name', Q.param('author'))) + const author = Q.filter(Q.term('author.name', 'delvedor')) const { body } = await client.search({ index: 'git', // use the boolean utilities to craft the final query @@ -36,4 +39,24 @@ async function run () { console.log(body.hits.hits) } -run().catch(console.log) +/** + * Fluent API + */ +async function run2 () { + const client = new Client({ node: 'http://localhost:9200' }) + + // define the query clauses + const fixDescription = F().must(F().match('description', 'fix')) + const files = F().should(F().term('files', 'test').term('files', 'docs')) + const author = F().filter(F().term('author.name', 'delvedor')) + const { body } = await client.search({ + index: 'git', + // use the boolean utilities to craft the final query + body: F().and(fixDescription, files, author) + }) + + console.log(body.hits.hits) +} + +run1().catch(console.log) +run2().catch(console.log)