Updated examples
This commit is contained in:
@ -18,56 +18,103 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Client } from '../../'
|
import { Client } from '../../'
|
||||||
import { Q } from '../'
|
import { Q, F } from '../'
|
||||||
|
|
||||||
// You can compile a query if you need to get
|
/**
|
||||||
// the best performances out of your code.
|
* Pure functions API
|
||||||
// The query crafting and compilation should be done
|
*/
|
||||||
// outside of your hot code path.
|
{
|
||||||
// First of all yu should create your query almost
|
// You can compile a query if you need to get
|
||||||
// in the same way as you were doing before, the only
|
// the best performances out of your code.
|
||||||
// difference, is that all the paramegers you are passing
|
// The query crafting and compilation should be done
|
||||||
// now should be updated with the `Q.param` API.
|
// outside of your hot code path.
|
||||||
// The only parameter or `Q.param`, is the name of the parameter
|
// First of all you should create your query almost
|
||||||
// that you were passing before.
|
// in the same way as you were doing before, the only
|
||||||
const query = Q(
|
// difference, is that all the paramegers you are passing
|
||||||
Q.match('description', Q.param('description')),
|
// now should be updated with the `Q.param` API.
|
||||||
Q.filter(
|
// The only parameter or `Q.param`, is the name of the parameter
|
||||||
Q.term('author.name', Q.param('author'))
|
// that you were passing before.
|
||||||
),
|
const query = Q(
|
||||||
Q.size(10)
|
Q.match('description', Q.param('description')),
|
||||||
)
|
Q.filter(
|
||||||
|
Q.term('author.name', Q.param('author'))
|
||||||
|
),
|
||||||
|
Q.size(10)
|
||||||
|
)
|
||||||
|
|
||||||
// Afterwards, you can create an interface that represents
|
// Afterwards, you can create an interface that represents
|
||||||
// the input object of the compiled query. The input object
|
// the input object of the compiled query. The input object
|
||||||
// contains all the parameters you were passing before, the
|
// contains all the parameters you were passing before, the
|
||||||
// keys are the same you have passed to the various `Q.param`
|
// keys are the same you have passed to the various `Q.param`
|
||||||
// invocations before. It defaults to `unknown`.
|
// invocations before. It defaults to `unknown`.
|
||||||
interface Input {
|
interface Input {
|
||||||
description: string
|
description: string
|
||||||
author: string
|
author: string
|
||||||
}
|
}
|
||||||
// Once you have created the query and the input interface,
|
// In this example we will use `Q.compile`, the returned function
|
||||||
// you must pass the query to `Q.compile` and store the result
|
// works in the same way as `Q.compileUnsafe` but the function returned by the
|
||||||
// in a variable. `Q.compile` returns a function that accepts
|
// safe API is an order of magnitude slower.
|
||||||
// a single object parameter, which is the same you have declared
|
// `Q.compile` can be used with unstrusted input (but it's not recommended).
|
||||||
// in the interface before.
|
// Once you have created the query and the input interface,
|
||||||
const compiledQuery = Q.compile<Input>(query)
|
// you must pass the query to `Q.compile` and store the result
|
||||||
|
// in a variable. `Q.compile` returns a function that accepts
|
||||||
|
// a single object parameter, which is the same you have declared
|
||||||
|
// in the interface before.
|
||||||
|
const compiledQuery = Q.compile<Input>(query)
|
||||||
|
|
||||||
async function run () {
|
async function run () {
|
||||||
const client = new Client({ node: 'http://localhost:9200' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
const { body } = await client.search({
|
const { body } = await client.search({
|
||||||
index: 'git',
|
index: 'git',
|
||||||
// Finally, you call the function inside your hot code path,
|
// Finally, you call the function inside your hot code path,
|
||||||
// the returned value will be the query.
|
// the returned value will be the query.
|
||||||
body: compiledQuery({
|
body: compiledQuery({
|
||||||
description: 'fix',
|
description: 'fix',
|
||||||
author: 'delvedor'
|
author: 'delvedor'
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
console.log(body.hits.hits)
|
console.log(body.hits.hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
run().catch(console.log)
|
||||||
}
|
}
|
||||||
|
|
||||||
run().catch(console.log)
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
// The theory behind query compilation is the same here,
|
||||||
|
// the query crafting and compilation should be done
|
||||||
|
// outside of your hot code path.
|
||||||
|
const query = new F()
|
||||||
|
.match('description', Q.param('description'))
|
||||||
|
.filter(f => f
|
||||||
|
.term('author.name', Q.param('author'))
|
||||||
|
)
|
||||||
|
.size(10)
|
||||||
|
|
||||||
|
interface Input {
|
||||||
|
description: string
|
||||||
|
author: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const compiledQuery = query.compile<Input>()
|
||||||
|
|
||||||
|
async function run () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: compiledQuery({
|
||||||
|
description: 'fix',
|
||||||
|
author: 'delvedor'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.hits.hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
run().catch(console.log)
|
||||||
|
}
|
||||||
|
|||||||
120
dsl/examples/compile-unsafe-query.ts
Normal file
120
dsl/examples/compile-unsafe-query.ts
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||||
|
* license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright
|
||||||
|
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||||
|
* the Apache 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
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Client } from '../../'
|
||||||
|
import { Q, F } from '../'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pure functions API
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
// You can compile a query if you need to get
|
||||||
|
// the best performances out of your code.
|
||||||
|
// The query crafting and compilation should be done
|
||||||
|
// outside of your hot code path.
|
||||||
|
// First of all you should create your query almost
|
||||||
|
// in the same way as you were doing before, the only
|
||||||
|
// difference, is that all the paramegers you are passing
|
||||||
|
// now should be updated with the `Q.param` API.
|
||||||
|
// The only parameter or `Q.param`, is the name of the parameter
|
||||||
|
// that you were passing before.
|
||||||
|
const query = Q(
|
||||||
|
Q.match('description', Q.param('description')),
|
||||||
|
Q.filter(
|
||||||
|
Q.term('author.name', Q.param('author'))
|
||||||
|
),
|
||||||
|
Q.size(10)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Afterwards, you can create an interface that represents
|
||||||
|
// the input object of the compiled query. The input object
|
||||||
|
// contains all the parameters you were passing before, the
|
||||||
|
// keys are the same you have passed to the various `Q.param`
|
||||||
|
// invocations before. It defaults to `unknown`.
|
||||||
|
interface Input {
|
||||||
|
description: string
|
||||||
|
author: string
|
||||||
|
}
|
||||||
|
// In this example we will use `Q.compileUnsafe`, the returned function
|
||||||
|
// works in the same way as `Q.compile` but the function returned by the
|
||||||
|
// unsafe API is an order of magnitude faster.
|
||||||
|
// You should NEVER use `Q.compileUnsafe` with untrusted input.
|
||||||
|
// Once you have created the query and the input interface,
|
||||||
|
// you must pass the query to `Q.compileUnsafe` and store the result
|
||||||
|
// in a variable. `Q.compile` returns a function that accepts
|
||||||
|
// a single object parameter, which is the same you have declared
|
||||||
|
// in the interface before.
|
||||||
|
const compiledQuery = Q.compileUnsafe<Input>(query)
|
||||||
|
|
||||||
|
async function run () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
// Finally, you call the function inside your hot code path,
|
||||||
|
// the returned value will be the query.
|
||||||
|
body: compiledQuery({
|
||||||
|
description: 'fix',
|
||||||
|
author: 'delvedor'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.hits.hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
run().catch(console.log)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
// The theory behind query compilation is the same here,
|
||||||
|
// the query crafting and compilation should be done
|
||||||
|
// outside of your hot code path.
|
||||||
|
const query = new F()
|
||||||
|
.match('description', Q.param('description'))
|
||||||
|
.filter(f => f
|
||||||
|
.term('author.name', Q.param('author'))
|
||||||
|
)
|
||||||
|
.size(10)
|
||||||
|
|
||||||
|
interface Input {
|
||||||
|
description: string
|
||||||
|
author: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const compiledQuery = query.compileUnsafe<Input>()
|
||||||
|
|
||||||
|
async function run () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: compiledQuery({
|
||||||
|
description: 'fix',
|
||||||
|
author: 'delvedor'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.hits.hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
run().catch(console.log)
|
||||||
|
}
|
||||||
@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Client } from '../../'
|
import { Client } from '../../'
|
||||||
import { Q, A } from '../'
|
import { Q, A, F } from '../'
|
||||||
|
|
||||||
async function run () {
|
/**
|
||||||
|
* Pure function API
|
||||||
|
*/
|
||||||
|
async function run1 () {
|
||||||
const client = new Client({ node: 'http://localhost:9200' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
// get the day where the most commits were made
|
// get the day where the most commits were made
|
||||||
@ -41,4 +44,30 @@ async function run () {
|
|||||||
console.log(body.aggregations)
|
console.log(body.aggregations)
|
||||||
}
|
}
|
||||||
|
|
||||||
run().catch(console.log)
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
async function run2 () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
// get the day where the most commits were made
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: new F()
|
||||||
|
.size(0)
|
||||||
|
// 'day_most_commits' is the name of the aggregation
|
||||||
|
.aggs(
|
||||||
|
A.day_most_commits.dateHistogram({
|
||||||
|
field: 'committed_date',
|
||||||
|
interval: 'day',
|
||||||
|
min_doc_count: 1,
|
||||||
|
order: { _count: 'desc' }
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.aggregations)
|
||||||
|
}
|
||||||
|
|
||||||
|
run1().catch(console.log)
|
||||||
|
run2().catch(console.log)
|
||||||
|
|||||||
@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Client } from '../../'
|
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' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
// search commits that contains 'fix' but do not changes test files
|
// search commits that contains 'fix' but do not changes test files
|
||||||
@ -37,4 +40,24 @@ async function run () {
|
|||||||
console.log(body.hits.hits)
|
console.log(body.hits.hits)
|
||||||
}
|
}
|
||||||
|
|
||||||
run().catch(console.log)
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
async function run2 () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
// search commits that contains 'fix' but do not changes test files
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: new F()
|
||||||
|
// You can avoid to call `.must`, as any query will be
|
||||||
|
// sent inside a `must` block unless specified otherwise
|
||||||
|
.must(f => f.match('description', 'fix'))
|
||||||
|
.mustNot(f => f.term('files', 'test'))
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.hits.hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
run1().catch(console.log)
|
||||||
|
run2().catch(console.log)
|
||||||
|
|||||||
@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Client } from '../../'
|
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' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
// last 10 commits for 'elasticsearch-js' repo
|
// last 10 commits for 'elasticsearch-js' repo
|
||||||
@ -36,4 +39,23 @@ async function run () {
|
|||||||
console.log(body.hits.hits)
|
console.log(body.hits.hits)
|
||||||
}
|
}
|
||||||
|
|
||||||
run().catch(console.log)
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
async function run2 () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
// last 10 commits for 'elasticsearch-js' repo
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: new F()
|
||||||
|
.term('repository', 'elasticsearch-js')
|
||||||
|
.sort('committed_date', { order: 'desc' })
|
||||||
|
.size(10)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.hits.hits)
|
||||||
|
}
|
||||||
|
|
||||||
|
run1().catch(console.log)
|
||||||
|
run2().catch(console.log)
|
||||||
|
|||||||
@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Client } from '../../'
|
import { Client } from '../../'
|
||||||
import { Q, A } from '../'
|
import { Q, A, F } from '../'
|
||||||
|
|
||||||
async function run () {
|
/**
|
||||||
|
* Pure function API
|
||||||
|
*/
|
||||||
|
async function run1 () {
|
||||||
const client = new Client({ node: 'http://localhost:9200' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
// top committers aggregation
|
// top committers aggregation
|
||||||
@ -44,4 +47,31 @@ async function run () {
|
|||||||
console.log(body.aggregations)
|
console.log(body.aggregations)
|
||||||
}
|
}
|
||||||
|
|
||||||
run().catch(console.log)
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
async function run2 () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
// top committers aggregation
|
||||||
|
// 'committers' is the name of the aggregation
|
||||||
|
const committersAgg = A.committers.terms(
|
||||||
|
{ field: 'committer.name.keyword' },
|
||||||
|
// you can nest multiple aggregations by
|
||||||
|
// passing them to the aggregation constructor
|
||||||
|
// 'line_stats' is the name of the aggregation
|
||||||
|
A.line_stats.stats({ field: 'stat.insertions' })
|
||||||
|
)
|
||||||
|
const { body } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: new F()
|
||||||
|
.matchAll()
|
||||||
|
.size(0)
|
||||||
|
.aggs(committersAgg)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(body.aggregations)
|
||||||
|
}
|
||||||
|
|
||||||
|
run1().catch(console.log)
|
||||||
|
run2().catch(console.log)
|
||||||
|
|||||||
@ -18,9 +18,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Client } from '../../'
|
import { Client } from '../../'
|
||||||
import { Q, A } from '../'
|
import { Q, A, F } from '../'
|
||||||
|
|
||||||
async function run () {
|
/**
|
||||||
|
* Pure function API
|
||||||
|
*/
|
||||||
|
async function run1 () {
|
||||||
const client = new Client({ node: 'http://localhost:9200' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
const committers = A.committers.terms(
|
const committers = A.committers.terms(
|
||||||
@ -58,4 +61,45 @@ async function run () {
|
|||||||
console.log(topMonths)
|
console.log(topMonths)
|
||||||
}
|
}
|
||||||
|
|
||||||
run().catch(console.log)
|
/**
|
||||||
|
* Fluent API
|
||||||
|
*/
|
||||||
|
async function run2 () {
|
||||||
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
|
const committers = A.committers.terms(
|
||||||
|
{ field: 'committer.name.keyword' },
|
||||||
|
A.insertions.sum({ field: 'stat.insertions' })
|
||||||
|
)
|
||||||
|
const topCommittersPerMonth = A.top_committer_per_month.maxBucket(
|
||||||
|
{ bucket_path: 'committers>insertions' }
|
||||||
|
)
|
||||||
|
const commitsPerMonth = A.commits_per_month.dateHistogram(
|
||||||
|
{
|
||||||
|
field: 'committed_date',
|
||||||
|
interval: 'day',
|
||||||
|
min_doc_count: 1,
|
||||||
|
order: { _count: 'desc' }
|
||||||
|
},
|
||||||
|
// nested aggregations
|
||||||
|
committers,
|
||||||
|
topCommittersPerMonth
|
||||||
|
)
|
||||||
|
const topCommittersPerMonthGlobal = A.top_committer_per_month.maxBucket(
|
||||||
|
{ bucket_path: 'commits_per_month>top_committer_per_month' }
|
||||||
|
)
|
||||||
|
|
||||||
|
const { body: topMonths } = await client.search({
|
||||||
|
index: 'git',
|
||||||
|
body: new F()
|
||||||
|
// we want to know the top month for 'delvedor'
|
||||||
|
.filter(f => f.term('author', 'delvedor'))
|
||||||
|
.size(0)
|
||||||
|
.aggs(commitsPerMonth, topCommittersPerMonthGlobal)
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(topMonths)
|
||||||
|
}
|
||||||
|
|
||||||
|
run1().catch(console.log)
|
||||||
|
run2().catch(console.log)
|
||||||
|
|||||||
Reference in New Issue
Block a user