Elasticsearch

目录

 

一、Elastichsearch基本函数操作

        1.1 ClientInterface接口方法参数

        1.2 插入并且更新数据

        1.3 删除数据

        1.4 获取索引表的结构

        1.5 单文档操作

        1.6 批量文档操作

        2.1 集群状态函数(HTTP指令)

二、Elastichsearch聚合查询操作

 


一、Elastichsearch函数操作

1.1 ClientInterface接口方法参数

除了通过ESId和ESParentId这两个注解来指定文档id和parentid,ClientInterface接口中还提供了一组方法来提供docid和parentid两个参数来指定文档id和parentid。

  • 单文档添加/修改-直接指定文档id和parentid的值
public abstract String addDocumentWithId(String indexName, String indexType, Object bean,Object docId) throws ElasticSearchException;


public abstract String addDocumentWithId(String indexName, String indexType, Object bean,Object docId,Object parentId) throws ElasticSearchException;


public abstract String addDocument(String indexName, String indexType, Object bean,Object docId,Object parentId,String refreshOption) throws ElasticSearchException;


public abstract String addDocument(String indexName, String indexType, Object bean,Object docId,String refreshOption) throws ElasticSearchException;

public String addDocumentWithParentId(String indexName, String indexType, Object bean,Object parentId) throws ElasticSearchException;

public String addDocumentWithParentId(String indexName, String indexType, Object bean,Object parentId,String refreshOption) throws ElasticSearchException;

public String addDateDocumentWithParentId(String indexName, String indexType, Object bean,Object parentId) throws ElasticSearchException;

public String addDateDocumentWithParentId(String indexName, String indexType, Object bean,Object parentId,String refreshOption) throws ElasticSearchException;
  • 批量文档添加和修改-指定文档id和parentId对应的对象字段名称
/**
     * 指定对象集合的文档id字段
     */
    /**
     * 批量创建索引,根据时间格式建立新的索引表
     * @param indexName
     * @param indexType
     * @param beans
     * @param docIdField 对象中作为文档id的Field
     * @return
     * @throws ElasticSearchException
     */
    public abstract String addDateDocumentsWithIdOptions(String indexName, String indexType, List<Object> beans,String docIdField,String refreshOption) throws ElasticSearchException;
    /**
     * 批量创建索引,根据时间格式建立新的索引表
     * @param indexName
     * @param indexType
     * @param beans
     * @param docIdField 对象中作为文档id的字段名称
     * @return
     * @throws ElasticSearchException
     */
    public abstract String addDateDocumentsWithIdField(String indexName, String indexType, List<Object> beans,String docIdField) throws ElasticSearchException;
    public abstract String addDocumentsWithIdField(String indexName, String indexType, List<Object> beans,String docIdField,String refreshOption) throws ElasticSearchException;
    public abstract String addDocumentsWithIdField(String indexName, String indexType,  List<Object> beans,String docIdField) throws ElasticSearchException;


    /**********************/
    /**
     * 批量创建索引,根据时间格式建立新的索引表
     * @param indexName
     * @param indexType
     * @param beans
     * @param docIdField 对象中作为文档id的字段名称
     * @return
     * @throws ElasticSearchException
     */
    public abstract String addDateDocumentsWithIdField(String indexName, String indexType, List<Object> beans,String docIdField,String parentIdField,String refreshOption) throws ElasticSearchException;
    /**
     * 批量创建索引,根据时间格式建立新的索引表
     * @param indexName
     * @param indexType
     * @param beans
     * @param docIdField 对象中作为文档id的字段名称
     * @return
     * @throws ElasticSearchException
     */
    public abstract String addDateDocumentsWithIdField(String indexName, String indexType, List<Object> beans,String docIdField,String parentIdField) throws ElasticSearchException;
    public abstract String addDocumentsWithIdField(String indexName, String indexType, List<Object> beans,String docIdField,String parentIdField,String refreshOption) throws ElasticSearchException;
    public abstract String addDocumentsWithIdParentField(String indexName, String indexType,  List<Object> beans,String docIdField,String parentIdField) throws ElasticSearchException;

 

1.2 插入并且更新数据

public static void testDataInsertAndUpdate(){
        //连接ES服务
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        
        Demo demo = new Demo();//创建demo实例对象
        //对其属性进行赋值
        demo.demoId = 1L;
        demo.contentbody = "13423423";
        demo.agentStarttime  = new Date();
        demo.applicationName = "newBPP";
        demo.name = "name";
        
        String result =  clientUtil.addDocument("demo",//索引名称
                "demo",//索引类型
                demo );//索引数据对象
        System.out.println(result);
    }

 

1.3 删除数据

public static void deleteData(){
        //获取ES链接
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        按照索引的ID进行删除
        String delete = clientUtil.deleteDocument("demo",//索引名称
                "demo",//索引类型
                "1" );
        System.out.println(delete);
    }

 

1.4 获取索引表的结构

 public static void testGetmapping(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        String date = format.format(new Date());
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        System.out.println(clientUtil.getIndexMapping("demo*"));
        //clientUtil.dropIndice("demo"+date);//此行代码需要注释掉否则不能正常获取mapping
    }

1.5 单文档操作

public static void testAddDateDocument() throws ParseException{
        //testGetmapping();
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");//获取日期格式
        String date = format.format(new Date());//获取新的日期
        //获取xml文件的配置需要将esmapper设置为source资源
        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/ww.xml");
        //创建一个新的demo
        Demo demo = new Demo();
        demo.demoId = 5l;
        demo.agentStarttime = new Date();
        demo.applicationName = "blackcatdemo";
        demo.contentbody = "this is content body";
        //根据dsl脚本创建索引文档,将文档保存到当天的索引表中demo-2018.02.03
        String response = clientUtil.addDateDocument("demo",//索引表,自动添加日期信息到索引表名称中
                "Demo",//索引类型
                "createDemoDocument",//创建文档对应的脚本名称,在esmapper/ww.xml中配置
                demo);

        System.out.println("addDateDocument-------------------------");
        System.out.println(response);
        //根据文档id获取索引文档,返回json格式
        response = clientUtil.getDocument("demo-"+date,//索引表,手动指定日期信息
                "Demo",//索引类型
                "5");
        System.out.println("getDocument-------------------------");
        System.out.println(response);
        //根据文档id获取索引文档,返回Demo对象,此处索引对象需要的参数索引类型需要与上面的一致否则报错
        demo = clientUtil.getDocument("demo-"+date,//索引表
                "Demo",//索引类型
                "5",//索引文档ID
                Demo.class);
    }
//格式化时间
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main{
    public static void main(String[] args){
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        System.out.println(sdf.format(date));
    }
}
上述代码的运行结果
2019-09-16 23:07

代码运行结果:

1.6 批量操作(代码有疑问)

public static void testBulkAddDateDocument() throws ParseException{
        //testGetmapping();
        //获取日期格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        //获取时间日期
        String date = format.format(new Date());
        //获取ES服务
        ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/ww.xml");
        //创建一个demo形式的列表集合并把每一个demo放入其中
        List<Demo> demos = new ArrayList<>();
        Demo demo = new Demo();
        demo.demoId = 2L;
        demo.agentStarttime = new Date();
        demo.applicationName = "blackcatdemo2";
        demo.contentbody = "this is content body2";
        demos.add(demo);

        demo = new Demo();
        demo.demoId = 3L;
        demo.agentStarttime = new Date();
        demo.applicationName = "blackcatdemo3";
        demo.contentbody = "this is content body3";
        demos.add(demo);

        //批量添加索引文档
        String response = clientUtil.addDateDocuments("demo",//索引表
                "demo",//索引类型
                "createDemoDocument",//创建文档对应的脚本名称,在esmapper/ww.xml中配置
                demos);

        System.out.println("addDateDocument-------------------------");
        System.out.println(response);
        //getDocument文档参数需要特别注意
        response = clientUtil.getDocument("demo-"+date,//索引表
                "demo",//索引类型
                "2L");
        System.out.println("getDocument-------------------------");
        System.out.println(response);

        demo = clientUtil.getDocument("demo-"+date,//索引表
                "demo",//索引类型
                "3L",//索引文档ID
                Demo.class);
    }

疑问:未成功添加进入ID,并且检索返回的文档失败

运行结果:

2.1返回集群ES健康的状态(HTTP指令)

//返回集群ES的健康状态
    public static void clusterHeathCheck(){
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        //返回json格式健康状态
        String heath = clientUtil.executeHttp("_cluster/health?pretty",ClientInterface.HTTP_GET);
        System.out.println(heath);
    }

代码结果:

public void clusterState(){
        ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
        //返回json格式集群状态
        String state = clientUtil.executeHttp("_cluster/state?pretty",ClientInterface.HTTP_GET);
        System.out.println(state);

    }

代码运行结果:

二、Elasticsearch聚合查询操作

  • 需要先定义XML的代码(可以在POSTMAN 上进行执行与验证)
  • 在Java中调用函数执行XML脚本
ESDatas<Traces> data //ESDatas为查询结果集对象,封装了返回的当前查询的List<Traces>结果集、符合条件的总记录数totalSize、以及聚合查询的结果
            = clientUtil.searchList"trace-*/_search",//查询操作,查询indices trace-*中符合条件的数据
                                "queryServiceByCondition",//通过名称引用配置文件中的query dsl语句
                                traceExtraCriteria,//查询条件封装对象
                                Traces.class);//指定返回的po对象类型,po对象中的属性与indices表中的文档filed名称保持一致


public void testDirectDslQuery(){
		String queryAll = "{\"query\": {\"match_all\": {}}}";
		ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
		ESDatas<Demo> esDatas =clientUtil.searchList("demo/_search",//demo为索引表,_search为检索操作action
				queryAll,//queryAll变量对应的dsl语句
				Demo.class);
		//获取结果对象列表
		List<Demo> demos = esDatas.getDatas();

		//获取总记录数
		long totalSize = esDatas.getTotalSize();
		System.out.println(totalSize);
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值