elasticsearch测试

package com.jt;

import java.net.InetAddress;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

public class EsTest {
    public static TransportClient getClient() throws Exception{
        //java TransportClient client连接es
        //Settings.EMPTY表示连接过程使用默认配置
        //例如集群名称默认是elasticsearch
        TransportClient client=new PreBuiltTransportClient(Settings.EMPTY).
                addTransportAddress(new InetSocketTransportAddress(
                        InetAddress.getByName("10.9.21.220"),9300));
        return client;
    }
   /*
    * 客户连接es,执行一个方法判断连接是否成功
    */
    @Test
    public void test01() throws Exception{
        TransportClient client=EsTest.getClient();
        //利用client的方法,获取一个索引jtdb tb_item 下id 为830972的doc
        //参数问题:
        //第一个参数:代表索引
        //第二个参数:代表类型,也就是数据库的表名称
        //第三个参数:代表在表中的id
        GetResponse response=client.prepareGet("jtdb_item","tb_item","536563").get();
        //从response,获取doc数据
        String result=response.getSourceAsString();
        System.out.println(result);
    }
    /*
     * 判断索引是否存在
     */
    @Test
    public void test02() throws Exception{
        TransportClient client=EsTest.getClient();
        //获取管理对象
        IndicesAdminClient indicesAdminClient=client.admin().indices();
        //获取索引的doc
        IndicesExistsResponse response1=indicesAdminClient.prepareExists("index01").get();
        IndicesExistsResponse response2=indicesAdminClient.prepareExists("jtdb_item").get();
        //判断索引是否存在
        System.out.println(response1.isExists());
        System.out.println(response2.isExists());
    }
    
    /*
     * 创建索引
     */
    @Test
    public void test03() throws Exception{
        TransportClient client=EsTest.getClient();
        //获取管理对象
        IndicesAdminClient indicesAdminClient=client.admin().indices();
        CreateIndexResponse response=indicesAdminClient.prepareCreate("index03").get();
        System.out.println(response);
    }
    
    /*
     * 删除索引
     */
    @Test
    public void test04() throws Exception{
        TransportClient client=EsTest.getClient();
        //获取管理对象
        IndicesAdminClient indicesAdminClient=client.admin().indices();
        DeleteIndexResponse response=indicesAdminClient.prepareDelete("index03").get();
        System.out.println(response);//可以不打印
    }
    /*
     * 新建文档
     * 文档是对象,底层http协议如何把对象转换为json字符串
     * 引入一个第三方工具jacksonJson:将对象转化为json
     */
    private static final ObjectMapper mapper=new ObjectMapper();
    @Test
    public void test05() throws Exception{
        Item item=new Item();
        item.setId(10L);
        item.setBarcode("sdfas");
        item.setTitle("三星大地利尔");
        item.setSellPoint("灭口神奇");
        String docStr=new ObjectMapper().writeValueAsString(item);
        //client调用添加方法将字符串通过http发送到es存储
        //如果不拿结果,最后的方法可以不加
        IndexResponse reponse=EsTest.getClient().prepareIndex("index01","item","1").
        setSource(docStr).execute().actionGet();
        System.out.println(reponse);
    }
    /*
     * machQuery查询数据
     */
    @Test
    public void test06() throws Exception{
        //连接对象
        TransportClient client=EsTest.getClient();
        //构造查询条件对象
        //org.elasticsearch.index.query
        QueryBuilder query=QueryBuilders.matchQuery("title","苹果").
                operator(Operator.AND);
        //将对象封装数据
        //获取返回结构,d打印部分字段
        //searche指向一个索引文件名
        SearchResponse reponse=client.prepareSearch("jtdb_item").setQuery(query).setSize(10).get();
        //reponse里包含着50条查询结果
        SearchHits hits=reponse.getHits();//查询条数
        for(SearchHit hit:hits){
            System.out.println("文档id"+hit.getId());
            System.out.println("商品id"+hit.getSource().get("id"));
            System.out.println("商品标题"+hit.getSource().get("title"));
            
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个常见的elasticsearch测试用例是创建索引和删除索引。在Spring Data Elasticsearch中,可以使用ElasticsearchRestTemplate来执行这些操作。在测试类中,可以通过注入ElasticsearchRestTemplate来使用它。例如,可以使用以下代码创建索引的测试用例: ``` @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESIndexTest { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Test public void createIndex(){ System.out.println("创建索引"); // 在这里执行创建索引的操作 } } ``` 同样地,可以使用以下代码来删除索引: ``` @RunWith(SpringRunner.class) @SpringBootTest public class SpringDataESIndexTest { @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate; @Test public void deleteIndex(){ boolean isSuccess = elasticsearchRestTemplate.deleteIndex(Product.class); System.out.println("删除成功与否 = " + isSuccess); // 在这里执行删除索引的操作 } } ``` 通过在测试用例中执行相应的操作,可以测试索引的创建和删除功能。这些测试用例可以帮助确保elasticsearch的功能正常运行,并且可以在开发过程中进行自动化测试。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ElasticSearch入门-测试用例](https://blog.csdn.net/weixin_42581660/article/details/118334952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值