DSL查询
This commit is contained in:
parent
839a976465
commit
d0741b4ed7
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="tb_hotel@106.15.251.123" uuid="a8eb04e7-59f0-4872-8f1d-fc3b8a19688b">
|
||||
<driver-ref>mysql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<imported>true</imported>
|
||||
<remarks>$PROJECT_DIR$/src/main/resources/application.yaml</remarks>
|
||||
<jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:mysql://106.15.251.123:3305/tb_hotel?useSSL=false</jdbc-url>
|
||||
<jdbc-additional-properties>
|
||||
<property name="com.intellij.clouds.kubernetes.db.host.port" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
|
||||
<property name="com.intellij.clouds.kubernetes.db.container.port" />
|
||||
</jdbc-additional-properties>
|
||||
<working-dir>$ProjectFileDir$</working-dir>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
|
@ -1,19 +1,13 @@
|
|||
package cn.itcast.hotel;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@MapperScan("cn.itcast.hotel.mapper")
|
||||
@SpringBootApplication
|
||||
public class HotelDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HotelDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,184 @@
|
|||
package cn.itcast.hotel;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.lucene.search.TotalHits;
|
||||
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.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class DemoTest {
|
||||
private RestHighLevelClient client;
|
||||
|
||||
/**
|
||||
* * 因为下面会重复使用输出语句所以就单独抽出来封装了
|
||||
*
|
||||
* @param response 查询结果响应体
|
||||
*/
|
||||
public static void handelResponse(SearchResponse response) {
|
||||
// 查询的总数
|
||||
TotalHits total = response.getHits().getTotalHits();
|
||||
System.out.println("total===>" + total);
|
||||
|
||||
SearchHits searchHits = response.getHits();
|
||||
// 输出查询结果
|
||||
searchHits.forEach(hit -> {
|
||||
// 查询时JSON数据,可以使用实体将其转换。
|
||||
String json = hit.getSourceAsString();
|
||||
System.out.println(json);
|
||||
|
||||
// 高亮返回体
|
||||
Map<String, HighlightField> map = hit.getHighlightFields();
|
||||
if (map != null) {
|
||||
System.out.println("-----------------高亮显示---------------");
|
||||
HighlightField highlightField = map.get("name");
|
||||
System.out.println("name===>" + highlightField.getName());
|
||||
System.out.println("fragments===>" + highlightField.getFragments()[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.client = new RestHighLevelClient(RestClient.builder(
|
||||
HttpHost.create("http://192.168.3.98:9200"))
|
||||
);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void teardown() throws IOException {
|
||||
this.client.close();
|
||||
}
|
||||
|
||||
// 普通查询
|
||||
@Test
|
||||
void testMatchAll() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询全部内容
|
||||
request.source().query(QueryBuilders.matchAllQuery());
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// match查询-单字段查询
|
||||
@Test
|
||||
void testMatch() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容
|
||||
request.source().query(QueryBuilders.matchQuery("all", "如家"));
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// match查询-多字段查询
|
||||
@Test
|
||||
void testMultiMatchQuery() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容
|
||||
request.source().query(QueryBuilders.multiMatchQuery("如家", "name", "business"));
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// 精确查询
|
||||
@Test
|
||||
void termQuery() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容-精确查找
|
||||
request.source().query(QueryBuilders.termQuery("city", "深圳"));
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// 范围查找
|
||||
@Test
|
||||
void testRange() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容
|
||||
request.source().query(QueryBuilders.rangeQuery("price").gte(100).lt(150));
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// 布尔查询
|
||||
@Test
|
||||
void testBoolean() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容
|
||||
BoolQueryBuilder query = QueryBuilders.boolQuery();
|
||||
query.must(QueryBuilders.termQuery("city", "杭州"));
|
||||
query.filter(QueryBuilders.rangeQuery("price").lt(250));
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// 排序、分页
|
||||
@Test
|
||||
void testFrom() throws IOException {
|
||||
// 页码,每页大小
|
||||
int page = 1, size = 5;
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容
|
||||
request.source().query(QueryBuilders.matchAllQuery());
|
||||
request.source().from((page - 1) * size).size(5);
|
||||
request.source().sort("price", SortOrder.ASC);
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
|
||||
// 高亮
|
||||
@Test
|
||||
void testHighLighter() throws IOException {
|
||||
// 指定查询的索引库
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
|
||||
// 查询内容
|
||||
request.source().query(QueryBuilders.matchQuery("all", "如家"));
|
||||
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
|
||||
|
||||
// 得到查询返回体,传入返回体到封装的返回体输出
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
handelResponse(response);
|
||||
}
|
||||
}
|
|
@ -33,7 +33,8 @@ public class HotelDocumentTest {
|
|||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
this.client = new RestHighLevelClient(RestClient.builder(
|
||||
HttpHost.create("http://192.168.1.4:9200"))
|
||||
// HttpHost.create("http://192.168.1.4:9200"))
|
||||
HttpHost.create("http://192.168.3.98:9200"))
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,8 @@ public class HotelIndexTest {
|
|||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
this.client = new RestHighLevelClient(RestClient.builder((
|
||||
HttpHost.create("http://192.168.1.4:9200"))
|
||||
// HttpHost.create("http://192.168.1.4:9200"))
|
||||
HttpHost.create("http://192.168.3.98:9200"))
|
||||
// 集群缓环境下可以配置多个
|
||||
// HttpHost.create("http://192.168.1.7:9200")
|
||||
));
|
||||
|
|
|
@ -0,0 +1,145 @@
|
|||
package cn.itcast.hotel;
|
||||
|
||||
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.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class HotelSearchTest {
|
||||
private RestHighLevelClient client;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.client = new RestHighLevelClient(RestClient.builder(
|
||||
// HttpHost.create("http://192.168.1.4:9200"))
|
||||
HttpHost.create("http://192.168.3.98:9200")
|
||||
));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void teardown() throws Exception {
|
||||
this.client.close();
|
||||
}
|
||||
|
||||
// 查询文档
|
||||
@Test
|
||||
void testMatchAll() throws IOException {
|
||||
// 1. 准备request
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
// 2. 准备DSL
|
||||
request.source().query(QueryBuilders.matchAllQuery());
|
||||
// 3.发送请求
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
// 解析结果
|
||||
SearchHits searchHits = response.getHits();
|
||||
long total = searchHits.getTotalHits().value;
|
||||
// 总数
|
||||
System.out.println("total===>" + total);
|
||||
searchHits.forEach(hit -> {
|
||||
// 可以将这个转成酒店的对象
|
||||
String json = hit.getSourceAsString();
|
||||
System.out.println(json);
|
||||
});
|
||||
}
|
||||
|
||||
// 根据字段查询
|
||||
@Test
|
||||
void testMatch() throws Exception {
|
||||
// 准备Request
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
// 准备DSL
|
||||
request.source().query(QueryBuilders.matchQuery("all", "如家"));
|
||||
// 发起请求
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
// 解析响应
|
||||
SearchHits searchHits = response.getHits();
|
||||
// 得到总数
|
||||
long total = searchHits.getTotalHits().value;
|
||||
System.out.println("total===>" + total);
|
||||
SearchHit[] hits = searchHits.getHits();
|
||||
for (SearchHit hit : hits) {
|
||||
System.out.println(hit);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据范围查询
|
||||
@Test
|
||||
void testBool() throws Exception {
|
||||
// 1. 准备request
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
// 准备DSL
|
||||
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
|
||||
boolQuery.must(QueryBuilders.termQuery("city", "上海"));
|
||||
// 添加range
|
||||
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));
|
||||
request.source().query(boolQuery);
|
||||
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
SearchHits searchHits = response.getHits();
|
||||
|
||||
// 得到总数
|
||||
long total = searchHits.getTotalHits().value;
|
||||
System.out.println("total===>" + total);
|
||||
searchHits.forEach(hit -> {
|
||||
// 可以将这个转成酒店的对象
|
||||
String json = hit.getSourceAsString();
|
||||
System.out.println(json);
|
||||
});
|
||||
}
|
||||
|
||||
// 排序查询
|
||||
@Test
|
||||
void testPageSort() throws Exception {
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
request.source().query(QueryBuilders.matchAllQuery());
|
||||
request.source().sort("price", SortOrder.ASC);
|
||||
request.source().from(0).size(5);
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
|
||||
SearchHits searchHits = response.getHits();
|
||||
searchHits.forEach(hit -> {
|
||||
String json = hit.getSourceAsString();
|
||||
System.out.println(json);
|
||||
});
|
||||
}
|
||||
|
||||
// 高亮显示
|
||||
@Test
|
||||
void testHighlight() throws Exception {
|
||||
// 准备request
|
||||
SearchRequest request = new SearchRequest("hotel");
|
||||
// 准备DSL
|
||||
request.source().query(QueryBuilders.matchQuery("all", "如家"));
|
||||
// 高亮
|
||||
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
|
||||
// 发送请求
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
// 解析响应
|
||||
SearchHits searchHits = response.getHits();
|
||||
searchHits.forEach(hit -> {
|
||||
String json = hit.getSourceAsString();
|
||||
System.out.println(json);
|
||||
|
||||
// 得到高亮
|
||||
Map<String, HighlightField> map = hit.getHighlightFields();
|
||||
HighlightField highlightField = map.get("name");
|
||||
System.out.println("name===>" + highlightField.getName());
|
||||
System.out.println("fragments===>" + highlightField.getFragments()[0]);
|
||||
});
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue