简略学习elasticsearch

1.es安装部署(7.15.1)

较简单,简略流程

1.创建es用户(linux)

2.使用es用户解压安装es官方文件包

https://www.elastic.co/cn/start

3.修改配置文件

4.启动es;后台运行添加-d 参数

# .\elasticsearch -d

配置文件:elasticsearch.yml

# 集群名称
cluster.name: ruby
# 节点名称,各节点不同
node.name: hdp22
path.data: /data/es/data
path.logs: /data/es/log
# 绑定的ip,0.0.0.0 表示任一ip可访问9200端口
network.bind_host: 0.0.0.0
# 和其他节点传输数据的ip
network.publish_host: 192.168.1.22
# 种子节点 包含在种子节点内的ip会纳入集群
discovery.seed_hosts: ["hdp21", "hdp22","hdp23"]
# 初始的master候选节点
cluster.initial_master_nodes: ["hdp21", "hdp22","hdp23"]

出现问题:
文件打开限制与最大进程限制不足,配置增加就行
参考
https://www.cnblogs.com/zhi-leaf/p/8484337.html

2.kibana安装部署

kibana可看作es的web界面,不用那么麻烦的弄curl去访问

1.在任意一台节点下解压官方的安装包

https://www.elastic.co/cn/start

2.修改配置文件

3.打开http://hdp22:5601/app/home#/

配置文件:

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://hdp22:9200"]
i18n.locale: "zh-CN"

3.添加中文分词器ik

https://github.com/medcl/elasticsearch-analysis-ik
将分词器文件包解压到 your-es-root/plugins/ik 路径下 重启es集群
参考
https://blog.csdn.net/xsdxs/article/details/72853288
https://blog.csdn.net/qq_26803795/article/details/106522611

4.创建第一个索引

PUT /danmaku
{
	"settings": {
		"number_of_shards": 3,
		"number_of_replicas": 2
	},
	"mappings": {
		"properties": {
			"room_id":{"type":"keyword"},
      "uname": {"type":"text","analyzer":"ik_max_word","search_analyzer": "ik_smart"},
      "uid": {"type":"keyword"},
      "msg": {"type":"text","analyzer":"ik_max_word","search_analyzer": "ik_smart"},
      "msg_type": {"type":"keyword"}
		}
	}
}

返回

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "danmaku"
}

5.添加文档

使用POST 添加(PUT方法不用输入_id,但不幂等)

POST /danmaku/_doc/22047448_58***68_1634628033810
{
  "room_id":"22047448",
  "uname":"RAmenL",
  "uid":"58***68",
  "msg":"sio酱 斯哈斯哈",
  "msg_type":"0"
}
POST /danmaku/_doc/22047448_58***68_1634628033815
{
  "room_id":"22047448",
  "uname":"RAmenL",
  "uid":"58***68",
  "msg":"sio酱 昆卡昆卡",
  "msg_type":"0"
}
POST /danmaku/_doc/22047448_58***68_1634628033820
{
  "room_id":"22047448",
  "uname":"RAmen_L",
  "uid":"58***68",
  "msg":"苦呀汐",
  "msg_type":"0"
}

6.查询

通过id查询

GET /danmaku/_doc/22047448_58***68_163462803381

指定字段查询

GET /danmaku/_search
{
  "query": {
    "match": {
      "msg": "sio"
    }
  }
}

多词查询

如果使用两个词会出现相关度的不同

GET /danmaku/_search
{
  "query": {
    "match": {
      "msg": "sio昆卡"
    }
  }
}

和下方类似

GET /danmaku/_search
{
  "query":{
    "bool":{
      "should":[
          {"match":{"msg":"sio"}},
          {"match":{"msg":"昆卡"}}
        ]
    }
  }
}

组合查询

使用bool过滤器,should表示可以有,但不一定要有,但有的话更相关

GET /danmaku/_search
{
  "query": {
    "bool": {
        "must":{
          "match":{"uname":"三摩氟锑酸"}
        },
        "must_not":{
          "match":{"msg":"昆卡"}
        },
        "should": [
          {"match":{"msg":"斯哈斯哈"}},
          {"match":{"msg":"汐"}}
        ]
    }
  }
}

加权查询

GET /danmaku/_search
{
  "query": {
    "bool": {
        "must":{
          "match":{"uname":"三摩氟锑酸"}
        },
        "must_not":{
          "match":{"msg":"昆卡"}
        },
        "should": [
          {"match":{"msg":{"query":"斯哈斯哈","boost":1}}},
          {"match":{"msg":{"query":"汐","boost":7}}}
        ]
    }
  }
}

7.查询集群状态

查询节点

# GET /_cat/nodes
192.168.1.21 22 94 10 0.83 0.75 0.81 cdfhilmrstw - hdp21
192.168.1.23 22 96 21 1.47 1.46 1.40 cdfhilmrstw * hdp23
192.168.1.22 18 97 27 2.02 1.77 1.95 cdfhilmrstw - hdp22

查询健康状态

# GET /_cat/health?v
epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1634630862 08:07:42  ruby    green           3         3     29  13    0    0        0             0                  -                100.0%

查看索引

# GET /_cat/indices?v
health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   danmaku                         TjjOytQPSCayeM54XT-NFg   3   2          3            0     45.4kb         15.1kb

参考资料

https://www.elastic.co/guide/cn/elasticsearch/guide/current/search-in-depth.html

posted @ 2023-04-24 14:12  RAmenLCH  阅读(18)  评论(0)    收藏  举报