SpringBoot整合RestHighLevelClient实现查询操作

这篇具有很好参考价值的文章主要介绍了SpringBoot整合RestHighLevelClient实现查询操作。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

😊 @ 作者: 一恍过去
💖 @ 主页: https://blog.csdn.net/zhuocailing3390
🎊 @ 社区: Java技术栈交流
🎉 @ 主题: SpringBoot整合RestHighLevelClient实现查询操作
⏱️ @ 创作时间: 2022年08月14日

1、pom引入

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.7</version>
        </dependency>	
		<dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch的客户端 -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
        </dependency>
        <!-- elasticsearch依赖2.x的log4j -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
    </dependencies>

2、配置类

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 
 * @Date: 2022/8/13 10:47
 * @Description:
 **/
@Configuration
public class ElasticsearchConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
                // 配置ES连接地址
                RestClient.builder(new HttpHost("192.168.80.121", 9200, "http"))
        );
    }
}

3、数据准备

为了方便查询操作,创建索引叫做:nba,并且添加数据,http请求如下:

# 创建索引及映射
# PUT http://192.168.80.121:9200/nba
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "team_name": {
         "type": "keyword"
      },
      "position": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "play_year": {
      "type": "integer",
       "index": "false"
      },
      "jerse_no": {
      "type": "integer"
     }
    }
  }
}

# 依次创建如下文档
# POST http://192.168.80.121:9200/nba/_doc/1
{
  "name": "韦德",
  "team_name": "热火",
  "position": "得分后卫",
  "play_year": 16,
  "jerse_no": 3
}

# POST http://192.168.80.121:9200/nba/_doc/2
{
  "name": "波什",
  "team_name": "热火",
  "position": "大前锋",
  "play_year": 16,
  "jerse_no": 1
}

# POST http://192.168.80.121:9200/nba/_doc/3
{
  "name": "詹姆斯",
  "team_name": "热火",
  "position": "小前锋",
  "play_year": 16,
  "jerse_no": 6
}

# POST http://192.168.80.121:9200/nba/_doc/4
{
  "name": "张三三",
  "team_name": "热火",
  "position": "大前锋",
  "play_year": 16,
  "jerse_no": 1
}
# POST http://192.168.80.121:9200/nba/_doc/5
{
  "name": "张三四",
  "team_name": "热火",
  "position": "大前锋",
  "play_year": 16,
  "jerse_no": 1
}
# POST http://192.168.80.121:9200/nba/_doc/6
{
  "name": "是张三四",
  "team_name": "热火",
  "position": "大前锋",
  "play_year": 16,
  "jerse_no": 1
}
# POST http://192.168.80.121:9200/nba/_doc/7
{
  "name": "张三四五",
  "team_name": "热火",
  "position": "大前锋",
  "play_year": 16,
  "jerse_no": 1
}

4、查询操作

4.1 查询全部

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    
    /**
     * 查询全部数据
     * @return
     * @throws IOException
     */
    @GetMapping("/matchAll")
    public void addBatch() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引player的所有数据
        request.indices("player");
        // 查询方式为所有
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"詹姆斯","team_name":"热火","position":"小前锋","play_year":16,"jerse_no":6},
{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3}]

4.2 词条查询(term)

term查询被用于精确值匹配,不对查询条件进行分词,这些精确值可以是number、date、bool、keyword,不能是text。

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 词条查询
     * @return
     * @throws IOException
     */
    @GetMapping("/term")
    public void term() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 查询方式为 条词查询
        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("jerse_no", "3")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }	
}

结果:

[{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3}]

4.3 匹配查询(match)

4.3.1 or关系匹配(默认)

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * or关系匹配查询
     * @return
     * @throws IOException
     */
    @GetMapping("/match")
    public void match() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 查询条件为`大的前锋`的position进行查询,由于进行了分词,会匹配了所有包含`前锋`的文档
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchQuery("position", "大的前锋")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

查询条件为大的前锋的position进行查询,由于进行了分词,会匹配了所有包含前锋的文档

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"詹姆斯","team_name":"热火","position":"小前锋","play_year":16,"jerse_no":6}]

4.3.2 and关系匹配(operator)

match类型查询,会把查询条件进行分词,然后进行匹配查询,使用operator设置and关系,使得多个词条之间是and的关系。

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * and关系匹配查询
     * @return
     * @throws IOException
     */
    @GetMapping("/matchAnd")
    public void matchAnd() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 查询条件为`大的前锋`的position进行查询,由于进行了分词,会匹配了所有包含`前锋`的文档
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchQuery("position", "前锋大").operator(Operator.AND)));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

前锋大进行分词,并且字段要包含分词后的所有值(不管关键词所在位置)

{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1}

4.3.3 精确短语匹配(match_phrase)

按关键字分词顺序精确查询(忽略空格),结果只会出现"大前锋",不会有其他值;
比如大前锋会被分词为大、前锋,如果查询条件为大 前 锋就不会有数据,因为大 前 锋是三个词了与大、前锋不匹配;

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     *  精确短语匹配
     * @return
     * @throws IOException
     */
    @GetMapping("/matchPhrase")
    public void matchPhrase() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 按关键字顺序分词精确查询(忽略空格),结果只会出现"大前锋",不会有其他值
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchPhraseQuery("position", "大 前锋")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1}]

4.4 多字段查询(multi_match)

multi_match与match类似,不同的是它可以在多个字段中查询

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     *  多字段查询
     * @return
     * @throws IOException
     */
    @GetMapping("/multiMatch")
    public void multiMatch() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 按关键字顺序分词精确查询(忽略空格),结果只会出现"大前锋",不会有其他值
        request.source(new SearchSourceBuilder().query(QueryBuilders.multiMatchQuery("后卫","name","position")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }

}

结果:

name、postion这两个字段会对后卫进行分词查询(or关系)

[{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3}]

4.5 多词条精确匹配(terms)

terms 查询和 term 查询一样,但它允许指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件;

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 多词条精确匹配
     * @return
     * @throws IOException
     */
    @GetMapping("/terms")
    public void terms() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

		// 匹配 jerse_no为3或者6的数据
        request.source(new SearchSourceBuilder().query(QueryBuilders.termsQuery("jerse_no", "3","6")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

返回球衣号为3或者6的文档数据

[{"name":"詹姆斯","team_name":"热火","position":"小前锋","play_year":16,"jerse_no":6},
{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3}
]

4.6 前缀条词查询(prefix)

不能是text类型的字段

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 前缀条词查询
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "前缀条词查询", notes = "前缀条词查询")
    @GetMapping("/prefix")
    public void prefix() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 匹配指定前缀的字段(匹配以`大`开头的position字段)
        request.source(new SearchSourceBuilder().query(QueryBuilders.prefixQuery("position", "大")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }

}

结果:

匹配指定前缀的字段(匹配以开头的position字段)

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1}]

4.7 正则条词查询

请求参数,查询name包含 的数据;
韦的Unicode码:\u97e6,韶的Unicode码:\u97f6,查询name字段包含 \u97e6到\u97f6区间的中文;

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 正则条词查询
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "正则条词查询", notes = "正则条词查询")
    @GetMapping("/regexp")
    public void regexp() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 匹配name包含 `韦`的数据
        request.source(new SearchSourceBuilder().query(QueryBuilders.regexpQuery("name", "[\u97e6-\u97f6]")));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }

}

结果:

[{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3}
]

4.8 高亮查询

高亮查询就是在普通查询的基础上,加上高亮字段

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    
    /**
     * 高亮查询
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "高亮查询", notes = "高亮查询")
    @GetMapping("/highlight")
    public void highlight() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 高亮查询就是在普通查询的基础上,加上高亮字段
        SearchSourceBuilder builder = new SearchSourceBuilder();
        PrefixQueryBuilder prefixQueryBuilder = QueryBuilders.prefixQuery("position", "大");

        // 将position字段设置为高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("position");

        builder.highlighter(highlightBuilder);
        builder.query(prefixQueryBuilder);

        // 执行查询
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());

            // 高亮结果
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            System.out.println(highlightFields);
        }
    }
}

结果:

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{position=[position], fragments[[<font color='red'></font>前锋]]}]

4.9 模糊查询(fuzzy)

模糊查询是指,允许查询结果出现误差,误差范围可以进行手动指定

比如:查询name张三,误差值为1,那么可能会查询出张三三张三三四张三四五等结果,误差值可以设置为AUTO

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 模糊查询
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "模糊查询", notes = "模糊查询")
    @GetMapping("/fuzzy")
    public void fuzzy() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 查询`name`为`张三`,误差值为`2`,那么可能会查询出`张三三`、`张三三四`、`张三四五`等结果
        request.source(new SearchSourceBuilder().query(QueryBuilders.fuzzyQuery("name", "张三").fuzziness(Fuzziness.AUTO)));
//        request.source(new SearchSourceBuilder().query(QueryBuilders.fuzzyQuery("name", "张三").fuzziness(Fuzziness.TWO)));
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

[{"name":"张三三","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"是张三四","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"张三四","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"张三四五","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1}]

4.10 指定显示字段查询(过滤字段)

默认情况下,在搜索的结果会把文档的所有字段都返回存入_source中,如果我们只想获取其中的部分字段,我们可以添加_source的过滤,也可以指定不返回字段参数,适用于所有的查询请求,示例如下:

# 1、指定返回字段
"_excludes": ["name","team_name"]

# 2、includes:指定想要显示的字段
"_source": {"includes": ["name","nickname"] }

# 3、excludes:指定不想要显示的字段
	"_source": {"excludes": ["name","nickname"] }

代码:

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 指定显示字段查询
     * @throws IOException
     */
    @ApiOperation(value = "指定显示字段查询", notes = "指定显示字段查询")
    @GetMapping("/source")
    public void source() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 查询条件为`大的前锋`的position进行查询,由于进行了分词,会匹配了所有包含`前锋`的文档
        SearchSourceBuilder builder = new SearchSourceBuilder();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("position", "大的前锋");
        builder.query(matchQueryBuilder);

        // 设置展示字段,fetchSource的第一个参数为需要展示的字段,第二个参数为不需要展示字段,二者取一即可
        // 不展示`team_name`字段
        builder.fetchSource(null,"team_name");

        // 展示`name`和`team_name`字段
        String [] includes = {"team_name","name"};
        builder.fetchSource(includes,null);

        // 执行查询
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:

只展示nameteam_name字段

{"name":"波什","team_name":"热火"},
{"name":"詹姆斯","team_name":"热火"},]

4.11 布尔组合查询

bool把各种其它查询通过must(与)must_not(非)should(或)的方式进行组合;

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 布尔组合查询
     * @throws IOException
     */
    @ApiOperation(value = "布尔组合查询", notes = "布尔组合查询")
    @GetMapping("/bool")
    public void bool() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 布尔组合查询
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        /*
        * must:`与`条件
        * mustNot:`非`条件
        * should:`或`条件
         */
        //boolQueryBuilder.must(QueryBuilders.matchQuery("jerse_no", 30));
        //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name", "韦"));

        // 查询`name`为`韦`或者`jerse_no`为`6`的数据
        boolQueryBuilder.should(QueryBuilders.matchQuery("name", "韦"));
        boolQueryBuilder.should(QueryBuilders.matchQuery("jerse_no", 6));

        // 执行查询
        builder.query(boolQueryBuilder);
        request.source(builder);

        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for ( SearchHit hit : hits ) {
            System.out.println(hit.getSourceAsString());
        }
    }

}

结果:

查询name或者jerse_no6的数据

[{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3},
{"name":"詹姆斯","team_name":"热火","position":"小前锋","play_year":16,"jerse_no":6}]

4.12 范围查询

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 布尔组合查询
     * @throws IOException
     */
    @ApiOperation(value = "布尔组合查询", notes = "布尔组合查询")
    @GetMapping("/range")
    public void range() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 范围查询
        SearchSourceBuilder builder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("jerse_no");

        // 查询jerse_no在1到5的数据
        rangeQuery.gte(1);
        rangeQuery.lte(5);

        builder.query(rangeQuery);
        request.source(builder);

        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }

}

结果:

查询jerse_no在1到5的数据

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3}]

range查询允许以下字符:

操作符 说明
gt 大于
gte 大于等于
lt 小于
lte 小于等于

4.13 排序查询

排序查询,是对所有类型查询的结果,进行排序处理;

@RestController
@RequestMapping("/query")
@Slf4j
public class QueryController {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    /**
     * 排序查询
     * @throws IOException
     */
    @ApiOperation(value = "排序查询", notes = "排序查询")
    @GetMapping("/sort")
    public void sort() throws IOException {
        SearchRequest request = new SearchRequest();
        // 查询索引为nba的数据
        request.indices("nba");

        // 对查询后的数据进行排序
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());

        // 对结果排序
        // play_year 倒序
        builder.sort("play_year", SortOrder.DESC);
        // jerse_no 顺序
        builder.sort("jerse_no", SortOrder.ASC);

        // 执行查询
        request.source(builder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        // 获取数据
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsString());
        }
    }
}

结果:文章来源地址https://www.toymoban.com/news/detail-439685.html

[{"name":"波什","team_name":"热火","position":"大前锋","play_year":16,"jerse_no":1},
{"name":"韦德","team_name":"热火","position":"得分后卫","play_year":16,"jerse_no":3},
{"name":"詹姆斯","team_name":"热火","position":"小前锋","play_year":16,"jerse_no":6}]

到了这里,关于SpringBoot整合RestHighLevelClient实现查询操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • springboot整合RestHighLevelClient

    yml config https://www.cnblogs.com/tanghaorong/p/16344391.html

    2024年02月05日
    浏览(35)
  • ES聚合查询 基于RestHighLevelClient依赖 Java操作

    一、介绍 (偏自我理解)         1.ES聚合查询通用流程                 1.分组 ( 好比Mysql --- group by )                 2.组内聚合 也叫 组内指标( 好比Mysql --- SUM()、COUNT()、AVG()、MAX()、MIN() )         2.桶(我要是es开发者,我起名叫啥都行)                 1.满足特

    2024年02月06日
    浏览(46)
  • SpringBoot+Elasticsearch使用resthighlevelclient对象查询条件为“且+或”

    查询年龄为15或者16或者17或者18的且班级为1班的学生信息 首先,确保您的项目中包含了 Elasticsearch 的依赖: 然后,您可以创建一个包含查询逻辑的服务类。假设您有一个名为 StudentService 的服务类: 在上述代码中,您需要替换 your_index_name 为实际的 Elasticsearch 索引名称,并根

    2024年01月25日
    浏览(45)
  • ElasticSearch系列 - SpringBoot整合ES:restHighLevelClient.count(countRequest, RequestOptions.DEFAULT)

    restHighLevelClient.count(countRequest, RequestOptions.DEFAULT) 是 Elasticsearch Java High Level REST Client 中用于执行计数请求的方法。 具体来说,它接受两个参数: countRequest:一个 CountRequest 对象,表示计数请求的参数,包括要计数的索引、查询条件等。 RequestOptions.DEFAULT:一个 RequestOptions 对象

    2024年02月08日
    浏览(58)
  • SpringBoot(57) 整合Plumelog实现日志查询

    一、前言 Plumelog 一个简单易用的java分布式日志组件 https://gitee.com/plumeorg/plumelog 二、docker-compose一键搭建日志服务 plumelog + elasticsearch + redis tips: 详情可查看 https://gitee.com/zhengqingya/docker-compose 访问地址:ip地址:8891 账号:admin 密码:admin docker-compose.yml 三、SpringBoot整合Plumelog 1、

    2023年04月24日
    浏览(44)
  • SpringBoot整合ElasticSearch实现分页查询

    本文使用SpringBoot整合ElasticSearch实现分页查询 还是继续使用spring-boot-starter-data-elasticsearch来实现分页查询操作 数据准备 使用ElasticsearchRestTemplate来实现 程序结果 使用ElasticsearchOperations来实现 程序结果 本文记录了SpringBoot整合ElasticSearch来实现分页查询的两种方式

    2024年01月25日
    浏览(48)
  • 原生语言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文档的基本操作,es的高级查询.查询结果处理. 数据聚合.相关性系数打分

    ​ Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasti

    2024年02月05日
    浏览(80)
  • RestHighLevelClient实现ElasticSearch关联查询之父子文档

    RestHighLevelClient实现ElasticSearch关联查询之父子文档今天分享,承接上一篇内容: DSL操作关联查询 这篇我们通过javaAPI的方式实现: 一、springboot 配置 1、pom文件引用: 2、初始化配置es操作类 3、业务层引用 二、核心伪代码 1、创建父子索引 kibana查看 2、判断索引是否存在 3、删

    2024年02月10日
    浏览(40)
  • springboot整合elasticsearch实现类似于mysql的like查询

    目录 一、ES分页查询常用方式 二、引入es的依赖 三、es配置文件 四、es工具类 五、分页查询示例 1.from + size from表示从第几行开始,size表示查询多少条文档。from默认为0,size默认为10,最灵活的分页方式。 2.scroll 不适合用来做实时搜索,而更适用于后台批处理任务,如日志导

    2023年04月09日
    浏览(36)
  • SpringBoot 整合ElasticSearch实现模糊查询,批量CRUD,排序,分页,高亮

    准备一个空的SpringBoot项目 写入依赖 注意你的SpringBoot和你的es版本,一定要对应,如果不知道的可以查看这篇文章:https://blog.csdn.net/u014641168/article/details/130386872 我的版本是2.2.6,所以用的ES版本是 6.8.12,安装es请看这篇文章:https://blog.csdn.net/u014641168/article/details/130622430 查看

    2024年02月08日
    浏览(51)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包