Version background:
<Dependency><groupId>Org.elasticsearch.client</groupId><Artifactid>Transport</Artifactid><version>5.4.1</version></Dependency>
First get the connection of es:
// Set cluster name privatestaticfinal Settings Settings = Settings.builder (). Put ("Cluster.name", "my-application"). Build (); // Cluster Name // Create client Private Static Final Client New prebuilttransportclient (Settings) . addtransportaddress (New inetsockettransportaddress (Inetaddress.getbyname ("127.0.0.1"), 9300));
Create indexes and establish mappings
PublicString Create (map<string, object>map) { indexresponse Response=client. Prepareindex ("index", "type", "PRIMARY key ID"). SetSource (Xcontentfactory.jsonbuilder (). StartObject (). Fiel D ("Business Field 1", "Value of Business Field 1"). Field ("Business Field 2", "Value of Business field 2") . EndObject ()). Execute (). Actionget (); } return"Success"; }
Delete, based on primary key ID
Public String Deleteesinfo (map<string, object> Map) { =client.preparedelete ("index", "type", "PRIMARY key ID" ) . Execute () . Actionget (); Result=deleteresponse.getresult (). toString (); return result; }
But this is only based on the primary key delete, if the query to delete according to the condition? Previously, this delete by query function was removed in the 2.X version because the official thought it would cause some errors if you need to install the Delete-by-query plugin yourself. More than 5.0 versions, and select Return. to conditionally Delete
Boolquerybuilder QueryBuilder = querybuilders.boolquery (); Querybuilder.must (Querybuilders.termquery ("business Field 1", "Value of Business Field 1"); = DeleteByQueryAction.INSTANCE.newRequestBuilder (client) . Filter (QueryBuilder) . Source (" Index ") . get (); Long
Then, according to the primary key to update the ES information, which does not appear in the field to update, do not overwrite the original value.
Updateresponse response = ElasticsearchConfig.client.prepareUpdate ("index", "type", "PRIMARY Key ID") . Setdoc ( Xcontentfactory.jsonbuilder (). startobject () field ("business Field 1", "2") . Field ("Business field 2", "1" ) . EndObject ()). Execute () . Actionget ();
ElasticSearch Java api-5.x Delete and update index data