-
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.
Add RuntimeField, esb.runtimeField, esb.requestBodySearch().runtimeFi…
…eld() and esb.requestBodySearch().runtimeFields()
- Loading branch information
1 parent
8002c89
commit 9f0e0fc
Showing
8 changed files
with
261 additions
and
1 deletion.
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,71 @@ | ||
'use strict'; | ||
|
||
const isNil = require('lodash.isnil'); | ||
const validType = [ | ||
'boolean', | ||
'composite', | ||
'date', | ||
'double', | ||
'geo_point', | ||
'ip', | ||
'keyword', | ||
'long', | ||
'lookup' | ||
]; | ||
|
||
class RuntimeField { | ||
constructor(type, script, name) { | ||
this._body = {}; | ||
this._name = name; | ||
this._isTypeSet = false; | ||
this._isScriptSet = false; | ||
|
||
if (!isNil(type)) { | ||
this.type(type); | ||
} | ||
|
||
if (!isNil(script)) { | ||
this.script(script); | ||
} | ||
} | ||
|
||
name(name) { | ||
this._name = name; | ||
} | ||
|
||
script(script) { | ||
this._body.script = { | ||
source: script | ||
}; | ||
this._isScriptSet = true; | ||
} | ||
|
||
type(type) { | ||
const typeLower = type.toLowerCase(); | ||
if (!validType.includes(typeLower)) { | ||
throw new Error(`\`type\` must be one of ${validType.join(', ')}`); | ||
} | ||
this._body.type = typeLower; | ||
this._isTypeSet = true; | ||
} | ||
|
||
/** | ||
* Override default `toJSON` to return DSL representation for the `script`. | ||
* | ||
* @override | ||
* @returns {Object} returns an Object which maps to the elasticsearch query DSL | ||
*/ | ||
toJSON() { | ||
if (!this._isTypeSet) { | ||
throw new Error('`type` should be set'); | ||
} | ||
|
||
if (!this._isScriptSet) { | ||
throw new Error('`script` should be set'); | ||
} | ||
|
||
return this._body; | ||
} | ||
} | ||
|
||
module.exports = RuntimeField; |
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,52 @@ | ||
import test from 'ava'; | ||
import RuntimeField from '../../src/core/runtime-field'; | ||
|
||
test('constructor set arguments', t => { | ||
const valueA = new RuntimeField( | ||
'keyword', | ||
"emit(doc['name'].value)" | ||
).toJSON(); | ||
const valueB = new RuntimeField( | ||
'keyword', | ||
"emit(doc['name'].value)" | ||
).toJSON(); | ||
t.deepEqual(valueA, valueB); | ||
|
||
const expected = { | ||
type: 'keyword', | ||
script: { | ||
source: "emit(doc['name'].value)" | ||
} | ||
}; | ||
t.deepEqual(valueA, expected); | ||
|
||
let err = t.throws(() => new RuntimeField().toJSON(), Error); | ||
t.is(err.message, '`type` should be set'); | ||
|
||
err = t.throws(() => new RuntimeField('keyword').toJSON(), Error); | ||
t.is(err.message, '`script` should be set'); | ||
}); | ||
|
||
test('type validate and set argument', t => { | ||
const fieldA = new RuntimeField('keyword', "emit(doc['name'].value)"); | ||
fieldA.type('boolean'); | ||
const expected = { | ||
type: 'boolean', | ||
script: { | ||
source: "emit(doc['name'].value)" | ||
} | ||
}; | ||
t.deepEqual(fieldA.toJSON(), expected); | ||
|
||
const err = t.throws(() => fieldA.type('invalid'), Error); | ||
t.is( | ||
err.message, | ||
'`type` must be one of boolean, composite, date, double, geo_point, ip, keyword, long, lookup' | ||
); | ||
}); | ||
|
||
test('name set _name', t => { | ||
const fieldA = new RuntimeField(); | ||
fieldA.name('field-name'); | ||
t.deepEqual(fieldA._name, 'field-name'); | ||
}); |