Skip to content

Commit 023602b

Browse files
committed
Merge branch 'master' into wait-for-pending-tasks
* master: Use trial license in docs tests (elastic#34673) Scripting: Convert script fields to use script context (elastic#34164) TEST: Mute testDedupByPrimaryTerm ingest: processor stats (elastic#34202)
2 parents 931b9d3 + bf5f0af commit 023602b

File tree

37 files changed

+959
-242
lines changed

37 files changed

+959
-242
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/StoredScriptsIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void testGetStoredScript() throws Exception {
4747
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
4848

4949
PutStoredScriptRequest request =
50-
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
50+
new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
5151
assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
5252

5353
GetStoredScriptRequest getRequest = new GetStoredScriptRequest("calculate-score");
@@ -66,7 +66,7 @@ public void testDeleteStoredScript() throws Exception {
6666
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
6767

6868
PutStoredScriptRequest request =
69-
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
69+
new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
7070
assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
7171

7272
DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest(id);
@@ -89,7 +89,7 @@ public void testPutScript() throws Exception {
8989
Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()));
9090

9191
PutStoredScriptRequest request =
92-
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
92+
new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
9393
assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
9494

9595
Map<String, Object> script = getAsMap("/_scripts/" + id);

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void onFailure(Exception e) {
307307

308308
private void putStoredScript(String id, StoredScriptSource scriptSource) throws IOException {
309309
PutStoredScriptRequest request =
310-
new PutStoredScriptRequest(id, "search", new BytesArray("{}"), XContentType.JSON, scriptSource);
310+
new PutStoredScriptRequest(id, "score", new BytesArray("{}"), XContentType.JSON, scriptSource);
311311
assertAcked(execute(request, highLevelClient()::putScript, highLevelClient()::putScriptAsync));
312312
}
313313
}

docs/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ buildRestTests.expectedUnconvertedCandidates = [
3636
]
3737

3838
integTestCluster {
39-
/* Enable regexes in painless so our tests don't complain about example
40-
* snippets that use them. */
39+
if ("zip".equals(integTestCluster.distribution)) {
40+
setting 'xpack.license.self_generated.type', 'trial'
41+
}
42+
43+
// enable regexes in painless so our tests don't complain about example snippets that use them
4144
setting 'script.painless.regex.enabled', 'true'
4245
Closure configFile = {
4346
extraConfigFile it, "src/test/cluster/config/$it"

docs/painless/painless-contexts/painless-field-context.asciidoc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,15 @@ a customized value for each document in the results of a query.
1414
Contains the fields of the specified document where each field is a
1515
`List` of values.
1616

17-
{ref}/mapping-source-field.html[`ctx['_source']`] (`Map`)::
17+
{ref}/mapping-source-field.html[`params['_source']`] (`Map`, read-only)::
1818
Contains extracted JSON in a `Map` and `List` structure for the fields
1919
existing in a stored document.
2020

21-
`_score` (`double` read-only)::
22-
The original score of the specified document.
23-
2421
*Return*
2522

2623
`Object`::
2724
The customized value for each document.
2825

2926
*API*
3027

31-
The standard <<painless-api-reference, Painless API>> is available.
28+
The standard <<painless-api-reference, Painless API>> is available.

docs/reference/licensing/get-license.asciidoc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ For more information, see
3939
[float]
4040
==== Examples
4141

42-
The following example provides information about a basic license:
42+
The following example provides information about a trial license:
4343

4444
[source,js]
4545
--------------------------------------------------
@@ -53,9 +53,11 @@ GET _xpack/license
5353
"license" : {
5454
"status" : "active",
5555
"uid" : "cbff45e7-c553-41f7-ae4f-9205eabd80xx",
56-
"type" : "basic",
57-
"issue_date" : "2018-02-22T23:12:05.550Z",
58-
"issue_date_in_millis" : 1519341125550,
56+
"type" : "trial",
57+
"issue_date" : "2018-10-20T22:05:12.332Z",
58+
"issue_date_in_millis" : 1540073112332,
59+
"expiry_date" : "2018-11-19T22:05:12.332Z",
60+
"expiry_date_in_millis" : 1542665112332,
5961
"max_nodes" : 1000,
6062
"issued_to" : "test",
6163
"issuer" : "elasticsearch",
@@ -65,8 +67,10 @@ GET _xpack/license
6567
--------------------------------------------------
6668
// TESTRESPONSE[s/"cbff45e7-c553-41f7-ae4f-9205eabd80xx"/$body.license.uid/]
6769
// TESTRESPONSE[s/"basic"/$body.license.type/]
68-
// TESTRESPONSE[s/"2018-02-22T23:12:05.550Z"/$body.license.issue_date/]
69-
// TESTRESPONSE[s/1519341125550/$body.license.issue_date_in_millis/]
70+
// TESTRESPONSE[s/"2018-10-20T22:05:12.332Z"/$body.license.issue_date/]
71+
// TESTRESPONSE[s/1540073112332/$body.license.issue_date_in_millis/]
72+
// TESTRESPONSE[s/"2018-11-19T22:05:12.332Z"/$body.license.expiry_date/]
73+
// TESTRESPONSE[s/1542665112332/$body.license.expiry_date_in_millis/]
7074
// TESTRESPONSE[s/1000/$body.license.max_nodes/]
7175
// TESTRESPONSE[s/"test"/$body.license.issued_to/]
7276
// TESTRESPONSE[s/"elasticsearch"/$body.license.issuer/]

docs/reference/rest-api/info.asciidoc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,25 @@ Example response:
5757
},
5858
"license" : {
5959
"uid" : "893361dc-9749-4997-93cb-xxx",
60-
"type" : "basic",
61-
"mode" : "basic",
62-
"status" : "active"
60+
"type" : "trial",
61+
"mode" : "trial",
62+
"status" : "active",
63+
"expiry_date_in_millis" : 1542665112332
6364
},
6465
"features" : {
6566
"graph" : {
6667
"description" : "Graph Data Exploration for the Elastic Stack",
67-
"available" : false,
68+
"available" : true,
6869
"enabled" : true
6970
},
7071
"logstash" : {
7172
"description" : "Logstash management component for X-Pack",
72-
"available" : false,
73+
"available" : true,
7374
"enabled" : true
7475
},
7576
"ml" : {
7677
"description" : "Machine Learning for the Elastic Stack",
77-
"available" : false,
78+
"available" : true,
7879
"enabled" : true,
7980
"native_code_info" : {
8081
"version" : "7.0.0-alpha1-SNAPSHOT",
@@ -93,12 +94,12 @@ Example response:
9394
},
9495
"security" : {
9596
"description" : "Security for the Elastic Stack",
96-
"available" : false,
97-
"enabled" : true
97+
"available" : true,
98+
"enabled" : false
9899
},
99100
"watcher" : {
100101
"description" : "Alerting, Notification and Automation for the Elastic Stack",
101-
"available" : false,
102+
"available" : true,
102103
"enabled" : true
103104
}
104105
},
@@ -108,7 +109,7 @@ Example response:
108109
// TESTRESPONSE[s/"hash" : "2798b1a3ce779b3611bb53a0082d4d741e4d3168",/"hash" : "$body.build.hash",/]
109110
// TESTRESPONSE[s/"date" : "2015-04-07T13:34:42Z"/"date" : "$body.build.date"/]
110111
// TESTRESPONSE[s/"uid" : "893361dc-9749-4997-93cb-xxx",/"uid": "$body.license.uid",/]
111-
// TESTRESPONSE[s/"expiry_date_in_millis" : 1914278399999/"expiry_date_in_millis" : "$body.license.expiry_date_in_millis"/]
112+
// TESTRESPONSE[s/"expiry_date_in_millis" : 1542665112332/"expiry_date_in_millis" : "$body.license.expiry_date_in_millis"/]
112113
// TESTRESPONSE[s/"version" : "7.0.0-alpha1-SNAPSHOT",/"version": "$body.features.ml.native_code_info.version",/]
113114
// TESTRESPONSE[s/"build_hash" : "99a07c016d5a73"/"build_hash": "$body.features.ml.native_code_info.build_hash"/]
114115
// So much s/// but at least we test that the layout is close to matching....
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.script.expression;
21+
22+
import org.apache.lucene.expressions.Expression;
23+
import org.apache.lucene.expressions.SimpleBindings;
24+
import org.apache.lucene.index.LeafReaderContext;
25+
import org.apache.lucene.search.DoubleValues;
26+
import org.apache.lucene.search.DoubleValuesSource;
27+
import org.elasticsearch.script.FieldScript;
28+
import org.elasticsearch.script.GeneralScriptException;
29+
30+
import java.io.IOException;
31+
32+
public class ExpressionFieldScript implements FieldScript.LeafFactory {
33+
private final Expression exprScript;
34+
private final DoubleValuesSource source;
35+
36+
ExpressionFieldScript(Expression e, SimpleBindings b) {
37+
this.exprScript = e;
38+
this.source = exprScript.getDoubleValuesSource(b);
39+
}
40+
41+
@Override
42+
public FieldScript newInstance(final LeafReaderContext leaf) throws IOException {
43+
return new FieldScript() {
44+
45+
// Fake the scorer until setScorer is called.
46+
DoubleValues values = source.getValues(leaf, null);
47+
48+
@Override
49+
public Object execute() {
50+
try {
51+
return values.doubleValue();
52+
} catch (Exception exception) {
53+
throw new GeneralScriptException("Error evaluating " + exprScript, exception);
54+
}
55+
}
56+
57+
@Override
58+
public void setDocument(int d) {
59+
try {
60+
values.advanceExact(d);
61+
} catch (IOException e) {
62+
throw new IllegalStateException("Can't advance to doc using " + exprScript, e);
63+
}
64+
}
65+
};
66+
}
67+
}

modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.script.BucketAggregationScript;
4242
import org.elasticsearch.script.BucketAggregationSelectorScript;
4343
import org.elasticsearch.script.ClassPermission;
44+
import org.elasticsearch.script.FieldScript;
4445
import org.elasticsearch.script.FilterScript;
4546
import org.elasticsearch.script.NumberSortScript;
4647
import org.elasticsearch.script.ScoreScript;
@@ -139,6 +140,9 @@ public boolean execute() {
139140
} else if (context.instanceClazz.equals(NumberSortScript.class)) {
140141
NumberSortScript.Factory factory = (p, lookup) -> newSortScript(expr, lookup, p);
141142
return context.factoryClazz.cast(factory);
143+
} else if (context.instanceClazz.equals(FieldScript.class)) {
144+
FieldScript.Factory factory = (p, lookup) -> newFieldScript(expr, lookup, p);
145+
return context.factoryClazz.cast(factory);
142146
}
143147
throw new IllegalArgumentException("expression engine does not know how to handle script context [" + context.name + "]");
144148
}
@@ -289,6 +293,23 @@ private AggregationScript.LeafFactory newAggregationScript(Expression expr, Sear
289293
return new ExpressionAggregationScript(expr, bindings, specialValue);
290294
}
291295

296+
private FieldScript.LeafFactory newFieldScript(Expression expr, SearchLookup lookup, @Nullable Map<String, Object> vars) {
297+
SimpleBindings bindings = new SimpleBindings();
298+
for (String variable : expr.variables) {
299+
try {
300+
if (vars != null && vars.containsKey(variable)) {
301+
bindFromParams(vars, bindings, variable);
302+
} else {
303+
final ValueSource valueSource = getDocValueSource(variable, lookup);
304+
bindings.add(variable, valueSource.asDoubleValuesSource());
305+
}
306+
} catch (Exception e) {
307+
throw convertToScriptException("link error", expr.sourceText, variable, e);
308+
}
309+
}
310+
return new ExpressionFieldScript(expr, bindings);
311+
}
312+
292313
/**
293314
* This is a hack for filter scripts, which must return booleans instead of doubles as expression do.
294315
* See https://github.com/elastic/elasticsearch/issues/26429.

modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTests.java renamed to modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@
2424
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
2525
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
2626
import org.elasticsearch.index.mapper.MapperService;
27-
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType;
28-
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
27+
import org.elasticsearch.index.mapper.NumberFieldMapper;
28+
import org.elasticsearch.script.FieldScript;
2929
import org.elasticsearch.script.ScriptException;
30-
import org.elasticsearch.script.SearchScript;
3130
import org.elasticsearch.search.lookup.SearchLookup;
3231
import org.elasticsearch.test.ESTestCase;
3332

3433
import java.io.IOException;
3534
import java.text.ParseException;
3635
import java.util.Collections;
3736

37+
import static org.hamcrest.Matchers.equalTo;
3838
import static org.mockito.Matchers.anyInt;
3939
import static org.mockito.Matchers.anyObject;
4040
import static org.mockito.Mockito.mock;
4141
import static org.mockito.Mockito.when;
4242

43-
public class ExpressionTests extends ESTestCase {
43+
public class ExpressionFieldScriptTests extends ESTestCase {
4444
private ExpressionScriptEngine service;
4545
private SearchLookup lookup;
4646

4747
@Override
4848
public void setUp() throws Exception {
4949
super.setUp();
5050

51-
NumberFieldType fieldType = new NumberFieldType(NumberType.DOUBLE);
51+
NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
5252
MapperService mapperService = mock(MapperService.class);
5353
when(mapperService.fullName("field")).thenReturn(fieldType);
5454
when(mapperService.fullName("alias")).thenReturn(fieldType);
@@ -68,18 +68,11 @@ public void setUp() throws Exception {
6868
lookup = new SearchLookup(mapperService, ignored -> fieldData, null);
6969
}
7070

71-
private SearchScript.LeafFactory compile(String expression) {
72-
SearchScript.Factory factory = service.compile(null, expression, SearchScript.CONTEXT, Collections.emptyMap());
71+
private FieldScript.LeafFactory compile(String expression) {
72+
FieldScript.Factory factory = service.compile(null, expression, FieldScript.CONTEXT, Collections.emptyMap());
7373
return factory.newFactory(Collections.emptyMap(), lookup);
7474
}
7575

76-
public void testNeedsScores() {
77-
assertFalse(compile("1.2").needs_score());
78-
assertFalse(compile("doc['field'].value").needs_score());
79-
assertTrue(compile("1/_score").needs_score());
80-
assertTrue(compile("doc['field'].value * _score").needs_score());
81-
}
82-
8376
public void testCompileError() {
8477
ScriptException e = expectThrows(ScriptException.class, () -> {
8578
compile("doc['field'].value * *@#)(@$*@#$ + 4");
@@ -95,18 +88,18 @@ public void testLinkError() {
9588
}
9689

9790
public void testFieldAccess() throws IOException {
98-
SearchScript script = compile("doc['field'].value").newInstance(null);
91+
FieldScript script = compile("doc['field'].value").newInstance(null);
9992
script.setDocument(1);
10093

101-
double result = script.runAsDouble();
102-
assertEquals(2.718, result, 0.0);
94+
Object result = script.execute();
95+
assertThat(result, equalTo(2.718));
10396
}
10497

10598
public void testFieldAccessWithFieldAlias() throws IOException {
106-
SearchScript script = compile("doc['alias'].value").newInstance(null);
99+
FieldScript script = compile("doc['alias'].value").newInstance(null);
107100
script.setDocument(1);
108101

109-
double result = script.runAsDouble();
110-
assertEquals(2.718, result, 0.0);
102+
Object result = script.execute();
103+
assertThat(result, equalTo(2.718));
111104
}
112105
}

modules/lang-expression/src/test/java/org/elasticsearch/script/expression/StoredExpressionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testAllOpsDisabledIndexedScripts() throws IOException {
7070
.setIndices("test").setTypes("scriptTest").get();
7171
fail("search script should have been rejected");
7272
} catch(Exception e) {
73-
assertThat(e.toString(), containsString("cannot execute scripts using [search] context"));
73+
assertThat(e.toString(), containsString("cannot execute scripts using [field] context"));
7474
}
7575
try {
7676
client().prepareSearch("test")

0 commit comments

Comments
 (0)