Teng's blog Teng's blog
首页
Java
H5前端
GitHub (opens new window)
首页
Java
H5前端
GitHub (opens new window)
  • 认知

  • 入门

    • 环境准备
    • RESTful&JSON
    • 客户端工具
    • 倒排索引
    • 数据格式
    • RESTful-API

    • Java-API

      • 项目准备
      • 索引-创建
      • 索引-查询
      • 索引-删除
      • 文档-新增
      • 文档-修改
      • 文档-主键查询
      • 文档-删除
      • 文档-批量新增
      • 文档-批量删除
      • 文档-高级查询-全量
      • 文档-高级查询-关键字查询
        • SearchRequest
      • 文档-高级查询-分页
      • 文档-高级查询-排序
      • 文档-高级查询-指定返回字段
      • 文档-高级查询-Bool
      • 文档-高级查询-范围
      • 文档-高级查询-模糊
      • 文档-高亮
      • 文档-聚合查询-max
      • 文档-聚合查询-分组
  • 环境

  • 进阶

  • 框架集成

  • 优化

  • 面试题

  • Database-Elasticsearch
  • 入门
  • Java-API
Shetengteng
2022-01-29

文档-高级查询-关键字查询

# SearchRequest

创建 Test12_Doc_Query_Term

package com.stt.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 Test12_Doc_Query_Term {
    public static void main(String[] args) throws IOException {
        // 创建客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 构建查询请求
        SearchRequest request = new SearchRequest().indices("student");

        // 构建查询体
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.termQuery("age","30"));
        request.source(builder);

        SearchResponse response = client.search(request, RequestOptions.DEFAULT);

        // 查询后匹配
        System.out.println("took" + response.getTook());
        System.out.println("time out" + response.isTimedOut());

        SearchHits hits = response.getHits();

        System.out.println("total:" + hits.getTotalHits());
        System.out.println("max score:" + hits.getMaxScore());
        System.out.println("-----hits------");
        for (SearchHit hit : hits) {
            // 输出每条查询的记录
            System.out.println(hit.getSourceAsString());
        }

        // 关闭客户端
        client.close();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

返回

took32ms
time outfalse
total:2 hits
max score:1.0
-----hits------
{"name":"zhangsan","nickname":"zhangsan","sex":"男","age":30}
{"name":"zhangsan2","nickname":"zhangsan2","sex":"女","age":30}
1
2
3
4
5
6
7
Last Updated: 2022/02/05, 15:58:51
文档-高级查询-全量
文档-高级查询-分页

← 文档-高级查询-全量 文档-高级查询-分页→

Theme by Vdoing | Copyright © 2021-2022 Shetengteng | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式