JAVA API & Elasticsearch 7.10 基本查询 *** 作(一)

JAVA API & Elasticsearch 7.10 基本查询 *** 作(一),第1张

JAVA API & Elasticsearch 7.10 基本查询 *** 作(一)

 总结一下最近参与项目学到的知识,不知道自己会不会坚持写Blog(;´д`)

总之先从ES基本 *** 作开始了!


目录

AND查询

OR查询

范围查询


AND查询
List searchMustStudent(StudentQO studentQO) throws IOException;

public List searchMustStudent(StudentQO studentQO) throws IOException {

    //设置返回参数
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //SELECt name, gender, age
    sourceBuilder.fetchSource(new String[]{"name", "gender", "age"}, null);
    //FROM student
    SearchRequest searchRequest = new SearchRequest("student");

    //设置查询条件
    BoolQueryBuilder mustQueryBuilder = QueryBuilders.boolQuery();
    //WHERe id = studentQO.getId()
    mustQueryBuilder.must(QueryBuilders.termQuery("id", studentQO.getId()));
    //AND name != ""
    mustQueryBuilder.mustNot(QueryBuilders.termQuery("name", ""));

    //设置返回所有数据,否则将返回前一万条数据
    sourceBuilder.trackTotalHits(true);
    //添加查询条件
    sourceBuilder.query(mustQueryBuilder);
    //设置返回时间
    sourceBuilder.timeout(new Timevalue(1000));
    //设置返回结果
    searchRequest.source(sourceBuilder);

    //连接ES 开始搜索
    SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
    //获取结果
    SearchHits searchHits = response.getHits();
    SearchHit[] hits = searchHits.getHits();
    List studentVOS = new ArrayList<>();
    for (SearchHit hit: hits) {
        Map source = hit.getSourceAsMap();
        StudentVO studentVO = new StudentVO();
        studentVO.setName((String) source.get("name"));
        studentVO.setGender((String) source.get("gender"));
        studentVO.setAge((int) source.get("age"));
        studentVOS.add(studentVO);
    }

    //获取结果总数
    TotalHits totalHits = searchHits.getTotalHits();
    long value = totalHits.value;

    return studentVOS;
}

OR查询
List searchShouldStudent(List studentQO) throws IOException;

public List searchShouldStudent(List studentQO) throws IOException {

    //设置返回参数
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //SELECt name, gender, age
    sourceBuilder.fetchSource(new String[]{"name", "gender", "age"}, null);
    //FROM student
    SearchRequest searchRequest = new SearchRequest("student");

    //设置查询条件
    BoolQueryBuilder shouldQueryBuilder = QueryBuilders.boolQuery();
    //WHERe sid = studentQO.get(i).getId() OR .....
    for(int i = 0; i < studentQO.size(); i++){
        String id = studentQO.get(i).getId();
        shouldQueryBuilder.should(QueryBuilders.wildcardQuery("id", id));
    }

    //设置返回所有数据,否则将返回前一万条数据
    sourceBuilder.trackTotalHits(true);
    //添加查询条件
    sourceBuilder.query(shouldQueryBuilder);
    //设置返回时间
    sourceBuilder.timeout(new Timevalue(1000));
    //设置返回结果
    searchRequest.source(sourceBuilder);

    //连接ES 开始搜索
    SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
    //获取结果
    SearchHits searchHits = response.getHits();
    SearchHit[] hits = searchHits.getHits();
    List studentVOS = new ArrayList<>();
    for (SearchHit hit: hits) {
        Map source = hit.getSourceAsMap();
        StudentVO studentVO = new StudentVO();
        studentVO.setName((String) source.get("name"));
        studentVO.setGender((String) source.get("gender"));
        studentVO.setAge((int) source.get("age"));
        studentVOS.add(studentVO);
    }

    //获取结果总数
    TotalHits totalHits = searchHits.getTotalHits();
    long value = totalHits.value;

    return studentVOS;
}

如果要组合使用must和should,必须将should作为条件加入must中,否则should条件无法生效。


范围查询
List searchRangeStudent(int youngest, int eldest) throws IOException;

public List searchRangeStudent(int youngest, int eldest) throws IOException {

    //设置返回参数
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    //SELECt name, gender, age
    sourceBuilder.fetchSource(new String[]{"name", "gender", "age"}, null);
    //FROM student
    SearchRequest searchRequest = new SearchRequest("student");

    //设置查询条件
    BoolQueryBuilder mustQueryBuilder = QueryBuilders.boolQuery();
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
    //WHERe youngest <= age AND age <= eldest
    rangeQueryBuilder.gte(youngest);
    rangeQueryBuilder.lte(eldest);
    mustQueryBuilder.must(rangeQueryBuilder);

    //设置返回所有数据,否则将返回前一万条数据
    sourceBuilder.trackTotalHits(true);
    //添加查询条件
    sourceBuilder.query(mustQueryBuilder);
    //设置返回时间
    sourceBuilder.timeout(new Timevalue(1000));
    //设置返回结果
    searchRequest.source(sourceBuilder);

    //连接ES 开始搜索
    SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
    //获取结果
    SearchHits searchHits = response.getHits();
    SearchHit[] hits = searchHits.getHits();
    List studentVOS = new ArrayList<>();
    for (SearchHit hit: hits) {
        Map source = hit.getSourceAsMap();
        StudentVO studentVO = new StudentVO();
        studentVO.setName((String) source.get("name"));
        studentVO.setGender((String) source.get("gender"));
        studentVO.setAge((int) source.get("age"));
        studentVOS.add(studentVO);
    }

    //获取结果总数
    TotalHits totalHits = searchHits.getTotalHits();
    long value = totalHits.value;

    return studentVOS;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5664812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存