-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for script score query (#191)
Script score query was added in Elasticsearch v7.0
- Loading branch information
Showing
6 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
const { | ||
Query, | ||
Script, | ||
util: { checkType } | ||
} = require('../../core'); | ||
|
||
/** | ||
* A query that uses a script to provide a custom score for returned documents. | ||
* | ||
* [Elasticsearch reference](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html) | ||
* | ||
* NOTE: This query was added in elasticsearch v7.0. | ||
* | ||
* @example | ||
* const qry = esb.scriptScoreQuery() | ||
* .query(esb.matchQuery("message", "elasticsearch")) | ||
* .script(esb.script().source("doc['my-int'].value / 10")) | ||
* | ||
* @extends Query | ||
*/ | ||
class ScriptScoreQuery extends Query { | ||
// eslint-disable-next-line require-jsdoc | ||
constructor() { | ||
super('script_score'); | ||
} | ||
|
||
/** | ||
* Sets the query used to return documents. | ||
* | ||
* @param {Query} query A valid `Query` object | ||
* @returns {ScriptScoreQuery} returns `this` so that calls can be chained. | ||
*/ | ||
query(query) { | ||
checkType(query, Query); | ||
|
||
this._queryOpts.query = query; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the script used to compute the score of documents returned by the query. | ||
* | ||
* @param {Script} script A valid `Script` object | ||
* @returns {ScriptScoreQuery} returns `this` so that calls can be chained. | ||
*/ | ||
script(script) { | ||
checkType(script, Script); | ||
|
||
this._queryOpts.script = script; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the minimum score limit for documents to be included in search result. | ||
* | ||
* @param {number} limit Minimum score threshold | ||
* @returns {ScriptScoreQuery} returns `this` so that calls can be chained. | ||
*/ | ||
minScore(limit) { | ||
this._queryOpts.min_score = limit; | ||
return this; | ||
} | ||
} | ||
|
||
module.exports = ScriptScoreQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import test from 'ava'; | ||
import { | ||
MatchQuery, | ||
Script, | ||
ScriptScoreQuery, | ||
scriptScoreQuery | ||
} from '../../src'; | ||
import { | ||
illegalParamType, | ||
makeSetsOptionMacro, | ||
nameExpectStrategy | ||
} from '../_macros'; | ||
|
||
const setsOption = makeSetsOptionMacro( | ||
scriptScoreQuery, | ||
nameExpectStrategy('script_score') | ||
); | ||
|
||
const query = new MatchQuery('message', 'elasticsearch'); | ||
|
||
const lang = 'painless'; | ||
const source = | ||
"decayNumericLinear(params.origin, params.scale, params.offset, params.decay, doc['dval'].value)"; | ||
const params = { origin: 20, scale: 10, decay: 0.5, offset: 0 }; | ||
const script = new Script() | ||
.lang(lang) | ||
.source(source) | ||
.params(params); | ||
|
||
const instance = new ScriptScoreQuery(); | ||
|
||
test(illegalParamType, instance, 'query', 'Query'); | ||
test(illegalParamType, instance, 'script', 'Script'); | ||
|
||
test(setsOption, 'query', { param: query }); | ||
test(setsOption, 'script', { param: script }); | ||
test(setsOption, 'minScore', { param: 9.999 }); |