Elasticsearch Simple example of Java additions and deletions

Source: Internet
Author: User
Tags syslog

Since the development of the article, mainly in code-based, auxiliary some instructions. All of the content is the code that should actually be validated.

Introduction of header Files:

?
12345678910111213141516171819202122 importstaticorg.elasticsearch.node.NodeBuilder.nodeBuilder;importjava.io.IOException;importjava.net.InetAddress;importjava.util.Date;import java.util.Map;importjava.util.Set;importorg.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;importorg.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;importorg.elasticsearch.action.admin.indices.create.CreateIndexResponse;importorg.elasticsearch.action.index.IndexResponse;import org.elasticsearch.client.Client;importorg.elasticsearch.client.ClusterAdminClient;importorg.elasticsearch.client.transport.TransportClient;importorg.elasticsearch.cluster.health.ClusterIndexHealth;importorg.elasticsearch.common.settings.Settings;importorg.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.common.xcontent.XContentBuilder;importorg.elasticsearch.common.xcontent.XContentFactory;importorg.elasticsearch.node.Node;importstaticorg.elasticsearch.common.xcontent.XContentFactory.*;
Create an index?
123456789101112131415161718192021222324252627282930 XContentBuilder mapping = XContentFactory.jsonBuilder()    .startObject()        .startObject("settings")          .field("number_of_shards"1)//设置分片数量          .field("number_of_replicas"0)//设置副本数量        .endObject()    .endObject()    .startObject()        .startObject(type)//type名称            .startObject("properties"//下面是设置文档列属性。               .startObject("type").field("type""string").field("store""yes").endObject()               .startObject("eventCount").field("type""long").field("store""yes").endObject()               .startObject("eventDate").field("type""date").field("format""dateOptionalTime").field("store""yes").endObject()               .startObject("message").field("type""string").field("index""not_analyzed").field("store""yes").endObject()            .endObject()        .endObject()    .endObject();                       CreateIndexRequestBuilder cirb = client        .admin()        .indices()        .prepareCreate(indexName)//index名称        .setSource(mapping);CreateIndexResponse response = cirb.execute().actionGet();if(response.isAcknowledged()) {    System.out.println("Index created.");else{    System.err.println("Index creation failed.");}
Add a document?
123456789101112 IndexResponse response = client        .prepareIndex(indexName, type, "1")        .setSource(//这里可以直接用json字符串                jsonBuilder().startObject()                    .field("type""syslog")                    .field("eventCount"1)                    .field("eventDate"newDate())                    .field("message""secilog insert doc test")                .endObject()).get();System.out.println("index:"+response.getIndex()        +" insert doc id:"+response.getId()        +" result:"+response.isCreated());
Querying documents?
123456 GetResponse response = client.prepareGet("secilog""log""1").get();String source = response.getSource().toString();longversion = response.getVersion();String indexName = response.getIndex();String type = response.getType();String id = response.getId();

This article by Saikesaisi Pharmaceutical Lande (Secisland) original, reprint please indicate the author and source.

Modifying a document

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

The first type of code

?
123456789 UpdateRequest updateRequest = newUpdateRequest();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:

?
123456789101112131415 IndexRequest indexRequest = newIndexRequest(indexName, type, "3").source(jsonBuilder()    .startObject()        .field("type""syslog")        .field("eventCount"2)        .field("eventDate"newDate())        .field("message""secilog insert doc test")    .endObject());UpdateRequest updateRequest = newUpdateRequest(indexName, type, "3")    .doc(jsonBuilder()        .startObject()            .field("type""file")        .endObject())    .upsert(indexRequest);              client.update(updateRequest).get();
Delete a document?
12 DeleteResponse dresponse = client.prepareDelete("secilog""log""4").get();booleanisFound = dresponse.isFound(); //文档存在返回true,不存在返回false;
Delete index?
12 DeleteIndexRequest delete = newDeleteIndexRequest("secilog");client.admin().indices().delete(delete);

Lande (Secisland) will gradually analyze the features of the latest version of Elasticsearch, and look forward to it. Also welcome to join the Secisland public number for attention.

Elasticsearch Simple example of Java additions and deletions

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.