版本
不同版本的elasticsearch-rest-high-level-client和 elasticsearch之间存在兼容风险,请确保和elasticsearch版本一致,否则会出现无法预计的错误。
es配置
maven依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.0</version>
</dependency>
配置application.properties
spring.application.name=service-search
#多个节点用逗号隔开
elasticsearch.hostlist=127.0.0.1:9200
创建配置类ElasticsearchConfig
@Configuration
public class ElasticsearchConfig {
@Value("${elasticsearch.hostlist}")
private String hostlist;
@Bean(destroyMethod = "close")
public RestHighLevelClient restHighLevelClient(){
String[] split = hostlist.split(",");
HttpHost[] httpHost = new HttpHost[split.length];
for (int i = 0; i < split.length; i++) {
String[] item = split[i].split(":");
httpHost[i] = new HttpHost(item[0],Integer.parseInt(item[1]),"http");
}
return new RestHighLevelClient(RestClient.builder(httpHost));
}
}
搜索测试代码
搜索全部记录
GET /book/_search
{
"query": {
"match_all": {}
}
}
@SpringBootTest(classes = SearchApplication.class)
@RunWith(SpringRunner.class)
@Slf4j
public class TestSearch {
@Autowired
private RestHighLevelClient client;
//搜索全部记录
@Test
public void testSearchAll() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//获取某些字段
searchSourceBuilder.fetchSource(new String[]{"name"},new String[]{});
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
}
分页搜索
GET /book/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
@Test
public void testSearchPage() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
//设置分页
int page = 1;//当前页
int size = 2;//每页显示记录数
int from = (page - 1) * size;//每页起始记录数计算
searchSourceBuilder.from(from).size(size);
//获取某些字段
searchSourceBuilder.fetchSource(new String[]{"name"},new String[]{});
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
ids搜索
GET /book/_search
{
"query": {
"ids" : {
"values" : ["1", "4", "100"]
}
}
}
@Test
public void testSearchIds() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.idsQuery().addIds("1","4","100"));
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
按关键词搜索matchQuery
GET /book/_search
{
"query" : {
"match" : {
"description" : "java程序员"
}
}
}
@Test
public void testSearchMatch() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("description","java程序员"));
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
multi_match搜索
GET /book/_search
{
"query": {
"multi_match": {
"query": "java程序员",
"fields": ["name", "description"]
}
}
}
@Test
public void testSearchMultiMatch() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.multiMatchQuery("java程序员","name","description"));
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
bool搜索
GET /book/_search
{
"query": {
"bool" : {
"must" : [
{
"multi_match": {
"query": "java程序员",
"fields": ["name","description"]
}
}
],
"should" : [
{
"match": {
"studymodel": "201001"
}
}
]
}
}
}
@Test
public void testSearchBool() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//构建multi_match请求
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("java程序员", "name", "description");
//构建match请求
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("studymodel", "201001");
//构建bool请求
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(multiMatchQueryBuilder);
boolQueryBuilder.should(matchQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
filter搜索
GET /book/_search
{
"query": {
"bool" : {
"must" : [
{
"multi_match": {
"query": "java程序员",
"fields": ["name","description"]
}
}
],
"should" : [
{
"match": {
"studymodel": "201001"
}
}
],
"filter": {
"range": {
"price": {
"gte": 50,
"lte": 90
}
}
}
}
}
}
@Test
public void testSearchFilter() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//构建multi_match请求
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("java程序员", "name", "description");
//构建match请求
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("studymodel", "201001");
//构建bool请求
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(multiMatchQueryBuilder);
boolQueryBuilder.should(matchQueryBuilder);
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(50).lte(90));
searchSourceBuilder.query(boolQueryBuilder);
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
sort搜索
GET /book/_search
{
"query": {
"bool" : {
"must" : [
{
"multi_match": {
"query": "java程序员",
"fields": ["name","description"]
}
}
],
"should" : [
{
"match": {
"studymodel": "201001"
}
}
],
"filter": {
"range": {
"price": {
"gte": 50,
"lte": 90
}
}
}
}
},
"sort": [
{
"price": {
"order": "asc"
}
}
]
}
@Test
public void testSearchSort() throws IOException {
//1、构建搜索请求
SearchRequest searchRequest = new SearchRequest("book");
//构建搜索的请求体
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//构建multi_match请求
MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("java程序员", "name", "description");
//构建match请求
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("studymodel", "201001");
//构建bool请求
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(multiMatchQueryBuilder);
boolQueryBuilder.should(matchQueryBuilder);
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(50).lte(90));
searchSourceBuilder.query(boolQueryBuilder);
//按照价格字段升序排序
searchSourceBuilder.sort("price", SortOrder.ASC);
searchRequest.source(searchSourceBuilder);
//2、执行
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//3、获取结果
SearchHits searchHits = searchResponse.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
float score = hit.getScore();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String name = (String)sourceAsMap.get("name");
String description = (String)sourceAsMap.get("description");
Double price = (Double)sourceAsMap.get("price");
System.out.println("id: "+id);
System.out.println("score: "+score);
System.out.println("name: "+name);
System.out.println("description: "+description);
System.out.println("price: "+price);
System.out.println("---------------------------------");
}
}
网友评论