elasticsearch java 增删改查 版本1

既然是开发篇,主要以代码为主,辅助一些说明。所有的内容都是代码实际应该验证过的。

引入的头文件:

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.*;


创建索引

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.");
}


增加文档

IndexResponse response = client
        .prepareIndex(indexName, type, "1")
        .setSource(//这里可以直接用json字符串
                jsonBuilder().startObject()
                    .field("type", "syslog")
                    .field("eventCount", 1)
                    .field("eventDate", new Date())
                    .field("message", "secilog insert doc test")
                .endObject()).get();
System.out.println("index:"+response.getIndex()
        +" insert doc id:"+response.getId()
        +" result:"+response.isCreated());


查询文档

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();


修改文档

修改文档有两种方式,一种是直接修改,另一种是如果文档不存在则插入存在则修改。

第一种代码

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();


第二种代码:

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();


删除文档

DeleteResponse dresponse = client.prepareDelete("secilog", "log", "4").get();
boolean isFound = dresponse.isFound(); //文档存在返回true,不存在返回false;


删除索引

DeleteIndexRequest delete = new DeleteIndexRequest("secilog");
client.admin().indices().delete(delete);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值