ElasticSearch简单入门案例

1、原生操作

使用 Xput创建索引

说明:

RESTful接口URL的格式:

http://localhost:9200/<index>/<type>/[<id>]

其中index、type是必须提供的。

id是可选的,不提供es会自动生成。

index、type将信息进行分层,利于管理。

index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。

curl -XPUT 'http://node-1:9200/twitter/doc/1?pretty' -H 'Content-Type: application/json' -d '

{

    "user": "kimchy",

    "post_date": "2009-11-15T13:12:00",

    "message": "Trying out Elasticsearch, so far so good?"

}'

 

curl -XPUT 'http://node-1:9200/twitter/doc/2?pretty' -H 'Content-Type: application/json' -d '

{

    "user": "kimchy",

    "post_date": "2009-11-15T13:12:00",

    "message": "Trying out Elasticsearch, so far so good?"

}'

 

curl -XPUT 'http://node-1:9200/twitter/doc/3?pretty' -H 'Content-Type: application/json' -d '

{

    "user": "kimchy",

    "post_date": "2009-11-15T13:12:00",

    "message": "Trying out Elasticsearch, so far so good?"

}'

 

在插入的过程中index会自动创建,一个Doc由_index、_type、_id唯一指定(如果不指定ID,则会自动生成)。另外,在插入的过程中可以通过?version=、?timestamp=、?ttl=指定一些参数。

 

查询索引

curl -XGET 'http://node-1:9200/twitter/doc/1?pretty=true'

curl -XGET 'http://node-1:9200/twitter/doc/2?pretty=true'

curl -XGET 'http://node-1:9200/twitter/doc/3?pretty=true'

 

查询字段数据

通过关键字进行查询例如:q=user:kimchy

 

curl -XGET 'http://node-1:9200/twitter/_search?q=user:kimchy&pretty=true'

 

 

curl -XGET 'http://node-1:9200/twitter/_search?pretty=true' -H 'Content-Type: application/json' -d '

{

    "query" : {

        "match" : { "user": "kimchy" }

    }

}'

 

 

通过JSON的方式查询,查询的时候指定区间

curl -XGET 'http://node-1:9200/twitter/_search?pretty=true' -H 'Content-Type: application/json' -d '

{

    "query" : {

        "range" : {

            "post_date" : { "from" : "2009-11-15T13:00:00", "to" : "2009-11-15T14:00:00" }

        }

    }

}'

 

查询所有的索引库

curl -XGET ‘node-1:9200/_cat/indices?v&pretty’

 

更新索引库

执行PUT操作,如果已经存在,就相当于更新操作:

curl -XPUT 'http://node1:9200/twitter/doc/3?pretty' -H 'Content-Type: application/json' -d '

{

    "user": "lisi",

    "post_date": "2009-11-15T13:12:00",

    "message": "Trying out Elasticsearch, so far so good?"

}'

 

删除索引库

curl -XDELETE 'http://node1:9200/twitter'

 

 

2、javaAPI操作

Pom文件

<dependencies>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>6.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

CreateIndex

package com.hadoop.seven.elk;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CreateIndex {
    private TransportClient client;
    private IndexResponse response;

    @Before
    public void init() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-es").build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("node-1"), 9300));
    }

    @Test
    /**
     * 使用json串创建索引
     */
    
public void index1() throws UnknownHostException {
        String json = "{" +
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
                "}";
        response = client.prepareIndex("twitter", "doc", "100")
                .setSource(json, XContentType.JSON)
                .get();
    }

    @Test
    /**
     * 使用map对象创建索引
     */
    
public void index2() throws UnknownHostException {
        Map<String, Object> json = new HashMap<String, Object>();
        json.put("user", "kimchy");
        json.put("postDate", new Date());
        json.put("message", "trying out Elasticsearch");

        response = client.prepareIndex("twitter", "doc", "200").setSource(json).get();
    }

    @Test
    /**
     * 使用jsonBuilder创建索引
     */
    
public void index3() throws IOException {
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
                .field("user", "kimchy")
                .field("postDate", new Date())
                .field("message", "trying out Elasticsearch")
                .endObject();
        response = client.prepareIndex("twitter", "doc", "300")
                .setSource(builder).get();
    }

    @After
    public void close() throws UnknownHostException {
        if (response != null) {
            String index = response.getIndex();
            String type = response.getType();
            String id = response.getId();
            long version = response.getVersion();
            RestStatus status = response.status();
            System.out.println("index " + index + "  type" + type + "" + id + "" + version + "" + version);
            System.out.println(status);
            client.close();
        }
    }

    /**
     * 批量插入数据
     *
     *
@throws IOException
     */
    
@Test
    public void index4() throws IOException {
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        bulkRequest.add(client.prepareIndex("twitter", "doc", "400")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                        .endObject()
                )
        );
        bulkRequest.add(client.prepareIndex("twitter", "doc", "500")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "another post")
                        .endObject()
                )
        );
        //执行插入操作
        BulkResponse bulkResponse = bulkRequest.get();
        if (bulkResponse.hasFailures()) {
            System.out.println("-------------------我失败了-----------------");
        }
    }
}

QueryIndex

package com.hadoop.seven.elk;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class QueryIndex {
    private TransportClient client;
    private GetResponse response;

    @Before
    public void init() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-es").build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("node-1"), 9300));
    }
    @Test
    public void get1() throws UnknownHostException {
        response = client.prepareGet("twitter", "doc", "1").get();
    }

    @Test
    public void get2() throws UnknownHostException {
        MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
                .add("twitter", "doc", "100")
                .add("twitter", "doc", "200", "300", "400")
                .get();
        for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
            GetResponse response = itemResponse.getResponse();
            if (response.isExists()) {
                String json = response.getSourceAsString();
                System.out.println(json);
            }
        }
    }

    @After
    public void close() throws UnknownHostException {
        if (response != null) {
            String index = response.getIndex();
            String type = response.getType();
            String id = response.getId();
            long version = response.getVersion();
            System.out.println("index " + index + "  type" + type + "" + id + "" + version + "" + version);
            client.close();
        }
    }
}

DeleteIndex

package com.hadoop.seven.elk;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class DeleteIndex {
    private TransportClient client;
    private DeleteResponse response;

    @Before
    public void init() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "my-es").build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("node-1"), 9300));
    }

    @After
    public void close() throws UnknownHostException {
        if(response!=null) {
            String index = response.getIndex();
            String type = response.getType();
            String id = response.getId();
            long version = response.getVersion();
            System.out.println("index " + index + "  type" + type + "" + id + "" + version + "" + version);
            RestStatus status = response.status();
            System.out.println("status:" + status.getStatus());
            client.close();
        }
    }

    /**
     * 根据文档进行删除
     *
@throws UnknownHostException
     */
    
@Test
    public void delete1() throws UnknownHostException {
        response = client.prepareDelete("twitter", "doc", "100").get();
    }
    @Test
    /**
     * 根据根据查询结果删除数据,并触发相关事件
     */
    
public void delete2() throws UnknownHostException {
        DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
                .filter(QueryBuilders.matchQuery("gender", "male"))
                .source("twitter")
                .execute(new ActionListener<BulkByScrollResponse>() {
                    public void onResponse(BulkByScrollResponse response) {
                        long deleted = response.getDeleted();
                        System.out.println("---------------"+deleted);
                    }
                    public void onFailure(Exception e) {
                        System.out.println("------------错误了");
                    }
                });
    }
}

 

发布了38 篇原创文章 · 获赞 12 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览