首页 > 编程语言 >Java操作ElasticSearch(三、查询)

Java操作ElasticSearch(三、查询)

时间:2022-12-08 22:35:33浏览次数:49  
标签:Java searchSourceBuilder searchHits SearchRequest SearchSourceBuilder request 查询

查询的步骤如下

  1. 创建 SearchSourceBuilder 对象
  2. 添加查询条件 QueryBuilders
  3. 添加排序、分页等其他条件
  4. 创建 SearchRequest 对象,并指定索引库名称
  5. 发起请求,得到结果
  6. 获取总条数
  7. 获取 SearchHits 数组,并遍历
  8. 获取其中的 _source,是 JSON 数据
  9. 把 _source 反序列化为 User 对象

查询所有 match_all

@Test
public void test011() throws IOException {
    // 创建请求对象,用于描述本次请求语义
    SearchRequest request = new SearchRequest("user"); // 创建查询语义对象
    // 设置查询类型
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    // 添加SearchSourceBuilder对象到SearchRequest对象中
    request.source(searchSourceBuilder);
    // 发送请求给es
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    // 处理响应结果
    SearchHits searchHits = response.getHits();
    // 总条数
    System.out.println("命中的总条数: " + searchHits.getTotalHits().value); // 12
    // 返回数据数组
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        String sourceAsString = hit.getSourceAsString();
        User user = JSON.parseObject(sourceAsString, User.class);
        System.out.println(user);
    }
}

 

 

分词查询 match

@Test
public void test12() throws IOException {
    SearchRequest request = new SearchRequest("user");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchQuery("note", "唱歌表演"));
    request.source(searchSourceBuilder);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    SearchHits searchHits = response.getHits();
    System.out.println("命中数:" + searchHits.getTotalHits().value);
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        String sourceAsString = hit.getSourceAsString();
        User user = JSON.parseObject(sourceAsString, User.class);
        System.out.println(user);
    }
}

 

 

词条查询 term

@Test
public void test14() throws IOException {
    SearchRequest request = new SearchRequest("user");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.termQuery("gender", "女"));
    request.source(searchSourceBuilder);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    SearchHits searchHits = response.getHits();
    System.out.println("命中数:" + searchHits.getTotalHits());
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        String sourceAsString = hit.getSourceAsString();
        User user = JSON.parseObject(sourceAsString, User.class);
        System.out.println(user);
    }
}

 

 

范围查询 range

@Test
public void test15() throws IOException {
    SearchRequest request = new SearchRequest("user");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age");
    // 查询年龄大于20岁 小于25岁的同学
    queryBuilder.gt(20).lt(25);
    searchSourceBuilder.query(queryBuilder);
    request.source(searchSourceBuilder);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    SearchHits searchHits = response.getHits();
    System.out.println("命中数:" + searchHits.getTotalHits());
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        String sourceAsString = hit.getSourceAsString();
        User user = JSON.parseObject(sourceAsString, User.class);
        System.out.println(user);
    }
}

 

 

模糊查询 fuzzy

@Test
public void test16() throws IOException {
    SearchRequest request = new SearchRequest("user");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.fuzzyQuery("note", "三"));
    request.source(searchSourceBuilder);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    SearchHits searchHits = response.getHits();
    System.out.println("命中数:" + searchHits.getTotalHits());
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        String sourceAsString = hit.getSourceAsString();
        User user = JSON.parseObject(sourceAsString, User.class);
        System.out.println(user);
    }
}

 

 

 

布尔查询 bool

@Test
public void test13() throws IOException {
    SearchRequest request = new SearchRequest("user");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    // 添加条件
    queryBuilder.must(QueryBuilders.matchQuery("note", "Java"));
    queryBuilder.mustNot(QueryBuilders.termQuery("gender", "女"));
    searchSourceBuilder.query(queryBuilder);
    request.source(searchSourceBuilder);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    SearchHits searchHits = response.getHits();
    System.out.println("命中数:" + searchHits.getTotalHits());
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit hit : hits) {
        String sourceAsString = hit.getSourceAsString();
        User user = JSON.parseObject(sourceAsString, User.class);
        System.out.println(user);
    }
}

 

 

  

 

标签:Java,searchSourceBuilder,searchHits,SearchRequest,SearchSourceBuilder,request,查询
From: https://www.cnblogs.com/xulinjun/p/16967581.html

相关文章

  • 快速学会慢查询SQL排查
    转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.top/c56bd0c5.html你好,我是测试蔡坨坨。在往期文章中,我们聊过数据库基础知识,可参考「数据库基础,看完这篇就够了!」......
  • JavaDoc
    JavaDocJavaDoc是用来生成自己的API文档的参数信息@author作者名@version版本号@since指明需要最早使用的jdk版本@param参数名@return返回值情况@throws异......
  • Java包机制
    包机制为了更好的组织类,Java提供了包机制,用于区别类名的命名空间packagepkg1[.pkg2[.pkg3...]];一般利用公司域名倒置作为包名为了能够使用某一个包的成员,我们......
  • java中的递归机制
    本文主要讲述java中的递归机制。示例1,递归代码如下:publicclassRecursion01{publicstaticvoidmain(String[]args){Tt=newT();t.test......
  • Elasticsearch学习小结
    本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/286什么是es?Elasticsearch是由Apache开源的一个兼有搜索引擎和NoSQL数......
  • Laravel 查询包括软删除的记录
    本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/287###1.查询结果包括已被软删除的记录:Model::withTrashed()->get();#......
  • Java中的查找树和哈希表
    1、TreeSet(红黑树实现)底层使用TreeMap实现,储存结构为红黑树。构造器:publicTreeSet(){this((NavigableMap)(newTreeMap()));}add操作,把数据作为......
  • Javascript的事件
    JavaScript程序采用了异步事件驱动编程模型。在这种程序设计风格下,当文档、浏览器、元素或与之相关的对象发生某些有趣的事情时,Web浏览器就会产生事件(event)。例如,当Web......
  • BKPF会计凭证查询-ALV表
    *&---------------------------------------------------------------------**&ReportZ14*&*&-----------------------------------------------------------------......
  • SQLSERVER日期查询(年、月、日、季、周、时、分、秒)
     常用日期查询操作SELECTGETDATE()[当前日期],DATENAME(YEAR,GETDATE())[年],DATENAME(MONTH,GETDATE())[月],DATENAME(DAY,GETD......