es 的常用搜索用法

Posted 赖赖monkey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es 的常用搜索用法相关的知识,希望对你有一定的参考价值。

先导依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    
    <exclusions> //这里是忽略依赖冲突
        <exclusion>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-smile</artifactId>
        </exclusion>
    </exclusions>
</dependency>

@SpringBootTest(classes = HotelDemoApplication.class)
public class SearchTest 
    @Resource
    private IHotelService iHotelService;

    private RestHighLevelClient client;

    @BeforeEach
    void before() throws IOException 
        //1、连接的Es数据库地址
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://124.222.107.250:9200")
        ));
    

    @AfterEach
    void after() throws IOException 
        //关闭连接
        client.close();
    

    //查询所有
    @Test
    void matchAll() throws IOException 
        //1、准备request 要查的库 hotel
        SearchRequest request = new SearchRequest("hotel");
        //查询的条件都放这个对象里
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询的条件
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //searchSourceBuilder用来装查询条件的对象
        request.source(searchSourceBuilder);
        //2、发送请求、获得结果
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        //解析数据
        SearchHit[] hits = searchResponse.getHits().getHits();  //根据表结构来
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString(); //将对象转成json字符串
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);  //将字符串转成对象
            System.out.println(hotelDoc);
        
    

    //分词查询
    @Test
    void matchQuery() throws IOException 
        //1、准备request 要查的库 hotel
        SearchRequest request = new SearchRequest("hotel");
        //查询的条件都放这个对象里
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询的条件
        searchSourceBuilder.query(QueryBuilders.matchQuery("name","7天上海"));
        //searchSourceBuilder用来装查询条件的对象
        request.source(searchSourceBuilder);
        //2、发送请求、获得结果
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        //解析数据
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
            System.out.println(hotelDoc);
        
    

    //精确查询
    @Test
    void termQuery() throws IOException 
        //1、准备request  要查的库 hotel
        SearchRequest request = new SearchRequest("hotel");
        //查询的条件都放这个对象里
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询的条件
        searchSourceBuilder.query(QueryBuilders.termQuery("city","上海"));
        //searchSourceBuilder用来装查询条件的对象
        request.source(searchSourceBuilder);
        //2、发送请求、获得结果
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        //解析数据
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
            System.out.println(hotelDoc);
        
    

    //组合查询 boolQuery
    @Test
    void boolQuery() throws IOException 
        //1、准备request 要查的库 hotel
        SearchRequest request = new SearchRequest("hotel");
        //查询的条件都放这个对象里
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询的条件 多条件,放boolQuery
        BoolQueryBuilder boolQuery = new BoolQueryBuilder();
        //多query条件,第一个用must(匹配),后面用filter过滤  mustNot代表不匹配
        boolQuery.filter(QueryBuilders.termQuery("city","上海"));
        //rangeQuery是范围查询  lte()是小于  gte()是高于
        boolQuery.must(QueryBuilders.rangeQuery("price").lte(300));
        searchSourceBuilder.query(boolQuery);
        //searchSourceBuilder用来装查询条件的对象
        request.source(searchSourceBuilder);
        //2、发送请求、获得结果
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        //解析数据
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
            System.out.println(hotelDoc);
        
    


    //分页+排序
    @Test
    void sort() throws IOException 
        //1、准备request  要查的库 hotel
        SearchRequest request = new SearchRequest("hotel");
        //查询的条件都放这个对象里
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询的条件
        searchSourceBuilder.query(QueryBuilders.termQuery("city","上海"));
        searchSourceBuilder.sort("price", SortOrder.DESC);//根据字段排序
        searchSourceBuilder.size(30); //每页查多少
        searchSourceBuilder.from(0);  //从第几页开始查
        //searchSourceBuilder用来装查询条件的对象
        request.source(searchSourceBuilder);
        //2、发送请求、获得结果
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        //解析数据
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
            System.out.println(hotelDoc);
        
    

    //高亮显示
    @Test
    void highLight() throws IOException 
        //1、准备request 要查的库 hotel
        SearchRequest request = new SearchRequest("hotel");
        //查询的条件都放这个对象里
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //添加查询的条件
        searchSourceBuilder.query(QueryBuilders.matchQuery("name","7天上海"));
        //高亮对象
        HighlightBuilder highLightBuilder = new HighlightBuilder();
        //添加高亮参数
        highLightBuilder.field("name");                       //要高亮的字段名
        highLightBuilder.preTags("<em style='color:pink'>");  //添加前缀
        highLightBuilder.postTags("</em>");                   //添加后缀
        searchSourceBuilder.highlighter(highLightBuilder);
        //searchSourceBuilder用来装查询条件的对象
        request.source(searchSourceBuilder);
        //2、发送请求、获得结果
        SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
        //解析数据
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString();
            HotelDoc hotelDoc = JSON.parseObject(sourceAsString, HotelDoc.class);
            //获取高亮字段的数组
            HighlightField name = hit.getHighlightFields().get("name");
            //获取数组第一个元素的字符串
            String highName = name.fragments()[0].string();
            hotelDoc.setName(highName);
            System.out.println(hotelDoc);
        
    


以上是关于es 的常用搜索用法的主要内容,如果未能解决你的问题,请参考以下文章

Go常用包(二十五):ElasticSearch客户端的实现

ElasticSearch的常用方法

ES 高级用法

ES 高级用法

[翻译]ElasticSearch官方文档

ElasticSearch搜索引擎:常用的存储mapping配置项 与 doc_values详细介绍