@Test//添加文档到索引库
void testIndexDocument() throws IOException {
//GET /hotel/_doc/1
IndexRequest request = new IndexRequest("hotel").id("1");
request.source("{\"name\":\"zs\",\"city\":\"长沙\"}",XContentType.JSON);
client.index(request,RequestOptions.DEFAULT);
//在index这里创建倒排索引
}
@TableName("tb_hotel")
public class Hotel {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
private String latitude;
private String longitude;
private String pic;
}
mysql
mysql-connector-java
5.1.49
com.baomidou
mybatis-plus-boot-starter
3.0.5
public class HotelDoc {
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
/*经纬度换成location*/
private String location;
private String pic;
public HotelDoc() {
}
/*构造函数*/
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
/*纬度和经度*/
this.location = hotel.getLatitude()+","+hotel.getLongitude();
this.pic = hotel.getPic();
}
}
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.8.171:3306/hotel
spring.datasource.username=root
spring.datasource.password=root
#扫描包
mybatis-plus.mapper-locations=classpath:mapper/*.xml
#别名
mybatis-plus.type-aliases-package=com.pro.domain
#驼峰
mybatis-plus.configuration.map-underscore-to-camel-case=true
/*根据id查出索引库的文档,强转为对象输出*/
@Test
public void testGetDocumentById() throws IOException {
GetRequest request = new GetRequest("hotel", "38665");
//发请求,得到响应
GetResponse response = client.get(request, RequestOptions.DEFAULT);
String json = response.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
System.out.println(hotelDoc);
}
/*根据id修改索引库对应的文档*/
@Test
public void testUpdateDocument(){
//1.request
UpdateRequest request = new UpdateRequest("hotel", "38665");
//修改
request.doc(
"price","262",
"starName","三钻"
);
}
/*根据id删除索引库对应的文档*/
@Test
public void TestDeleteDocumentById() throws IOException {
//创建request对象
DeleteRequest request = new DeleteRequest("hotel", "38665");
//删除文档
client.delete(request,RequestOptions.DEFAULT);
}
/*将MySQL查出来的所有记录加到索引库
* 批量操作
* */
@Test
public void testBulkRequest() throws IOException {
QueryWrapper queryWrapper = new QueryWrapper();
List hotelList = hotelService.list(queryWrapper);
BulkRequest request = new BulkRequest();
for (Hotel hotel : hotelList) {
HotelDoc hotelDoc = new HotelDoc(hotel);
//将数据对象,一个个转为json,加入到批量操作的对象request中
request.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
//发送请求
client.bulk(request,RequestOptions.DEFAULT);
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
https://www.kancloud.cn/apachecn/elasticsearch-doc-zh/1945172
select * from class;
select * from stu;
select * from stu where classid = 1;
-- in 条件可以是一个或多个--
select * from stu where classid in (1);
select * from stu where classid in (1,2);
select * from stu where classid = (select classid from class where classname='1班');
-- 五个聚合函数 --
select count(*) from stu;
select avg(age) from stu;
select sum(age) from stu;
select max(age) from stu;
select min(age) from stu;
-- 分组查询 select 后面只能跟分组的字段,聚合函数--
select classid,avg(age) from stu GROUP BY classid;
-- 对所有记录筛选 --
select * from stu where age < 20;
-- 对组进行筛选,使用having,后面只能跟分组的字段,聚合函数 --
select classid,avg(age) from stu GROUP BY classid having avg(age) > 21;
-- 温哥华 --
select classid,avg(age) from stu where gender = '男' GROUP BY classid having avg(age) > 21;
select * from stu,class;
select * from stu,class where stu.classid=class.classid and stu.stuid=1;
-- 内连接 两边协商,没有的去取消,查出5条数据 --
select * from stu s inner join class c on s.classid=c.classid;
-- 左连接,以左为主,可以查6条数据 --
select * from stu s left join class c on s.classid=c.classid;
-- 右连接,以右为主,可以查5条数据 --
select * from stu s right join class c on s.classid=c.classid;
#查询dsl的语法
GET /hotel/_search
{
"query":{
"查询类型":{
"FIELD":"TEXT"
}
}
}
#查所有
GET /hotel/_search
{
"query":{
"match_all":{}
}
}
#精准查询 term 特点:不分词
GET /hotel/_search
{
"query":{
"term":{
"city":{
"value": "上海"
}
}
}
}
#范围内精准查询 range 特点:不分词
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 300
}
}
}
}
#地理查询,经纬度查询:
#geo_distance: 圆形范围
GET /hotel/_search
{
"query":{
"geo_distance":{
"distance":"150km",
"location":"31.174377,121.442875"
}
}
}
geo_bounding_box矩形范围:lat纬度,lon经度
#geo_bounding_box矩形范围:lat纬度,lon经度
GET /hotel/_search
{
"query":{
"geo_bounding_box":{
"location":{
"top_left":{
"lat":31.1,
"lon":121.5
},
"bottom_right":{
"lat":30.9,
"lon":121.7
}
}
}
}
}
#复合查询
将简单的查询组合起来
算分函数查询,function score ,可以控制文档相关性算分,
控制文档排名
1)function score
先查所有all里面分词有外滩的文档,然后再过滤出brand为如家的品牌(精准过滤),最后对对应文档的_score进行操作
其它 :sum,avg,max,min
#1)function score先查所有all里面分词有外滩的文档,
然后再过滤出brand为如家的品牌(精准过滤),最后对对应文档的_score进行操作
GET /hotel/_search
{
"query":{
"function_score": {
"query": {
"match":{
"all":"外滩"
}
},
"functions": [
{
"filter": {
"term":{
"brand": "如家"
}
},
"weight":10
}
],
"boost_mode": "replace"
}
}
}
#搜索如家,价格小于等于400,坐标在31.2,121.5周围十公里范围内的酒店
GET /hotel/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"FIELD": "如家"
}
}
],
"must_not": [
{
"range": {
"FIELD": {
"gte": 400
}
}
}
],
"filter": [
{
"geo_distance":{
"distance":"10km",
"location":{
"lat":31.21,
"lon":121.5
}
}
}
]
}
}
}
}
- #排序,根据评分降序
GET /hotel/_search
{
"query":{
"match_all": {}
},
"sort":[
{
"score":{
"order":"desc"
}
}
]
}
- #按坐标排序
GET /hotel/_search
{
"query":{
"match_all": {}
},
"sort":[
{
"_geo_distance": {
"location": "31.21,121.5",
"order": "desc",
"unit": "km"
}
}
]
}
- #按分值排序,分值一致时,按价格升序
GET /hotel/_search
{
"query":{
"match_all": {}
},
"sort":[
{
"score":{
"order": "desc"
},
"price": {
"order": "asc"
}
}
]
}
- #按某坐标,周围的酒店,距离降序排序
#查询的sort的值,是公里数
#注意,如果排序,则打分为null
GET /hotel/_search
{
"query":{
"match_all": {}
},
"sort":[
{
"_geo_distance": {
"location": {
"lat": 30,
"lon": 120
},
"order": "desc",
"unit": "km"
}
}
]
}
- #分页
#es 默认的返回10条,from,size,我们现在分20条
GET /hotel/_search
{
"query":{
"match_all": {}
},
"from": 0,
"size": 20,
"sort":[
{
"price": {
"order": "asc"
}
}
]
}
#练习:搜索:价格在220以内的酒店
#按从小到大升序排列
#取前五个酒店
GET /hotel/_search
{
"query": {
"range": {
"price": {
"lte": 220
}
}
},
"from": 0,
"size": 5,
"sort": [
{
"price": {
"order": "asc"
}
}
]
}