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);
?
}
高級查詢
1、分頁查詢
2、聚合查詢
3、高亮查詢
//高亮處理
@Highlight(fields = {@HighlightField(name = "desc"),@HighlightField(name = "address"),@HighlightField(name = "username")},parameters = @HighlightParameters(preTags = {"<span style='color:red'>"},postTags = {"</span>"})
)
public List<SearchHit<User>> findByDescOrAddressOrUsername(String desc, String address, String username);
五、es專業名詞
節點
分片
副本
索引
六、mysq+es的數據同步
-
使用Logstash實現Mysql與ElasticSearch實時同步。
-
使用go-mysql-elasticsearch實現Mysql與ElasticSearch實時同步。
-
使用RabbitMQ實現Mysql與ElasticSearch實時同步。
-
使用阿里巴巴Canal實現Mysql與ElasticSearch實時同步。
當前項目
主數據庫: mysql 事務機制
輔助數據庫:redis (小數據量的頻繁訪問 內存 首頁商品分類 推薦商品)
es(大量數據的檢索 模糊查詢 | 數據來自于多個字段 百度搜索 商城的搜索)
mysql:商品表
redis:查詢mysql中的數據,新增到redis
mysql------------------》es
方法(saveToMysql -->mq--> saveToEs )(updateToMysql -->mq--->updateToEs ) (deleteToMysql deleteToEs)
項目上線:首次同步,批量同步
項目運行:增量同步