Skip to content

Commit

Permalink
Script: support boolean fields in Fields API
Browse files Browse the repository at this point in the history
Add support for boolean fields with the following public API:

* `boolean getValue(boolean)`
  Return the first boolean or the given default

* `boolean getValue(int, boolean)`
  Return the boolean at index or the given default

* `boolean[] getValues()`
  Return a copy of the array of booleans, may return a zero length array.

Refs: elastic#79105
  • Loading branch information
stu-elastic committed Oct 28, 2021
1 parent f9bf4e5 commit eea3594
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ class org.elasticsearch.script.field.DelegateDocValuesField @dynamic_type {
def getValue(def)
List getValues()
}

class org.elasticsearch.script.field.BooleanDocValuesField @dynamic_type {
boolean getValue(boolean)
boolean getValue(int, boolean)
boolean[] getValues()
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ setup:
scaled_float: 3.14
token_count: count all these words please

- do:
index:
index: test
id: 2
body: {}

- do:
indices.refresh: {}

Expand All @@ -67,6 +73,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -77,12 +84,80 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
source: "doc['boolean'].value"
- match: { hits.hits.0.fields.field.0: true }

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
source: "field('boolean').getValue(false)"
- match: { hits.hits.0.fields.field.0: true }

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
source: "field('boolean').getValue(false)"
- match: { hits.hits.0.fields.field.0: true }

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 2 } }
script_fields:
field:
script:
source: "field('boolean').getValue(false)"
- match: { hits.hits.0.fields.field.0: false }

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
source: "field('boolean').getValue(1, false)"
- match: { hits.hits.0.fields.field.0: false }

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
source: "field('boolean').getValues().length"
- match: { hits.hits.0.fields.field.0: 1 }

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 2 } }
script_fields:
field:
script:
source: "field('boolean').getValues().length"
- match: { hits.hits.0.fields.field.0: 0 }


---
"date":
- skip:
Expand All @@ -92,6 +167,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -102,6 +178,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -114,6 +191,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -125,6 +203,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -136,6 +215,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
centroid:
script:
Expand All @@ -147,6 +227,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
bbox:
script:
Expand All @@ -160,6 +241,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
topLeft:
script:
Expand All @@ -176,6 +258,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
type:
script:
Expand All @@ -186,6 +269,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
width:
script:
Expand All @@ -202,6 +286,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -212,6 +297,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -224,6 +310,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -234,6 +321,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -249,6 +337,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -259,6 +348,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -271,6 +361,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -281,6 +372,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -293,6 +385,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -303,6 +396,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -315,6 +409,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -325,6 +420,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -337,6 +433,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -347,6 +444,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -359,6 +457,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -369,6 +468,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -381,6 +481,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -391,6 +492,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -403,6 +505,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -413,6 +516,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -425,6 +529,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand All @@ -435,6 +540,7 @@ setup:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
Expand Down
Loading

0 comments on commit eea3594

Please sign in to comment.