mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-04-14 11:19:57 +00:00
This commit reorganizes the docs to make Java API docs looking more like the REST docs. Also, with 2.0.0, FilterBuilders don't exist anymore but only QueryBuilders. Also, all docs api move now to docs/java-api/docs dir as for REST doc. Remove removed queries/filters ----- * Remove Constant Score Query with filter * Remove Fuzzy Like This (Field) Query (flt and flt_field) * Remove FilterBuilders Move filters to queries ----- * Move And Filter to And Query * Move Bool Filter to Bool Query * Move Exists Filter to Exists Query * Move Geo Bounding Box Filter to Geo Bounding Box Query * Move Geo Distance Filter to Geo Distance Query * Move Geo Distance Range Filter to Geo Distance Range Query * Move Geo Polygon Filter to Geo Polygon Query * Move Geo Shape Filter to Geo Shape Query * Move Has Child Filter by Has Child Query * Move Has Parent Filter by Has Parent Query * Move Ids Filter by Ids Query * Move Limit Filter to Limit Query * Move MatchAll Filter to MatchAll Query * Move Missing Filter to Missing Query * Move Nested Filter to Nested Query * Move Not Filter to Not Query * Move Or Filter to Or Query * Move Range Filter to Range Query * Move Ids Filter to Ids Query * Move Term Filter to Term Query * Move Terms Filter to Terms Query * Move Type Filter to Type Query Add missing queries ----- * Add Common Terms Query * Add Filtered Query * Add Function Score Query * Add Geohash Cell Query * Add Regexp Query * Add Script Query * Add Simple Query String Query * Add Span Containing Query * Add Span Multi Term Query * Add Span Within Query Reorganize the documentation ----- * Organize by full text queries * Organize by term level queries * Organize by compound queries * Organize by joining queries * Organize by geo queries * Organize by specialized queries * Organize by span queries * Move Boosting Query * Move DisMax Query * Move Fuzzy Query * Move Indices Query * Move Match Query * Move Mlt Query * Move Multi Match Query * Move Prefix Query * Move Query String Query * Move Span First Query * Move Span Near Query * Move Span Not Query * Move Span Or Query * Move Span Term Query * Move Template Query * Move Wildcard Query Add some missing pages ---- * Add multi get API * Add indexed-scripts link Also closes #7826 Related to https://github.com/elastic/elasticsearch/pull/11477#issuecomment-114745934
119 lines
3.7 KiB
Plaintext
119 lines
3.7 KiB
Plaintext
[[java-docs-update]]
|
|
=== Update API
|
|
|
|
|
|
You can either create an `UpdateRequest` and send it to the client:
|
|
|
|
[source,java]
|
|
--------------------------------------------------
|
|
UpdateRequest updateRequest = new UpdateRequest();
|
|
updateRequest.index("index");
|
|
updateRequest.type("type");
|
|
updateRequest.id("1");
|
|
updateRequest.doc(jsonBuilder()
|
|
.startObject()
|
|
.field("gender", "male")
|
|
.endObject());
|
|
client.update(updateRequest).get();
|
|
--------------------------------------------------
|
|
|
|
Or you can use `prepareUpdate()` method:
|
|
|
|
[source,java]
|
|
--------------------------------------------------
|
|
client.prepareUpdate("ttl", "doc", "1")
|
|
.setScript(new Script("ctx._source.gender = \"male\"" <1> , ScriptService.ScriptType.INLINE, null, null))
|
|
.get();
|
|
|
|
client.prepareUpdate("ttl", "doc", "1")
|
|
.setDoc(jsonBuilder() <2>
|
|
.startObject()
|
|
.field("gender", "male")
|
|
.endObject())
|
|
.get();
|
|
--------------------------------------------------
|
|
<1> Your script. It could also be a locally stored script name.
|
|
In that case, you'll need to use `ScriptService.ScriptType.FILE`
|
|
<2> Document which will be merged to the existing one.
|
|
|
|
Note that you can't provide both `script` and `doc`.
|
|
|
|
[[java-docs-update-api-script]]
|
|
==== Update by script
|
|
|
|
The update API allows to update a document based on a script provided:
|
|
|
|
[source,java]
|
|
--------------------------------------------------
|
|
UpdateRequest updateRequest = new UpdateRequest("ttl", "doc", "1")
|
|
.script(new Script("ctx._source.gender = \"male\""));
|
|
client.update(updateRequest).get();
|
|
--------------------------------------------------
|
|
|
|
|
|
[[java-docs-update-api-merge-docs]]
|
|
==== Update by merging documents
|
|
|
|
The update API also support passing a partial document, which will be merged into the existing document (simple
|
|
recursive merge, inner merging of objects, replacing core "keys/values" and arrays). For example:
|
|
|
|
[source,java]
|
|
--------------------------------------------------
|
|
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
|
|
.doc(jsonBuilder()
|
|
.startObject()
|
|
.field("gender", "male")
|
|
.endObject());
|
|
client.update(updateRequest).get();
|
|
--------------------------------------------------
|
|
|
|
|
|
[[java-docs-update-api-upsert]]
|
|
==== Upsert
|
|
|
|
There is also support for `upsert`. If the document already exists, the content of the `upsert`
|
|
element will be used to index the fresh doc:
|
|
|
|
[source,java]
|
|
--------------------------------------------------
|
|
IndexRequest indexRequest = new IndexRequest("index", "type", "1")
|
|
.source(jsonBuilder()
|
|
.startObject()
|
|
.field("name", "Joe Smith")
|
|
.field("gender", "male")
|
|
.endObject());
|
|
UpdateRequest updateRequest = new UpdateRequest("index", "type", "1")
|
|
.doc(jsonBuilder()
|
|
.startObject()
|
|
.field("gender", "male")
|
|
.endObject())
|
|
.upsert(indexRequest); <1>
|
|
client.update(updateRequest).get();
|
|
--------------------------------------------------
|
|
<1> If the document does not exist, the one in `indexRequest` will be added
|
|
|
|
If the document `index/type/1` already exists, we will have after this operation a document like:
|
|
|
|
[source,js]
|
|
--------------------------------------------------
|
|
{
|
|
"name" : "Joe Dalton",
|
|
"gender": "male" <1>
|
|
}
|
|
--------------------------------------------------
|
|
<1> This field is added by the update request
|
|
|
|
If it does not exist, we will have a new document:
|
|
|
|
[source,js]
|
|
--------------------------------------------------
|
|
{
|
|
"name" : "Joe Smith",
|
|
"gender": "male"
|
|
}
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|