es0102---语法格式、数据类型、整合springboot、创建库、创建映射、新增数据、自定义查询
ES
一、创建映射字段的语法格式
需要先构建索引库,在构建索引库中的映射关系
PUT /索引库名/_mapping
{"properties": {"字段名": {"type": "类型","index": true,"store": false,"analyzer": "分词器"}}
}
#新增数据 id自增
POST /hl/_doc
{"title":"小米手机","images":"http://image.lano.com/12479122.jpg","price":2699.00
}#自己指定id信息
POST /hl/_doc/2
{"title":"OPPO手机","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#修改数据
POST /hl/_doc/2
{"title":"VIVO手机","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#智能判断 根据数据自动添加到映射,判断并指定数据类型
POST /hl/_doc/3
{"title":"超米手机","images":"http://image.lanou.com/12479122.jpg","price":2899.00,"stock": 200,"saleable":true
}put /hl/_doc/4
{"title":"小米电视AAA","images":"http://image.lano.com/12479122.jpg","price":2999.00
}#查询数据
GET hl/_search
{"query": {"match_all": {}}
}#or查询数据 小米or电视
GET hl/_search
{"query": {"match": {"title": "小米电视"}}
}
# and查询
GET hl/_search
{"query": {"match": {"title": {"query": "小米电视","operator": "and"}}}
}PUT /hl/_doc/5
{"title":"乐视电视","images":"http://image.lanou.com/12479122.jpg","price":2899.00,"subTitle":"小米电视手机"
}#多字段查询
GET hl/_search
{"query": {"multi_match": {"query": "小米","fields": [ "title", "subTitle" ]}}
}
#单词条精准查询
GET /hl/_search
{"query":{"term":{"price":2699.00}}
}
#多词条精准查询
GET /hl/_search
{"query":{"terms":{"price":[2699.00,2899.00]}}
}
#只查询特定字段
GET /hl/_search
{"_source": ["title","price"],"query": {"term": {"price": 2699}}
}
#只查询特定字段 指定includes和excludes
GET hl/_search
{
"_source": {
"excludes": "images",
"includes": ["title","price"]
},
"query": {
"term": {
"price": 2899.00
}
}
}PUT /hl/_doc/6
{"title":"apple手机","images":"http://image.lanou.com/12479122.jpg","price":6899.00
}
#模糊半径为1查询
GET hl/_search
{"query": {"fuzzy": {"title": "app"}}
}#模糊半径为2查询
GET hl/_search
{"query": {"fuzzy": {"title": {"value": "app22","fuzziness": 2}}}
}
类型名称:映射的名称,字段名:任意填写。Elasticsearch7.0之后不支持类名名称写法所以需要添加include_type_name=true参数进行支持设置。
type:类型,可以是text、long、short、date、integer、object等
index:是否可以使用索引查询,默认为true
store:是否额外存储,默认为false
analyzer:分词器,这里的
ik_max_word
即使用ik分词器
二、了解数据类型
1、字符串
text: 可分词 不可聚合
keyword:不可分词 可聚合
2、数值
整数和浮点(float、double、half_float、scaled_float)
3、日期
date
三、使用springboot创建es项目
1、jar包
spring-boot-starter-data-elasticsearch
2、配置文件
spring:
elasticsearch:
uris: http://1.94.230.82:9200
3、使用esTemplate模版工具类
@RestController
@RequestMapping("/es")
public class EsController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;
四、Es实现的功能
1、创建索引库
restTemplate.indexOps(User.class).create();
/*
@Document(indexName = "索引库名",shards = 分片数,replicas = 副本数)*/
@Document(indexName = "user",shards = 1,replicas = 0)
public class User {
}
package com.hl.es.web;
import com.hl.es.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/es")
public class EsController {@Autowiredprivate ElasticsearchRestTemplate restTemplate;
@RequestMapping("/test")public void getEsTemplate(){boolean flag = restTemplate.indexOps(User.class).exists();System.out.println("索引库是否存在:"+flag);if(!flag){//创建索引库boolean flag2 = restTemplate.indexOps(User.class).create();System.out.println("索引库创建结果:"+flag2);}}
}
2、创建映射
@Document(indexName = "user",shards = 1,replicas = 0)
@Data
public class User {@Idprivate Integer id;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String username;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String desc;@Field(type = FieldType.Keyword,index = false)private String password;@Field(type = FieldType.Text,analyzer = "ik_max_word")private String address;@Field(type = FieldType.Double)private Double money;@Field(type = FieldType.Date,format = DateFormat.custom,pattern = "YYYY-MM-dd")private Date createTime;
}
@RequestMapping("/createMapping")
public Object createMapping(){Document document = restTemplate.indexOps(User.class).createMapping();boolean flag = restTemplate.indexOps(User.class).putMapping(document);System.out.println("创建映射:"+flag);return flag;
}
3、新增数据
@RequestMapping("/save")
public Object save(User user){User user2 = restTemplate.save(user);return user2;
}
4、查询数据
自定义查询
package com.hl.es.dao;
import com.hl.es.pojo.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface UserDaoextends ElasticsearchRepository<User, Integer> {//根据用户名查询集合//单字段public List<User> findByUsername(String username);public List<User> findByAddress(String address);//多字段public List<User> findByDescAndAddress(String desc, String address);public List<User> findByDescOrAddress(String desc, String address);//范围查询public List<User> findAllByIdGreaterThanEqual(Integer minId);public List<User> findByMoneyBetween(Double minPrice, Double maxPrice);//先根据范围查询,再降序排序public List<User> findByMoneyBetweenOrderByMoneyDesc(Double minPrice, Double maxPrice);
}