-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Painless Context Doc: Add field context example #35130
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,59 @@ a customized value for each document in the results of a query. | |
| *API* | ||
|
|
||
| The standard <<painless-api-reference, Painless API>> is available. | ||
|
|
||
|
|
||
| *Example* | ||
|
|
||
| To run this example, first follow the steps in | ||
| <<painless-context-examples, context examples>>. | ||
|
|
||
| You can then use these two example scripts to compute custom information | ||
| for each search hit and output it to two new fields. | ||
|
|
||
| The first script gets the doc value for the `datetime` field and calls | ||
| the `getDayOfWeek` function to determine the corresponding day of the week. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The callout below is fairly redundant. I'd remove the callout and edit the intro to read: The first script gets the doc value for the |
||
| [source,Painless] | ||
| ---- | ||
| doc['datetime'].value.getDayOfWeek(); | ||
| ---- | ||
|
|
||
| The second script calculates the number of actors. Actors' names are stored | ||
| as a text array in the `actors` field. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say "as a text array" -- then you don't have to reitrate that I'd also change the : to a . to be consistent with the punctuation above. (And the second sentence adds info, rather than really leading into the example.) |
||
| [source,Painless] | ||
| ---- | ||
| params['_source']['actors'].length; <1> | ||
| ---- | ||
|
|
||
| <1> By default, doc values are not available for text fields. However, | ||
| you can still calculate the number of actors by extracting actors | ||
| from `_source`. Note that `params['_source']['actors']` is a list. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rephrase this slightly: By default, doc values are not available for text fields. However, you can still calculate the number of actors by extracting |
||
|
|
||
| Submit the following request: | ||
|
|
||
| [source,js] | ||
| ---- | ||
| GET seats/_search | ||
| { | ||
| "query" : { | ||
| "match_all": {} | ||
| }, | ||
| "script_fields" : { | ||
| "day-of-week" : { | ||
| "script" : { | ||
| "source": "doc['datetime'].value.getDayOfWeek()" | ||
| } | ||
| }, | ||
| "number-of-actors" : { | ||
| "script" : { | ||
| "source": "params['_source']['actors'].length" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ---- | ||
| // CONSOLE | ||
| // TEST[skip: requires setup from other pages] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, I recommend sticking with the present tense and avoiding "we" except for cases where "we" means "Elastic" (particularly in cases where you're making a recommendation). In this case, I'd probably edit this to read:
You can then use these example scripts to compute custom information and output it to two new fields.