Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide more context variables in update scripts #5724

Closed
wants to merge 1 commit into from

Conversation

ofavre
Copy link

@ofavre ofavre commented Apr 8, 2014

In addition to _source, the following variables are available through the ctx map: _index, _type, _id, _version, _routing, _parent, _timestamp, _ttl.

Some of these fields are more useful still within the context of an Update By Query, see #1607, #2230, #2231.

ofavre pushed a commit to yakaz/elasticsearch-action-updatebyquery that referenced this pull request Apr 8, 2014
if (fetchedTimestamp instanceof String) {
timestamp = (String) fetchedTimestamp;
} else {
timestamp = fetchedTimestamp.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.toString just returns this, so you could remove the instanceof check and cast after you ensure it's non-null.

@ofavre
Copy link
Author

ofavre commented Apr 10, 2014

Changed what @pickypg proposed + rebased to current master.

@clintongormley
Copy link
Contributor

@dakrone please could you take a look at this

"ctx._source.parent = ctx._parent;\n" +
"ctx._source.routing = ctx._routing;\n" +
"ctx._source.timestamp = ctx._timestamp;\n" +
"ctx._source.ttl = ctx._ttl;\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example, you can use assert directly in the script itself, so you could do

.setScript("assert ctx._version == 1 : \"version should be 1\"" +
           ... etc ...

If you would like to (I think either way is fine, just depends on personal preference).

@dakrone
Copy link
Member

dakrone commented Oct 20, 2014

@ofavre this looks pretty good, can you rebase on the latest master since this is an older PR and make sure this still works now that Groovy is the default scripting language?

assertEquals("parentId1", getResponse.getSourceAsMap().get("parent"));
assertEquals("routing1", getResponse.getSourceAsMap().get("routing"));
assertEquals(timestamp, getResponse.getSourceAsMap().get("timestamp"));
assertEquals((double)111211211 - (postUpdateTs - postIndexTs), ((Number)getResponse.getSourceAsMap().get("ttl")).doubleValue(), postIndexTs - preIndexTs + postUpdateTs - preUpdateTs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this assert in particular (the timestamp one) could be greatly simplified by asserting inside of the script itself instead of setting a new field value.

@ofavre
Copy link
Author

ofavre commented Nov 10, 2014

Thanks for the suggestions, they were valuable, and I managed to simplify the ttl assertion a bit.
I rebased on current master.

@@ -522,6 +522,99 @@ public void testUpdateRequestWithScriptAndShouldUpsertDoc() throws Exception {
}

@Test
public void testContextVariables() throws Exception {
createIndex();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty createIndex causes the tests to fail, it should be createIndex("test")

@dakrone
Copy link
Member

dakrone commented Nov 11, 2014

@ofavre thanks for the update! I left one more comment about a failing test, if you can fix that I'll merge this in

@ofavre
Copy link
Author

ofavre commented Nov 11, 2014

I somehow rebase on a 3 month old master.
Everything should now be ok.

@clintongormley clintongormley added review :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache and removed feedback_needed labels Nov 11, 2014
if (fetchedTimestamp != null) {
timestamp = fetchedTimestamp.toString();
} else {
timestamp = originalTimestamp.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originalTimestamp can be null here, causing an NPE

@dakrone
Copy link
Member

dakrone commented Nov 12, 2014

Hi @ofavre

I left another comment, running the tests with your change causes the following to fail:

  - org.elasticsearch.document.BulkTests.testBulkUpdate_largerVolume
  - org.elasticsearch.routing.AliasRoutingTests.testAliasCrudRouting

In addition to `_source`, the following variables are available through
the `ctx` map: `_index`, `_type`, `_id`, `_version`, `_routing`,
`_parent`, `_timestamp`, `_ttl`.

Some of these fields are more useful still within the context of an
Update By Query, see elastic#1607, elastic#2230, elastic#2231.
@ofavre
Copy link
Author

ofavre commented Nov 13, 2014

You're right, I've added a check for nullity in the else clause.
Sorry for that.

@dakrone
Copy link
Member

dakrone commented Nov 14, 2014

Pushed to 1.x and master, thanks @ofavre !

@dakrone dakrone closed this Nov 14, 2014
@ofavre
Copy link
Author

ofavre commented Nov 14, 2014

Thank you @clintongormley and @dakrone for pushing this forward! ;-)

@clintongormley clintongormley added :Distributed Indexing/CRUD A catch all label for issues around indexing, updating and getting a doc by id. Not search. and removed :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache labels Jun 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Distributed Indexing/CRUD A catch all label for issues around indexing, updating and getting a doc by id. Not search. >enhancement v1.5.0 v2.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants