第07讲:Java操作之ElasticSearch高级查询

Posted 大坏蛋^_^

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第07讲:Java操作之ElasticSearch高级查询相关的知识,希望对你有一定的参考价值。

实验1、查询所有索引数据

package demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
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.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class Demo07 

    public static void main(String[] args) throws IOException 
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");

        //构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //查询所有数据
        sourceBuilder.query(QueryBuilders.matchAllQuery());

        //获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //查询匹配
        SearchHits hits = response.getHits();
        System.out.println("查询花费时间,单位毫秒:"+response.getTook());
        System.out.println("是否超时:"+response.isTimedOut());
        System.out.println("总数:"+hits.getTotalHits());
        System.out.println("匹配度分值:"+hits.getMaxScore());
        System.out.println("hits===============================>>");
        for(SearchHit hit : hits)
            //输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        
        System.out.println("<<===============================");
        //关闭客户端连接
        client.close();
    

运行结果:

实验2、term查询,查询条件为关键字

package demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class Demo08 

    public static void main(String[] args) throws IOException 
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );
        
        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");
        
        //构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("age", 30));
        request.source(sourceBuilder);
        
        //获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //查询匹配
        SearchHits hits = response.getHits();
        System.out.println("查询花费时间,单位毫秒:"+response.getTook());
        System.out.println("是否超时:"+response.isTimedOut());
        System.out.println("总数:"+hits.getTotalHits());
        System.out.println("匹配分度值:"+hits.getMaxScore());
        System.out.println("hits=================>>");
        for(SearchHit hit: hits)
            //输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        

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

运行结果:

实验3、分页查询

package demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

public class Demo09 

    public static void main(String[] args) throws IOException 
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");

        //构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery());

        //分页查询
        //当前页起始索引
        sourceBuilder.from(0);
        //每页显示的条数
        sourceBuilder.size(2);

        request.source(sourceBuilder);

        //获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //查询匹配
        SearchHits hits = response.getHits();
        System.out.println("查询花费时间,单位毫秒:"+response.getTook());
        System.out.println("是否超时:"+response.isTimedOut());
        System.out.println("总数:"+hits.getTotalHits());
        System.out.println("匹配分度值:"+hits.getMaxScore());
        System.out.println("hits=================>>");
        for(SearchHit hit: hits)
            //输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        

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

运行结果:

实验4、数据排序

package demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

public class Demo10 

    public static void main(String[] args) throws IOException 
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");

        //构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery());

        //排序
        sourceBuilder.sort("age", SortOrder.ASC);
        request.source(sourceBuilder);

        //获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //查询匹配
        SearchHits hits = response.getHits();
        System.out.println("查询花费时间,单位毫秒:"+response.getTook());
        System.out.println("是否超时:"+response.isTimedOut());
        System.out.println("总数:"+hits.getTotalHits());
        System.out.println("匹配分度值:"+hits.getMaxScore());
        System.out.println("hits=================>>");
        for(SearchHit hit: hits)
            //输出每条查询的结果信息
            System.out.println(hit.getSourceAsString());
        

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

运行结果:

实验5、过滤字段

package demo;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

public class Demo11 

    public static void main(String[] args) throws IOException 
        //创建客户端对象
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        //创建搜索请求对象
        SearchRequest request = new SearchRequest();
        request.indices("student");

        //构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery());

        //查询字段过滤
        String[] excludes = ;
        String[] includes = "name", "age";
        sourceBuilder.fetchSource(includes, excludes);

        request.source(sourceBuilder);

        //获取响应对象
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //查询匹配
        SearchHits hits = response.getHits();
        System.out.println("查询花费时间,单位毫秒:"+response.getTook());
        System.out.<

以上是关于第07讲:Java操作之ElasticSearch高级查询的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch:Java操作ElasticSearch索引之CRUD

Elasticsearch之JAVA API操作

ElasticSearch第二天

java 操作elasticsearch之搭建测试项目环境

Elasticsearch连续剧之实战篇Java操作es

第006讲:Python之常用操作符