Elasticsearch的简单测试应用

这里写图片描述

这里写图片描述
客户端打开。

代码如下:

User

package com.zhiyou.model;

public class User {
    private Long id;
    private String username;
    private int age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}

ElasticTest

package com.zhiyou.test;

import com.alibaba.fastjson.JSON;
import com.zhiyou.model.User;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;

public class ElasticTest {
    @Test
    public void test01() throws  Exception{
        // 启动客户端连接
        TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        // 关闭客户端连接
        client.close();

    }

    // 添加索引
    @Test
    public void test02() throws Exception{
        TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        User user=new User();
        user.setId(3L);
        user.setAge(77);
        user.setUsername("王五");

        IndexResponse response=client.prepareIndex("user","userinfo",user.getId()+"")
                .setSource(JSON.toJSONString(user), XContentType.JSON)
                .get();

        System.out.println(response.toString());
        //IndexResponse[index=user,type=userinfo,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]
        client.close();
    }

    //获取索引
    @Test
    public void test03() throws Exception{
        TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        GetResponse response = client.prepareGet("user", "userinfo", "1").execute().actionGet();

        String json=response.getSourceAsString();
        System.out.println(json);
        //{"age":30,"id":1,"username":"张三"}
        User user = JSON.parseObject(json, User.class);
        System.out.println(user);
        //User{id=1, username='张三', age=30}

        client.close();

    }

    //删除索引
    @Test
    public void test04() throws Exception{
        TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        DeleteResponse response = client.prepareDelete("user", "userinfo", "1").execute().actionGet();
        String result=response.getResult().name();
        System.out.println(result);
        //DELETED
        client.close();
    }

    //根据条件搜索
    @Test
    public void test05() throws Exception{
        TransportClient client=new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));

        SearchResponse response=client.prepareSearch("user")
                .setTypes("userinfo")
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                //.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18))     // Filter
                .setFrom(0).setSize(20).setExplain(true)//这个分页,默认10条
                .get();
        System.out.println(response.getHits().getAt(1).getSourceAsString());
        //{"age":30,"id":1,"username":"张三"}
        // 数据不是根据id的顺序保存
        client.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值