-
Notifications
You must be signed in to change notification settings - Fork 245
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
Raw json support #200
Raw json support #200
Conversation
@spinscale @dadoonet can you have a look at the docs and the @szabosteve can you do a quick review of the docs (rendered)? |
I'm wondering why do we need to provide the CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index("some-index")
.withJson(
jsonStream,
client._jsonpMapper()
)
); Would it be possible to have a default one like this: CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index("some-index")
.withJson(jsonStream)
); |
About the documentation, I think that each example should be self contained. So for the last section, add again the Reader queryJson = new StringReader(
"{" +
" \"query\": {" +
" \"range\": {" +
" \"@timestamp\": {" +
" \"gt\": \"now-1w\"" +
" }" +
" }" +
" }" +
"}"); |
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.
Left some comments.
|
||
|
||
public void query1() throws IOException { | ||
//tag::query |
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.
I'd write:
{
//tag::query
Reader queryJson = new StringReader(
"{" +
" \"query\": {" +
" \"range\": {" +
" \"@timestamp\": {" +
" \"gt\": \"now-1w\"" +
" }" +
" }" +
" }" +
"}");
SearchRequest agg1 = SearchRequest.of(b -> b
.withJson(queryJson, client._jsonpMapper()) //<1>
.aggregations("max-cpu", a1 -> a1 //<2>
.dateHistogram(h -> h
.field("@timestamp")
.calendarInterval(CalendarInterval.Hour)
)
.aggregations("max", a2 -> a2
.max(m -> m.field("host.cpu.usage"))
)
)
.size(0)
);
Map<String, Aggregate> aggs1 = client
.search(agg1, Void.class) //<3>
.aggregations();
//end::query
}
{
//tag::query-and-agg
Reader queryJson = new StringReader(
"{" +
" \"query\": {" +
" \"range\": {" +
" \"@timestamp\": {" +
" \"gt\": \"now-1w\"" +
" }" +
" }" +
" }" +
"}");
Reader aggJson = new StringReader(
"\"size\": 0, " +
"\"aggs\": {" +
" \"hours\": {" +
" \"date_histogram\": {" +
" \"field\": \"@timestamp\"," +
" \"interval\": \"hour\"" +
" }," +
" \"aggs\": {" +
" \"max-cpu\": {" +
" \"max\": {" +
" \"field\": \"host.cpu.usage\"" +
" }" +
" }" +
" }" +
" }" +
"}");
SearchRequest agg2 = SearchRequest.of(b -> b
.withJson(queryJson, client._jsonpMapper()) //<1>
.withJson(aggJson, client._jsonpMapper()) //<2>
.ignoreUnavailable(true) //<3>
);
Map<String, Aggregate> aggs2 = client
.search(agg2, Void.class)
.aggregations();
//end::query-and-agg
}
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.
Addressed in 78e8ee2
protected abstract B self(); | ||
|
||
@Override | ||
public B withJson(JsonParser parser, JsonpMapper mapper) { |
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.
Would it be possible to have a default json mapper that we can use as well?
So the method would be:
public B withJson(JsonParser parser)
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.
Addressed in 78e8ee2
So I pretty much only looked at First, can one input overwrite another? The samples only show things along side. What happens in Second, still thinking about the wording of Third, what happens on invalid JSON (or duplicate keys)? Early error when trying to parse JSON or is a wrong request sent to Elasticsearch? The The samples would be a great candidate for text blocks, but I suppose, we need to retain Java BWC here :-) |
@spinscale answers below
That's right. You can also overwrite properties loaded from JSON programatically. Key point is that it overwrites existing values and doesn't merge them (this would involve complex rules, both to implement and understand). I will add a section about it in the docs to make it clear and unambiguous.
Ok, so we keep
This goes through the object's deserializer ("partial deserialization" as said in the
[Edited] Indeed, perfect use case for Java 15 text blocks. But doc samples are extracted from test classes and this code base targets Java8... |
Regarding your third point: I need to think if this is a good idea or not, as this method offers a nice way out of the problem to provide java client extensions for custom plugin functionality. I'd start with your approach and check for feedback? Apart from that all you said makes sense! LGTM |
Custom plugins that add new API endpoints and/or new component types for queries, aggregations, etc are a different topic that will be addressed specifically. Typically for components we should have a |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-7.17 7.17
# Navigate to the new working tree
cd .worktrees/backport-7.17
# Create a new branch
git switch --create backport-200-to-7.17
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick --mainline 1 5a2bb06b661adac122b523a84b52d80768446897
# Push it to GitHub
git push --set-upstream origin backport-200-to-7.17
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-7.17 Then, create a pull request where the |
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-8.1 8.1
# Navigate to the new working tree
cd .worktrees/backport-8.1
# Create a new branch
git switch --create backport-200-to-8.1
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick --mainline 1 5a2bb06b661adac122b523a84b52d80768446897
# Push it to GitHub
git push --set-upstream origin backport-200-to-8.1
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-8.1 Then, create a pull request where the |
Can we get a new release with this enhancement? (perhaps 8.1.1 or 8.2.0) I'm new to elasticsearch, so I was reading the official documentation for 8.1 and saw |
@jsnyder4 it is part of 8.1.1 that was released on March 18th. |
Yeah, I see that now. Thank you very much. I'm using it now and it works great. Very helpful. |
hi,swallez
|
Fixes #143
Adds
withJson
methods to object builders. These methods populate an object builder from arbitrary JSON by using its deserializer. This allows creating API objects with a combination of programmatic call to property setters and arbitrary JSON data.The main use cases are loading search requests, index mappings or documents from arbitrary JSON sources.