面试题:
Q1: 生产环境中分片如何设置?
10分片,2副本。 ----> 参考了腾讯公司。
1G内存 ---> 20分片,一个节点假设有32G内存,其饱和度分配数量是640左右。
10台服务器,32c,32G,500个分片。饱和度 ---> 6400 分片。
Q2: ES的集群颜色
红色(red):
有部分主分片无法访问,意味着暂时ES集群有部分数据可能丢失了。
通常情况下,在ES集群启动时,可能会短暂出现这种情况下,或者是ES集群故障。
黄色(yellow):
部分副本分片无法访问,此时集群的所有数据都可以访问到,但应该尽快修复集群无法访问的分片。
绿色(green):
集群处于正常状态,即所有的主分片和副本分片均可以正常访问。
Q3: 文档的路由公式,文档是基于哪种机制存储到某个索引的?
hash("_id") % primary_shards_numbers ---> [0,primary_shards_numbers - 1]
Q4: ES的堆内存调优?
1)当物理机的内存数量小于64G时,建议设置为物理机的一半内存;
2)当物理机的内存数量大于64G时,建议最大设置的内存数量是32G,剩余的内存空间由Lucene分片使用;
3)不建议将物理内存全部使用,建议给操作系统保留[3%-10%]的内存空间。
修改ES的堆内存大小:
jps:
查看JAVA的相关进程的信息,其中第一列,查看的是PID,第二列是进程的名称。
常用选项:
jps:
显示程序本身。
jps -l :
显示程序的完整包名称。
jmap:
查看java的运行进程的相关信息,比如堆内存空间。
常见的选项:
jmap -heap
主要观察:
MaxHeapSize = 268435456 (256.0MB)
ES的堆内存修改
1)修改堆内存大小
vim /oldboyedu/softwares/elasticsearch-7.17.5/config/jvm.options
...
# 设置堆内存的初始化分配大小。
-Xms256m
# 设置堆内存的最大的内存使用上线。
-Xmx256m
2)同步到其他节点
data_rsync.sh /oldboyedu/softwares/elasticsearch-7.17.5/config/jvm.options
3)重启服务
systemctl restart es7
推荐阅读:
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/important-settings.html#heap-size-settings
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/advanced-configuration.html#set-jvm-heap-size
DSL查询: 全文搜索-match
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query":{
"match":{
"auther": "高云飞"
}
},
"size": 30
}
解释说明:
query:
固定语法,表示发起一次查询。
match:
标签全文检索,在数据库进行模糊搜索,会对中文进行分词。
size:
每页显示多少条数据。
DSL查询: 精确匹配-match_phrase
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query":{
"match_phrase":{
"auther": "李鹏飞"
}
}
}
解释说明:
query:
固定语法,表示发起一次查询。
match_phrase
精确匹配查询的内容。
DSL查询: 全量查询-match_all,可以不写请求体。
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query":{
"match_all":{}
}
}
解释说明:
query:
固定语法,表示发起一次查询。
match_all
匹配所有的内容。
DSL查询: 分页查询-size-form
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query":{
"match_phrase":{
"auther": "李鹏飞"
}
},
"size": 3,
"from": 9
}
解释说明:
query:
固定语法,表示发起一次查询。
size:
每页显示的数据条目。
from:
跳过的数据条目,若想要查看第N页,则应该跳过 "(N-1)* size"
DSL查询: 查看源数据指定的字段-_source
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query":{
"match_phrase":{
"auther": "王建平"
}
},
"_source": ["title","auther","brand","price"]
}
解释说明:
_source:
查看源数据指定的字段。
DSL查询: 查询存在某个字段的文档-exists
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query":{
"exists" : {
"field": "hobby"
}
}
}
解释说明:
exists
判断是否存在某个文档。
field:
指定基于哪个字段来判断该文档是否存在。
DSL查询: 语法高亮
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query": {
"match": {
"title": "香水"
}
},
"highlight": {
"pre_tags": [
"
DSL查询: 排序
1)准备数据
POST http://10.0.0.103:9200/_bulk
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "戴尔(DELL)31.5英寸 4K 曲面 内置音箱 低蓝光 影院级色彩 FreeSync技术 可壁挂 1800R 电脑显示器 S3221QS","price":3399.00, "brand":"Dell","weight":"15.25kg","item":"https://item.jd.com/100014940686.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "三星(SAMSUNG)28英寸 4K IPS 10.7亿色 90%DCI-P3 Eyecomfort2.0认证 专业设计制图显示器(U28R550UQC)","price":2099.00, "brand":"SAMSUNG","weight":"7.55kg","item":"https://item.jd.com/100009558656.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "ALIENWARE外星人新品外设高端键鼠套装AW510K机械键盘cherry轴 RGB/AW610M 610M 无线鼠标+510K机械键盘+510H耳机","price":6000.00, "brand":"ALIENWARE外星人","weight":"1.0kg","item":"https://item.jd.com/10030370257612.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "樱桃CHERRY MX8.0彩光87键游戏机械键盘合金女生樱粉色版 彩光-粉色红轴-粉色箱 官方标配","price":4066.00, "brand":"樱桃CHERRY","weight":"1.0kg","item":"https://item.jd.com/10024385308012.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "罗技(G)G610机械键盘 有线机械键盘 游戏机械键盘 全尺寸背光机械键盘 吃鸡键盘 Cherry红轴","price":429.00, "brand":"罗技","weight":"1.627kg","item":"https://item.jd.com/3378484.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "美商海盗船(USCORSAIR)K68机械键盘黑色 防水防尘樱桃轴体 炫彩背光游戏 有线 红光红轴","price":499.00, "brand":"美商海盗船","weight":"1.41kg","item":"https://item.jd.com/43580479783.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "雷蛇(Razer) 蝰蛇标准版 鼠标 有线鼠标 游戏鼠标 人体工程学 电竞 黑色 6400DPI lol吃鸡神器cf","price":109.00, "brand":"雷蛇","weight":"185.00g","item":"https://item.jd.com/8141909.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "罗技(G)G502 HERO主宰者有线鼠标 游戏鼠标 HERO引擎 RGB鼠标 电竞鼠标 25600DPI","price":299.00, "brand":"罗技","weight":"250.00g","item":"https://item.jd.com/100001691967.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "武极 i5 10400F/GTX1050Ti/256G游戏台式办公电脑主机DIY组装机","price":4099.00, "brand":"武极","weight":"5.0kg","item":"https://item.jd.com/1239166056.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "宏碁(Acer) 暗影骑士·威N50-N92 英特尔酷睿i5游戏台机 吃鸡电脑主机(十一代i5-11400F 16G 256G+1T GTX1650)","price":5299.00, "brand":"宏碁","weight":"7.25kg","item":"https://item.jd.com/100020726324.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "京天 酷睿i7 10700F/RTX2060/16G内存 吃鸡游戏台式电脑主机DIY组装机","price":7999.00, "brand":"京天","weight":"10.0kg","item":"https://item.jd.com/40808512828.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "戴尔(DELL)OptiPlex 3070MFF/3080MFF微型台式机电脑迷你小主机客厅HTPC 标配 i5-10500T/8G/1T+256G 内置WiFi+蓝牙 全国联保 三年上门","price":3999.00, "brand":"DELL","weight":"2.85kg","item":"https://item.jd.com/10025304273651.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "伊萌纯种英短蓝白猫活体猫咪幼猫活体英国短毛猫矮脚猫英短蓝猫幼体银渐层蓝白活体宠物蓝猫幼崽猫咪宠物猫短 双血统A级 母","price":4000.00, "brand":"英短","weight":"1.0kg","item":"https://item.jd.com/10027188382742.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "柴墨 金渐层幼猫英短猫宠物猫英短金渐层猫咪活体猫活体纯种小猫银渐层 双血统","price":12000.00, "brand":"英短","weight":"3.0kg","item":"https://item.jd.com/10029312412476.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "Redmi Note10 Pro 游戏智能5G手机 小米 红米","price":9999.00, "brand":"小米","weight":"10.00g","item":"https://item.jd.com/100021970002.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "【二手99新】小米Max3手机二手手机 大屏安卓 曜石黑 6G+128G 全网通","price":1046.00, "brand":"小米","weight":"0.75kg","item":"https://item.jd.com/35569092038.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "现货速发(10天价保)小米11 5G手机 骁龙888 游戏智能手机 PRO店内可选 黑色 套装版 12GB+256GB","price":4699.00, "brand":"小米","weight":"0.75kg","item":"https://item.jd.com/10025836790851.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "小米手环6 NFC版 全面彩屏 30种运动模式 24h心率检测 50米防水 智能手环","price":279.00, "brand":"小米","weight":"65.00g","item":"https://item.jd.com/100019867468.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "HUAWEI MateView无线原色显示器无线版 28.2英寸 4K+ IPS 98% DCI-P3 10.7亿色 HDR400 TypeC 双扬声器 双MIC","price":4699.00, "brand":"华为","weight":"9.8kg","item":"https://item.jd.com/100021420806.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "华为nova7se/nova7 se 5G手机( 12期免息可选 )下单享好礼 绮境森林 乐活版 8G+128G(1年碎屏险)","price":2999.00, "brand":"华为","weight":"500.00g","item":"https://item.jd.com/10029312412476.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "华为HUAWEI FreeBuds 4i主动降噪 入耳式真无线蓝牙耳机/通话降噪/长续航/小巧舒适 Android&ios通用 陶瓷白","price":479.00, "brand":"华为","weight":"137.00g","item":"https://item.jd.com/100018510746.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "HUAWEI WATCH GT2 华为手表 运动智能手表 两周长续航/蓝牙通话/血氧检测/麒麟芯片 华为gt2 46mm 曜石黑","price":1488.00, "brand":"华为","weight":"335.00g","item":"https://item.jd.com/100008492922.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "Apple苹果12 mini iPhone 12 mini 5G 手机(现货速发 12期免息可选) 蓝色 5G版 64G","price":4699.00, "brand":"苹果","weight":"280.00g","item":"https://item.jd.com/10026100075337.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "Apple iPhone 12 (A2404) 128GB 紫色 支持移动联通电信5G 双卡双待手机","price":6799.00, "brand":"苹果","weight":"330.00g","item":"https://item.jd.com/100011203359.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "华硕ROG冰刃双屏 十代英特尔酷睿 15.6英寸液金导热300Hz电竞游戏笔记本电脑 i9-10980H 32G 2T RTX2080S","price":48999.00, "brand":"华硕","weight":"2.5kg","item":"https://item.jd.com/10021558215658.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "联想小新Air15 2021超轻薄笔记本电脑 高色域学生办公设计师游戏本 八核锐龙R7-5700U 16G内存 512G固态 升级15.6英寸IPS全面屏【DC调光护眼无闪烁】","price":5499.00, "brand":"苹果","weight":"10.0kg","item":"https://item.jd.com/33950552707.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "苹果(Apple)MacBook Air 13.3英寸 笔记本电脑 【2020款商务灰】十代i7 16G 512G 官方标配 19点前付款当天发货","price":10498.00, "brand":"苹果","weight":"1.29kg","item":"https://item.jd.com/10021130510120.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "科大讯飞机器人 阿尔法蛋A10智能机器人 专业教育人工智能编程机器人学习机 智能可编程 白色","price":1099.00, "brand":"科大讯飞","weight":"1.7kg","item":"https://item.jd.com/100005324258.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "robosen乐森机器人六一儿童节礼物自营孩子玩具星际特工智能编程机器人儿童语音控制陪伴益智变形机器人","price":2499.00, "brand":"senpowerT9-X","weight":"3.01kg","item":"https://item.jd.com/100006740372.html"}
{ "create": { "_index": "oldboyedu-shopping"} }
{ "title": "优必选(UBTECH)悟空智能语音监控对话人形机器人儿童教育陪伴早教学习机玩具","price":4999.00, "brand":"优必选悟空","weight":"1.21kg","item":"https://item.jd.com/100000722348.html"}
2)查看数据
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query":{
"match_phrase":{
"brand": "dell"
}
},
"sort":{
"price":{
"order": "desc"
}
},
"size": 5,
"from": 0
}
解释说明:
sort:
对产品进行排序处理。
price:
这是一个自定义字段(该字段可以根据实际情况进行修改哟!),表示基于哪个字段进行排序。
order:
指定排序的顺序,有升序("asc")和降序("desc")之分。
DSL语句-多条件查询
1)使用must表示必须同时满足的条件
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"brand": "dell"
}
},
{
"match": {
"price": 3999
}
}
]
}
}
}
2)使用should表示仅满足其一的条件
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"brand": "dell"
}
},
{
"match": {
"price": 4699
}
}
]
}
}
}
3)使用should和must条件混用时需要引入"minimum_should_match",该值可以是数字,也可以是百分比。
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "小苹华"
}
}
],
"should": [
{
"match": {
"price": 279
}
},
{
"match": {
"price": 4699
}
},
{
"match":{
"weight": "65.00g"
}
}
],
"minimum_should_match": 2
}
}
}
4)使用should和must_not条件混用时无需引入"minimum_should_match"。
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"brand": "小苹华"
}
}
],
"should" :[
{
"match" : {
"price": 499
}
}
]
}
}
}
DSL语句-过滤查询-filter,配合其他的查询语句。
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "小苹华"
}
}
],
"should": [
{
"match_phrase": {
"title": "蓝色"
}
}
],
"minimum_should_match": 1,
"filter": {
"range": {
"price": {
"gte": 4699,
"lt": 8000
}
}
}
}
},
"sort": {
"price": {
"order": "desc"
}
},
"_source": [
"brand",
"price",
"title"
],
"highlight": {
"pre_tags": [
"
温馨提示:
filter可以基于范围(range)查询,对某个字段(price)进行比较
gt:
大于
lt:
小于
gte:
大于等于
lte:
小于等于
DSL语句-精确匹配多个值进行查询,但不建议查询的值存在中文,因为会导致效果不明显。
1)基于价格匹配多个值
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"terms": {
"price": [
4699,
299,
4066
]
}
}
}
2)基于品牌匹配,不建议使用中文哟
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"terms": {
"brand": [
"dell"
]
}
}
}
DSL语句-多词搜索,了解即可。
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": {
"query": "显示器曲面",
"operator": "and"
}
}
}
]
}
},
"highlight": {
"pre_tags": [
"
温馨提示:
当我们将"operator"设置为"and"则文档必须包含"query"中的所有词汇,"operator"的默认值为"or"。
权重案例:
POST http://10.0.0.103:9200/oldboyedu-shopping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": {
"query": "小苹华"
}
}
}
],
"should": [
{
"match_phrase": {
"title": {
"query": "防水",
"boost": 2
}
}
},
{
"match_phrase": {
"title": {
"query": "黑色",
"boost": 10
}
}
}
]
}
},
"highlight": {
"fields": {
"title": {},
"brand": {}
}
},
"_source": ""
}
温馨提示:
修改"boost"字段的值来提升相应权重。
聚合查询: -分组,terms.
POST http://10.0.0.103:9200/oldboyedu-linux82-shopping/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"auther": "高云飞"
}
}
]
}
},
"aggs": {
"oldboyedu_brand_group": {
"terms": {
"field": "brand.keyword"
}
}
},
"size": 0
}
解释说明:
aggs:
代表的是聚合查询。
oldboyedu_brand_group:
自定义名称,聚合的名称。
terms:
用于分组的匹配查询。
field:
基于哪个字段进行查询。
聚合查询-查询苹果品牌最贵的商品
POST http://10.0.0.103:9200/oldboyedu-shopping/_search # 统计苹果商品中最贵的。
{
"query": {
"match_phrase": {
"brand": "苹果"
}
},
"aggs": {
"oldboyedu_max_shopping": {
"max": {
"field": "price"
}
}
}
}
聚合查询-统计华为商品中最便宜的
POST http://10.0.0.103:9200/oldboyedu-shopping/_search # 统计华为商品中最便宜的。
{
"query": {
"match_phrase": {
"brand": "华为"
}
},
"aggs": {
"oldboyedu_min_shopping": {
"min": {
"field": "price"
}
}
}
}
聚合查询-统计小米商品的平均价格
POST http://10.0.0.103:9200/oldboyedu-shopping/_search # 统计小米商品的平均价格。
{
"query": {
"match_phrase": {
"brand": "小米"
}
},
"aggs": {
"oldboyedu_avg_shopping": {
"avg": {
"field": "price"
}
}
}
}
聚合查询-统计买下小米所有商品的价格
POST http://10.0.0.103:9200/oldboyedu-shopping/_search # 统计买下小米所有商品的价格。
{
"query": {
"match_phrase": {
"brand": "小米"
}
},
"aggs": {
"oldboyedu_sum_shopping": {
"sum": {
"field": "price"
}
}
}
}
当写入文档时,字段的数据类型会被ES动态自动创建,但有的时候动态创建的类型并符合我们的需求。这个时候就可以使用映射解决。
使用映射技术,可以对ES文档的字段类型提前定义我们期望的数据类型,便于后期的处理和搜索。
text:
全文检索,可以被全文匹配,即该字段是可以被拆分的。
keyword:
精确匹配,必须和内容完全匹配,才能被查询出来。
ip:
支持Ipv4和Ipv6,将来可以对该字段类型进行IP地址范围搜索。
参考链接:
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/mapping-types.html
IP案例
PUT http://10.0.0.101:9200/oldboyedu-linux81-elk # 创建索引时指定映射关系
{
"mappings" :{
"properties": {
"ip_addr" : {
"type": "ip"
}
}
}
}
GET http://10.0.0.101:9200/oldboyedu-linux81-elk # 查看索引的映射关系
POST http://10.0.0.101:9200/_bulk # 创建测试数据
{ "create": { "_index": "oldboyedu-linux81-elk"} }
{ "ip_addr": "192.168.10.101" }
{ "create": { "_index": "oldboyedu-linux81-elk"} }
{ "ip_addr": "192.168.10.201" }
{ "create": { "_index": "oldboyedu-linux81-elk"} }
{ "ip_addr": "172.31.10.100" }
{ "create": { "_index": "oldboyedu-linux81-elk"} }
{ "ip_addr": "10.0.0.222" }
GET http://10.0.0.101:9200/oldboyedu-linux81-elk/_search # 查看IP的网段
{
"query": {
"match" : {
"ip_addr": "192.168.0.0/16"
}
}
}
其他数据类型案例:
PUT http://10.0.0.101:9200/oldboyedu-linux82-elk-2022 # 创建索引
GET http://10.0.0.101:9200/oldboyedu-linux82-elk-2022 # 查看索引信息
PUT http://10.0.0.101:9200/oldboyedu-linux82-elk-2022/_mappings # 为已创建的索引修改数据类型
{
"properties": {
"name": {
"type": "text",
"index": true
},
"gender": {
"type": "keyword",
"index": true
},
"telephone": {
"type": "text",
"index": false
},
"address": {
"type": "keyword",
"index": false
},
"email": {
"type": "keyword"
},
"ip_addr": {
"type": "ip"
}
}
}
POST http://10.0.0.101:9200/_bulk # 添加测试数据
{ "create": { "_index": "oldboyedu-linux82-elk-2022"} }
{ "ip_addr": "192.168.10.101" ,"name": "王建平","gender":"男性的","telephone":"33333333","address":"沙河","email":"[email protected]"}
{ "create": { "_index": "oldboyedu-linux82-elk-2022"} }
{ "ip_addr": "192.168.20.21" ,"name": "赵佳佳","gender":"男性的","telephone":"55555","address":"松兰堡","email":"[email protected]"}
{ "create": { "_index": "oldboyedu-linux82-elk-2022"} }
{ "ip_addr": "172.28.30.101" ,"name": "李鹏飞","gender":"女性的","telephone":"33333333","address":"于辛庄","email":"[email protected]"}
{ "create": { "_index": "oldboyedu-linux82-elk-2022"} }
{ "ip_addr": "172.28.50.121" ,"name": "高云飞","gender":"女性的","telephone":"444444444","address":"于辛庄","email":"[email protected]"}
{ "create": { "_index": "oldboyedu-linux82-elk-2022"} }
{ "ip_addr": "10.0.0.67" ,"name": "李明凯","gender":"男性的","telephone":"22222222","address":"松兰堡","email":"[email protected]"}
GET http://10.0.0.101:9200/oldboyedu-linux82-elk-2022/_search # 基于gender字段搜索
{
"query":{
"match":{
"gender": "女"
}
}
}
GET http://10.0.0.101:9200/oldboyedu-linux82-elk-2022/_search # 基于name字段搜索
{
"query":{
"match":{
"name": "王"
}
}
}
GET http://10.0.0.101:9200/oldboyedu-linux82-elk-2022/_search # 基于email字段搜索
{
"query":{
"match":{
"email": "[email protected]"
}
}
}
GET http://10.0.0.101:9200/oldboyedu-linux80-elk-2022/_search # 基于ip_addr字段搜索
{
"query": {
"match" : {
"ip_addr": "192.168.0.0/16"
}
}
}
GET http://10.0.0.101:9200/oldboyedu-linux80-elk-2022/_search # 基于address字段搜索,无法完成。
{
"query":{
"match":{
"address": "松兰堡"
}
}
}
IK分词器安装
1.下载IK分词器
https://github.com/medcl/elasticsearch-analysis-ik
2.安装IK分词器
install -o oldboyedu -g oldboyedu -d /oldboyedu/softwares/elasticsearch-7.17.5/plugins/ik
cd /oldboyedu/softwares/elasticsearch-7.17.5/plugins/ik
wget http://192.168.11.253/day03-/softwares/elasticsearch-analysis-ik-7.17.5.zip
unzip elasticsearch-analysis-ik-7.17.5.zip
rm -f elasticsearch-analysis-ik-7.17.5.zip
chown -R oldboyedu:oldboyedu *
3.同步IK分词器插件
data_rsync.sh /oldboyedu/softwares/elasticsearch-7.17.5/plugins/ik
4.重启ES服务
systemctl restart es7
5.验证,注意IK分词器支持: ik_max_word(细粒度拆分词汇)和ik_smart(粗粒度的拆分)
POST http://10.0.0.101:9200/_analyze
{
"analyzer": "ik_max_word",
"text": "我爱北京天安门!"
}
自定义IK分词器字典
(1)自定义字典
cd /oldboyedu/softwares/elasticsearch-7.17.5/plugins/ik/config/
cat > oldboyedu-linux82.dic <
艾欧尼亚
上号
带你飞
亚索
贼6
EOF
chown oldboyedu:oldboyedu oldboyedu-linux82.dic
(2)加载自定义字典
vim IKAnalyzer.cfg.xml
...
(4)重启ES集群
systemctl restart es
(5)测试分词器
GET http://10.0.0.101:9200/_analyze
{
"analyzer": "ik_smart",
"text": "嗨,哥们! 上号,我德玛西亚和艾欧尼亚都有号! 我亚索贼6,肯定能带你飞!!!"
}
(6)集群其它节点若没有同步词典,则查询依旧是无法获取到自定义的字典信息哟!
data_rsync.sh /oldboyedu/softwares/elasticsearch-7.17.5/plugins/ik # 同步IK分词器插件
今日内容回顾:
(1)DSL语句: 精确匹配,全文检索,排序,过滤,范围查询,高亮,分页,聚合查询,权重案例等等。 ***** 走DBA方向的要掌握。
(2)索引的映射,显示设置字段的类型;
(3)IK中文分词器; *****
(1)昨日作业优化:
1)创建索引并添加映射
PUT http://10.0.0.103:9200/oldboyedu-linux82-homework
{
"mappings": {
"properties": {
"type": {
"type": "keyword"
},
"title": {
"type": "text"
},
"brand": {
"type": "keyword"
},
"price": {
"type": "double"
},
"item": {
"type": "text"
},
"group": {
"type": "byte"
},
"auther": {
"type": "keyword"
},
"remote_ip_addr": {
"type": "ip"
}
}
},
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"aliases": {
"Linux容器运维工程师": {},
"大数据运维工程师": {}
}
}
相关字段说明:
"title":
商品的标题。
"price":
商品的价格。
"brand":
商品的品牌。
"item":
商品的链接。
"group":
组名称
"auther":
您的名字,谁提供的这些数据。
"type":
商品的类型。比如奢侈品,跑车,衣服,零食,成人用品,...
"remote_ip_addr":
商品服务端的IP地址。
2)往索引添加数据
POST http://10.0.0.103:9200/_bulk
{ "create":{"_index":"oldboyedu-linux82-homework" } }
{"type":"Luxury","title":"私人飞机","brand":"庞巴迪挑战者CL850","price":"70000000","item":"http://item.secoo.com/72825351.shtml?source=list","group":"3","auther":"刘东","remote_ip_addr":"47.94.201.216"}
{ "create":{"_index":"oldboyedu-linux82-homework" } }
{"type":"Luxury","title":"私人飞机","brand":"湾流G550","price":172000000.3215644,"item":"http://item.secoo.com/72615491.shtml?source=list","group":3,"auther":"刘东","remote_ip_addr":"219.141.140.10"}
{ "create":{"_index":"oldboyedu-linux82-homework" } }
{"type":"Luxury","title":"梅赛德斯奔驰C260L","brand":"奔驰","price":310000.10,"item":"http://item.secoo.com/72615491.shtml?source=list","group":2,"auther":"刘钢炮","remote_ip_addr":"219.141.136.10"}