Elasticsearch Java additions and deletions to check version 1

Source: Internet
Author: User
Tags syslog

Since it is a development article, mainly in code-oriented, auxiliary some instructions. All the content is actually validated by the code.

The header file introduced:

Import static Org.elasticsearch.node.NodeBuilder.nodeBuilder;
Import java.io.IOException;
Import java.net.InetAddress;
Import Java.util.Date;
Import Java.util.Map;
 
Import Java.util.Set;
Import Org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
Import Org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
Import Org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
Import Org.elasticsearch.action.index.IndexResponse;
Import org.elasticsearch.client.Client;
Import org.elasticsearch.client.ClusterAdminClient;
Import org.elasticsearch.client.transport.TransportClient;
Import Org.elasticsearch.cluster.health.ClusterIndexHealth;
Import org.elasticsearch.common.settings.Settings;
Import org.elasticsearch.common.transport.InetSocketTransportAddress;
Import Org.elasticsearch.common.xcontent.XContentBuilder;
Import Org.elasticsearch.common.xcontent.XContentFactory;
Import Org.elasticsearch.node.Node; Import Static Org.elasticsearch.common.xconTent. xcontentfactory.*;


Create an index

Xcontentbuilder mapping = Xcontentfactory.jsonbuilder (). StartObject (). StartObject ("Settings"). Fie
    LD ("Number_of_shards", 1)//sets the number of fragments. Field ("Number_of_replicas", 0)/Set the number of replicas. EndObject () EndObject ()
               . StartObject (). StartObject (type)//type name. StartObject ("Properties")//The following is setting document column properties. . StartObject ("type"). Field ("Type", "string"). Field ("Store", "yes"). EndObject (). StartObject ("EventCount"). Field ("Type", "Long"). Field ("Store", "yes"). EndObject (). StartObject ("Eventdate"). Field ("Type", "date"). Fie LD ("format", "Dateoptionaltime"). Field ("Store", "yes"). EndObject (). StartObject ("message"). Field ("Type", "s
    Tring "). Field (" Index "," not_analyzed "). Field (" Store "," yes "). EndObject (). EndObject (). EndObject ()
                        
. EndObject (); Createindexrequestbuilder CIRB = client. Admin (). Indices (). Preparecreate (Indexname)//index name. SetSource (mapping);
Createindexresponse response = Cirb.execute (). Actionget ();  if (response.isacknowledged ()) {System.out.println ("index created.");} else {System.err.println ("Index creation Failed. ");}


Add Document

Indexresponse response = client
        . Prepareindex (IndexName, type, "1")
        . SetSource (//Here you can use the JSON string
                directly Jsonbuilder (). StartObject (). Field ("
                    type", "Syslog")
                    . Field ("EventCount", 1)
                    . Field ("Eventdate", new Date ()).
                    Field ("Message", "Secilog insert Doc test")
                . EndObject ())
. System.out.println ("Index:" +response.getindex ()
        + "Insert Doc ID:" +response.getid ()
        + "Result:" + Response.iscreated ());


Querying documents

GetResponse response = Client.prepareget ("Secilog", "Log", "1"). get ();
String Source = Response.getsource (). toString ();
Long Version = Response.getversion ();
String IndexName = Response.getindex ();
String type = Response.gettype ();
String id = response.getid ();


Modify Document

There are two ways to modify a document, one is to modify it directly, and the other is to modify it if the document does not exist.

First kind of code

Updaterequest updaterequest = new Updaterequest ();
Updaterequest.index (indexname);
Updaterequest.type (type);
Updaterequest.id ("1");
Updaterequest.doc (Jsonbuilder ()
        . StartObject ().
            field ("Type", "File")
        . EndObject ());
Client.update (updaterequest). get ();


The second type of code:

Indexrequest indexrequest = new Indexrequest (IndexName, type, "3")
. Source (Jsonbuilder ()
    . StartObject ()
        . Field ("Type", "Syslog")
        . Field ("EventCount", 2)
        . Field ("Eventdate", New Date ())
        . Field ("Message", " Secilog Insert Doc Test ")
    . EndObject ());
Updaterequest updaterequest = new Updaterequest (IndexName, type, "3")
    . Doc (Jsonbuilder ()
        . StartObject ()
            . Field ("Type", "File")
        . EndObject ())
    . Upsert (indexrequest);              
Client.update (updaterequest). get ();


Delete Document

Deleteresponse dresponse = Client.preparedelete ("Secilog", "Log", "4"). get ();
Boolean isfound = Dresponse.isfound (); The document exists to return true and does not exist to return false;


Delete Index

Deleteindexrequest Delete = new Deleteindexrequest ("Secilog");
Client.admin (). Indices (). Delete (delete);


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.