2 - 基于ELK的ElasticSearch 7.8.x技术整理 - java操作篇 - 更新完毕
3、java操作ES篇
3.1、摸索java链接ES的流程
- 自行创建一个maven项目
3.1.1、依赖管理
点击查看代码
    <properties>
        <ES-version>7.8.0</ES-version>
        <log4j-version>1.2.17</log4j-version>
        <junit-version>4.13.2</junit-version>
        <jackson-version>2.13.0</jackson-version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <!-- 注意:这里的版本问题,要和下载的window的ES版本一致,甚至后续用linux搭建也是一样的
                          到时用linux时,ES、kibana的版本都有这样的限定
                -->
                <version>${ES-version}</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <!-- 注意:这里别搞成了elasticsearch-client
                    这个东西在7.x已经不推荐使用了,而到了8.0之后,这个elasticsearch-client已经完全被废弃了
                 -->
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <!-- 同样的,注意版本问题 -->
                <version>${ES-version}</version>
            </dependency>
			<!-- 这个我其实没用,要用的话,可以导入:log4j-api和log4j-core -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j-version}</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit-version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson-version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
3.1.2、摸索链接流程
3.1.2.1、获取父项目中的依赖
点击查看代码
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
3.1.2.2、摸索流程
点击查看代码
package cn.zixieqing;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName ConnectionTest
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class ConnectionTest {
    /*
     * @Author ZiXieQing
     * @Description // TODO 测试java链接ES
     * @Date  2021/12/14
     * @Param
     * @return
     */
    // 下面这个逻辑,对照shiro中的realm、manager、FilterFactoryBean的逻辑来看( 没用过的就当我没说^_^ )
    @Test
    public void test() throws IOException {
        // 3、创建HttpHost
        HttpHost host = new HttpHost("127.0.0.1", 9200);// 发现需要:String hostname, int port  这就很简单了涩
      // 当然:这个方法重载中有一个参数scheme  这个是:访问方式 根据需求用http / https都可以  这里想传的话用:http就可以了
        // 2、创建RestClientBuilder 但是:点击源码发现 - 没有构造方法
        // 既然没有,那肯定提供得有和xml版的mybatis加载完xml文件之后的builder之类的,找一下
        RestClientBuilder clientBuilder = RestClient.builder(host);
        // 发现1、有重载;2、重载之中有几个参数,而HttpHost... hosts 这个参数貌似贴近我们想要的东西了,所以建一个HttpHost
        // 1、要链接client,那肯定需要一个client咯,正好:导入得有high-level-client
        RestHighLevelClient esClient = new RestHighLevelClient(clientBuilder);   // 发现需要RestClientBuilder,那就建
        // 4、测试:只要 esClient. 就可以看到一些很熟悉的方法,可以在这里测试调一下哪些方法,然后去postman中获取数据看一下对不对
        // 这里不多做说明:java链接ES客户端的流程就是上面这样的,不过:这和MySQL数据库链接一样,记得不用了就关闭
        esClient.close();       // 当然:封装之后,这个关闭操作应该放出来,然后在封装的工具中只需要返回这个链接对象即可
    }
}


3.2、java中操作ES索引
3.2.1、向父项目获取自己要的依赖
点击查看代码
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
3.2.2、封装链接对象
点击查看代码
package cn.zixieqing.utile;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
 * @ClassName ESClientUtil
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class ESClientUtil {
    private static final String HOST = "127.0.0.1";     // 用localhost也行,不过后面用linux就要ip,所以:算在这里养成习惯吧
    private static final Integer PORT = 9200;
    public static RestHighLevelClient getESClient() {
        return new RestHighLevelClient( RestClient.builder( new HttpHost( HOST, PORT ) ) );
    }
}

3.2.3、创建索引
点击查看代码
package cn.zixieqing;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName CreateIndex
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class CreateIndex {
    @Test
    public void createIndexTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 创建索引
        // CreateIndexRequest()  第一个参数:要创建的索引名    第二个参数:请求选项  默认即可
        CreateIndexResponse response = esClient.indices().create(
                new CreateIndexRequest("person"), RequestOptions.DEFAULT );
        // 查看是否添加成功  核心方法:isAcknowledged()
        System.out.println( response.isAcknowledged() );
        esClient.close();
    }
}

用postman检验一下:

3.2.4、查询索引
点击查看代码
package cn.zixieqing.index;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName SearchIndex
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class SearchIndex {
    /*
     * @Author ZiXieQing
     * @Description // TODO 查询索引
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void searchIndexTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 获取索引
        GetIndexResponse response = esClient.indices().get(
                new GetIndexRequest("person"), RequestOptions.DEFAULT );
        // 熟悉GetIndexResponse中的几个api
        System.out.println( "Aliases" + response.getAliases() );
        System.out.println( "Mappings" + response.getMappings() );
        System.out.println( "Settings" + response.getSettings() );      // 这三者在用postman玩的时候,返回结果中都有
        esClient.close();
    }
}


3.2.5、删除索引
点击查看代码
package cn.zixieqing.index;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName DeleteIndex
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class DeleteIndex {
    /*
     * @Author ZiXieQing
     * @Description // TODO 删除索引
     * @Date  2021/12/14
     * @Param
     * @return
     */
    @Test
    public void deleteIndexTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 删除索引
        AcknowledgedResponse response = esClient.indices().delete(
                new DeleteIndexRequest("person"), RequestOptions.DEFAULT );
        // 检验一下:是否删除成功
        System.out.println( response.isAcknowledged() );
        esClient.close();
    }
}

用postman再检测一下:

3.3、java操作ES中的_doc - 重点中的重点
3.3.1、创建doc
- 这里还需要jackson-databind:前面已经导入 
- 同时:为了偷懒,所以把lombok也一起导入了 
- 父项目依赖管理 
点击查看代码
        <lombok-version>1.18.22</lombok-version>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok-version}</version>
        </dependency>
子项目获取依赖:
点击查看代码
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
编写实体类
点击查看代码
package cn.zixieqing.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
 * @ClassName UserEntity
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain = true)        // lombok的链式调用
public class UserEntity implements Serializable {
    private String id;
    private String name;
    private String sex;
}
测试:
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.entity.UserEntity;
import cn.zixieqing.utile.ESClientUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName InsertDoc
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class InsertDoc {
    /*
     * @Author ZiXieQing
     * @Description // TODO 新增文档_doc,当然下面这个过程利用ESClient封装的思路也可以抽离
     * @Date  2021/12/14
     * @Param
     * @return
     */
    @Test
    public void insertDocTest() throws IOException {
        // 1、获取链接
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        IndexRequest request = new IndexRequest();
        // 选择索引及设置唯一标识
        request.index("user").id("10002");
        // 2、添加数据
        // IndexRequest和IndexResponse这中间就是:做添加doc操作
        UserEntity userEntity = new UserEntity();
        userEntity.setId( "100" ).setName( "紫邪情" ).setSex( "女" );     // lombok链式调用
        // 转json      注:objectMapper是jackson-databind中的,不是ES中的
        String userJson = new ObjectMapper().writeValueAsString( userEntity );
        // 把转成的json字符串存到ES中去
        request.source( userJson , XContentType.JSON);
        // 3、发起请求 获取响应对象
        IndexResponse response = esClient.index( request, RequestOptions.DEFAULT );
        // 看看这个IndexResponse有哪些有用的api
        System.out.println( "响应转态:" + response.getResult() );   // 其他的一点 就可以看到了,都是字面意思
        esClient.close();
    }
}

**postman检测一下:

我的测试结构如下:

3.3.2、修改doc
- 这个修改是指的局部修改,全量修改就不用想了
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName DeleteDoc
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class UpdateDoc {
    /*
     * @Author ZiXieQing
     * @Description // TODO 删除doc
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void deleteDoc() throws IOException {
        // 1、获取链接对象
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        UpdateRequest request = new UpdateRequest();
        // 获取索引
        request.index("user").id("10002");
        // 2、修改doc数据
        request.doc(XContentType.JSON, "name", "邪公子");
        // 3、发起请求、获得响应对象
        UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
        System.out.println( "响应状态为:" + response.getResult() );
        esClient.close();
    }
}

postman检验一下:

3.3.3、查询doc
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName GetDoc
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class GetDoc {
    @Test
    public void getDocTest() throws IOException {
        // 1、获取链接对象
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        GetRequest request = new GetRequest();
        request.index("user").id("10002");
        // 2、发起请求、获取响应对象
        GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
        // 3、获取结果  推荐用getSourceAsString()
        String result = response.getSourceAsString();
        System.out.println( "获得的doc为:" + result );
        esClient.close();
    }
}

3.3.4、删除doc
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName DeleteDoc
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class DeleteDoc {
    /*
     * @Author ZiXieQing
     * @Description // TODO 删除doc
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void deleteDocTest() throws IOException {
        // 1、获取链接对象
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        DeleteRequest request = new DeleteRequest();
        // 获取索引
        request.index("user").id("10002");
        // 2、做删除操作
        DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
        System.out.println( "响应状态为:" + response.getResult() );
        // 3、关闭链接
        esClient.close();
    }
}

再次获取检验一下:

3.4.5、批量新增_doc数据
- 本质:把请求封装了而已,从而让这个请求可以传递各种类型参数,如:删除的、修改的、新增的,这样就可以搭配for循环
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName BatchDeleteDoc
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class BatchInsertDoc {
    /*
     * @Author ZiXieQing
     * @Description // TODO 批量添加doc数据
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void batchInsertDocTest() throws IOException {
        // 1、获取链接对象
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        BulkRequest request = new BulkRequest();
        // 当然:source的第二个参数都是传个对象,这里为了偷懒,嫖了别人的代码
        request.add( new IndexRequest()
                .index("user")
                .id("520")
                .source( XContentType.JSON, "name", "小紫1") );
        request.add( new IndexRequest()
                .index("user")
                .id("521")
                .source( XContentType.JSON, "name", "小紫2") );
        request.add( new IndexRequest()
                .index("user")
                .id("522")
                .source( XContentType.JSON, "name", "小紫3") );
        // 2、发送请求
        BulkResponse response = esClient.bulk( request, RequestOptions.DEFAULT );
        // 查看执行时间
        System.out.println( response.getTook() );
        esClient.close();
    }
}

postman检验一下:
点击查看代码
http://127.0.0.1:9200/user/_search		请求方式 get
// 返回结果
{
    "took": 585,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "520",
                "_score": 1.0,
                "_source": {
                    "name": "小紫1"
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "521",
                "_score": 1.0,
                "_source": {
                    "name": "小紫2"
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "522",
                "_score": 1.0,
                "_source": {
                    "name": "小紫3"
                }
            }
        ]
    }
}
3.4.6、批量删除_doc数据
- 本质:把请求封装了而已,从而让这个请求可以传递各种类型参数,如:删除的、修改的、新增的,这样就可以搭配for循环
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName BarchDeleteDoc
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class BatchDeleteDoc {
    /*
     * @Author ZiXieQing
     * @Description // TODO 批量删除doc数据
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void batchDeleteDoc() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        BulkRequest request = new BulkRequest();
        // 和批量添加相比,变的地方就在这里而已
        request.add(new DeleteRequest().index("user").id("520"));
        request.add(new DeleteRequest().index("user").id("521"));
        request.add(new DeleteRequest().index("user").id("522"));
        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook());
        esClient.close();
    }
}

postman检验一下:

3.4.7、高级查询 - 重点
3.4.7.1、全量查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName MatchAll
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class MatchAll {
    /*
     * @Author ZiXieQing
     * @Description // TODO 高级查询 - 全量查询  就是基础语法中在请求体内使用match_all那个知识点
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void matchAllTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 全量查询 match_all
        SearchResponse response = esClient.search(  new SearchRequest()
                                                        .indices("user")
                                                        .source(
                                                            new SearchSourceBuilder()
                                                                .query( QueryBuilders.matchAllQuery() )
                                                        ), RequestOptions.DEFAULT );
        // 查看执行了多少时间
        System.out.println( response.getTook() );
        // 把数据遍历出来看一下
        for ( SearchHit data : response.getHits() ) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
        // 上面的看不懂,那就看下面拆分的过程
//        // 1、获取链接对象
//        RestHighLevelClient esClient = ESClientUtil.getESClient();
//
//        // 3、创建SearchRequest对象
//        SearchRequest request = new SearchRequest();
//        request.indices("user");
//
//        // 5、创建SearchSourceBuilder对象
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//
//        // 6、进行查询  发现:需要QueryBuilders对象,看源码发现:没有构造,可是:有matchAllQuery()
//        searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
//
//        // 4、调用source()方法获取数据,但是发现:需要SearchSourceBuilder,继续创建
//        request.source( searchSourceBuilder );
//
//        // 2、发送请求  发现:需要SearchRequest  那就建一个
//        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
//        // 7、获取数据
//        for (SearchHit data : response.getHits()) {
//            System.out.println( data.getSourceAsString() );
//        }
//
//        // 8、关闭链接
//        esClient.close();
    }
}

3.4.7.2、条件查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName TermQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class TermQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO term条件查询  注意:这里不是说的基础篇中的 filter range 那个条件啊( 这个条件要求的是查询字段为int类型的 )
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void termQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 条件查询
        SearchResponse response = esClient.search( new SearchRequest()
                                                    .indices("user")
                                                    .source( new SearchSourceBuilder()
                                                             .query( QueryBuilders.termQuery("_id", "520" ) ) ),  // 对照全量查询:变的就是这里的方法调用
                                                    RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
}

3.4.7.3、分页查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName LimitQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class LimitQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 分页查询    对应基础篇中的from size
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void limitQueryTest() throws IOException {
        // 1、获取链接对象
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 3、创建SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 4、查询出所有的数据
        SearchSourceBuilder sourceBuilder = searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
        // 5、对数据进行分页操作
        sourceBuilder.from(0);
        sourceBuilder.size(2);
        // 2、发送请求
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .indices("user")
                                                        .source( searchSourceBuilder )
                                                    , RequestOptions.DEFAULT );
        // 7、查看数据
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        // 8、关闭链接
        esClient.close();
    }
}

3.4.7.4、排序查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName SortQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class SortQuery {
    @Test
    public void sortQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchSourceBuilder sourceBuilder = searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
        // 排序  以什么字段排序、排序方式是什么( 注意:别犯低级错误啊,用字符串来搞排序 )
        sourceBuilder.sort("_id", SortOrder.DESC);
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .indices("user")
                                                        .source( searchSourceBuilder ),
                                                   RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
}

3.4.7.5、条件过滤查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName FilterQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class FilterQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 查询过滤
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void filterQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 查询结果只需要什么?
        String[] includes = { "name" };
        // 查询结果不需要什么?
        String[] excludes = {};     // 根据需求自行填充
        searchSourceBuilder.fetchSource( includes,excludes );
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .indices("user")
                                                        .source( searchSourceBuilder )
                                                    , RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
}
我的数据没弄好,我建的doc中只有一个name,而老衲又懒得加了,所以:这里别让结果把自己搞混了

3.4.7.6、组合查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName UnionQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class UnionQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 组合查询至must查询
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void mustQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 注意:这里产生了改变,是调用的boolQuery()
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // 必须包含什么?
        boolQuery.must( QueryBuilders.matchQuery("author", "邪") );
        boolQuery.must( QueryBuilders.matchQuery("sex", "girl") );
        // 当然:也就有mustNot()不包含什么了
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .source( new SearchSourceBuilder()
                                                                    .query( boolQuery ) )
                                                    , RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
    /*
     * @Author ZiXieQing
     * @Description // TODO 组合查询之should查询
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void shouldQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        // 懒得烧蛇吃  不写了,知道这个should和must一样,复制粘贴多个条件即可
        boolQuery.should( QueryBuilders.matchQuery("title", "是") );
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .source( new SearchSourceBuilder()
                                                                    .query(boolQuery) )
                                                    , RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
}
must查询的结果:

should查询的结果

3.4.7.7、范围查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName RangeQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class RangeQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 范围查询  即:基础篇中的filter  range
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void rangeQuery() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("id");
        // 对结果进行处理  gt >  lt <   gte >=     lte  <=
        rangeQuery.gt("10000");
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .source( new SearchSourceBuilder()
                                                                    .query( rangeQuery ) )
                                                    , RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
}

3.4.7.8、模糊查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName FuzzyQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class FuzzyQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 模糊查询
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void fuzzyQuery() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 模糊查询
        // fuzziness( Fuzziness.ONE ) 表示的是:字符误差数  取值有:zero、one、two、auto
        // 误差数  指的是:fuzzyQuery("author","网二")这里面匹配的字符的误差嘛
        //                  可以有几个字符不一样 / 多 / 少几个字符?
        FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("author","网二").fuzziness( Fuzziness.ONE );
        SearchResponse response = esClient.search( new SearchRequest()
                                                        .source( new SearchSourceBuilder()
                                                                        .query(fuzzyQuery) )
                                                    , RequestOptions.DEFAULT );
        for (SearchHit data : response.getHits()) {
            System.out.println( data.getSourceAsString() );
        }
        esClient.close();
    }
}

3.4.7.9、高亮查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName HighLightQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
public class HighLightQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 高亮查询 highLight
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void highLightQueryTest() throws IOException {
        // 1、获取链接对象
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        // 3、创建SearchSourceBuilder对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 4、查询什么数据?
        TermsQueryBuilder termsQuery = QueryBuilders.termsQuery("author", "小紫1");
        // 5、构建高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        // 高亮编写
        highlightBuilder.preTags("<span color='blue'>");    // 构建标签前缀
        highlightBuilder.postTags("</span>");       // 构建标签后缀
        highlightBuilder.field("author");     // 构建高亮字段
        // 6、设置高亮
        searchSourceBuilder.highlighter( highlightBuilder );
        // 7、进行查询
        searchSourceBuilder.query( termsQuery );
        // 2、发送请求、获取响应对象
        SearchResponse response = esClient.search( new SearchRequest().indices("user").source( searchSourceBuilder ) , RequestOptions.DEFAULT);
        // 验证
        System.out.println(response);
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.getSourceAsString());
            System.out.println( hit.getHighlightFields());
        }
        // 9、关闭链接
        esClient.close();
    }
}
3.4.7.10、聚合查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
 * @ClassName AggQuery
 * @Author ZiXieQing
 * @Date 2021/12/14
 * Version 1.0
 **/
// 聚合查询
public class AggQuery {
    /*
     * @Author ZiXieQing
     * @Description // TODO 最大值查询
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void maxQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // max("maxId")  这个名字是随便取的  不讲究,就是取个名字而已        联想:有max、就有min、avg、count、sum......
        //                                  注:方法变成term()就是分组了
        // field("_id")  对哪个字段求最大值
        searchSourceBuilder.aggregation( AggregationBuilders.max("maxId").field("id") );
        SearchResponse response = esClient.search(new SearchRequest().source(searchSourceBuilder), RequestOptions.DEFAULT);
        // 检验
        System.out.println(response);
        esClient.close();
    }
    /*
     * @Author ZiXieQing
     * @Description // TODO 分组查询
     * @Date  2021/12/14
     * @Param []
     * @return void
     */
    @Test
    public void groupQueryTest() throws IOException {
        RestHighLevelClient esClient = ESClientUtil.getESClient();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 分组
        searchSourceBuilder.aggregation( AggregationBuilders.terms("groupQuery").field("author") );
        SearchResponse response = esClient.search( new SearchRequest().source(searchSourceBuilder), RequestOptions.DEFAULT);
        System.out.println(response);
        esClient.close();
    }
}

4、进阶篇链接
2 - 基于ELK的ElasticSearch 7.8.x技术整理 - java操作篇 - 更新完毕的更多相关文章
- 1 - 基于ELK的ElasticSearch 7.8.x 技术整理 - 基础语法篇 - 更新完毕
		准备工作 0.什么是ElasticSearch?它和Lucene以及solr的关系是什么? 这些是自己的知识获取能力,自行百度百科 1.下载ElasticSearch的window版,linux版后续 ... 
- 3 - 基于ELK的ElasticSearch 7.8.x技术整理 - 高级篇( 偏理论 )
		4.ES高级篇 4.1.集群部署 集群的意思:就是将多个节点归为一体罢了( 这个整体就有一个指定的名字了 ) 4.1.1.window中部署集群 - 了解即可 把下载好的window版的ES中的dat ... 
- 4 - 基于ELK的ElasticSearch 7.8.x技术整理 - 高级篇( 续 ) - 更新完毕
		0.前言 这里面一些理论和前面的知识点挂钩的,所以:建议看一下另外3篇知识内容 基础篇:https://www.cnblogs.com/xiegongzi/p/15684307.html java操作 ... 
- 启动elk中elasticsearch服务报错which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)
		解决办法: vi /etc/sysconfig/elasticsearch JAVA_HOME=/usr/local/java sudo systemctl restart elasticsearch ... 
- 基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合测试
		前言开源实时日志分析ELK平台(ElasticSearch, Logstash, Kibana组成),能很方便的帮我们收集日志,进行集中化的管理,并且能很方便的进行日志的统计和检索,下面基于ELK的最 ... 
- 基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合
		前言开源实时日志分析ELK平台(ElasticSearch, Logstash, Kibana组成),能很方便的帮我们收集日志,进行集中化的管理,并且能很方便的进行日志的统计和检索,下面基于ELK的最 ... 
- 基于ELK的简单数据分析
		原文链接: http://www.open-open.com/lib/view/open1455673846058.html 环境 CentOS 6.5 64位 JDK 1.8.0_20 Elasti ... 
- 基于ELK进行邮箱访问日志的分析
		公司希望能够搭建自己的日志分析系统.现在基于ELK的技术分析日志的公司越来越多,在此也记录一下我利用ELK搭建的日志分析系统. 系统搭建 系统主要是基于elasticsearch+logstash+f ... 
- 从0搭建一个基于 ELK 的日志、指标收集与监控系统
		为了使得私有化部署的系统能更健壮,同时不增加额外的部署运维工作量,本文提出了一种基于 ELK 的开箱即用的日志和指标收集方案. 在当前的项目中,我们已经使用了 Elasticsearch 作为业务的数 ... 
随机推荐
- maven常用Java配置
			maven国内镜像 ------------------------------------------------------------------------------------------ ... 
- FastDFS分布式文件系统及源码解析
			记录一次本人学习FastDFS-分布式文件系统的学习过程,希望能帮助到有需要的人. 首选得对此技术有个大概的了解,可以参考 https://www.cnblogs.com/centos2017/p/7 ... 
- 前端两大框架 vue 和 react 的区别
			1. 设计思想 vue: vue的官网介绍说vue是一种渐进式框架,采用自底向上增量开发的设计: react: 采用函数式编程,推崇纯组件,数据不可变,单向数据流: 2. 编写语法 vue: 采用单文 ... 
- java通过反射获取Java对象属性值
			说明: 作为反射工具类,通过对象和属性的名字获取对象属性的值,如果在当前对象属性没有找到,依次向上收集所有父类的属 性,直到找到属性值,没有找到返回null: 代码: 1.classUtil pack ... 
- 最基础的SSM框架整合篇
			一.简单理解 Spring.Spring MVC和MyBatis的整合主要原理就是将我们在单独使用Spring MVC和MyBatis过程中需要自己实例化的类都交由Ioc容器来管理,过程分为两步: 第 ... 
- 阿里云发布CloudOps白皮书,ECS自动化运维套件新升级
			12月10 日,2021云上架构与运维峰会上,阿里云发布业界首部<云上自动化运维白皮书>(简称CloudOps白皮书),并在其中提出了CloudOps成熟度模型.同时,阿里云还宣布了ECS ... 
- ctypes与numpy.ctypeslib的使用
			numpy ctypeslib 与 ctypes接口使用说明 作者:elfin 目录 一.numpy.ctypeslib使用说明 1.1 准备好一个C++计算文件 1.2 ctypeslib主要的五个 ... 
- CF918B Radio Station 题解
			Content 有 \(n\) 个形如 \(a_i.b_i.c_i.d_i\) 的 IP 地址.有 \(m\) 条命令,每条命令由一条字符串 \(s\) 和一个形如 \(p.q.r.s\) 的 IP ... 
- grep的时候Binary file matches  **.log 怎么解决
			操作 grep "hello world" test.log 结果 Binary file test.log matches 原因:grep认为test.log是二进制文件 解决方 ... 
- java 多线程:Thread类常用方法:setPriority优先级、interrupt中断标记、suspend暂停与唤醒resume(已过时);daemon守护线程
			常用方法: boolean isAlive() 测试此线程是否存活. boolean isDaemon() 测试此线程是否为守护程序线程. static void sleep?(long millis ... 
