Elasticsearch使用记录
ELK
Elasticsearch Logstash Kibana搭建
es解决文档搜索问题
kibana进行可视化查询、增加
安装
Mac安装es和kibana
打开终端输入
brew install elasticsearch
brew isntall kibana
启动:先启动es,再启动kibana
es和kibana的启动都是直接输入名称
elasticsearch 启动es http://localhost:9200 默认端口9200
kibana 启动kibana , 启动后登陆 http://localhost:5601 默认端口5601
安装es-head,一个超好用的可视化插件
- chrome商店里面直接搜
elasticsearch-head
安装就行 - es安装es-head
2. 基础操作
2.1 索引的操作
//创建索引
put indexName
//查询索引
get /indexName
2.2 添加数据
put indexName/_doc
{
"question":"什么是机器人",
"answer":"机器人"
}
批量添加 https://blog.csdn.net/JiKaTogether/article/details/90769747
2.3 mapping
注意Mappin一旦设置好,就只能加字段,但是不能修改,一经修改,索引就会重建
PUT website
{
"mappings": {
"user": { // 这就是一个root object
"_all": { "enabled": false }, // 禁用_all字段
"properties": {
"user_id": { "type": "text" },
"name": {
"type": "text",
"analyzer": "english"
},
"age": { "type": "integer" },
"sex": { "type": "keyword" },
"birthday": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"address": {
"type": "text",
"index": false // 不分词
}
}
}
}
}
2.4 插入数据
2.5 查询数据
//这种是精确匹配
GET session_index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "session_event",
"query": {
"bool": {
"must": [
{"match": {
"session_event.message": "第一聊天消息"
}}
]
}
}
}
}
]
}
}
}
https://blog.csdn.net/pony_maggie/article/details/105126342 看下这篇文章
2.5.1 嵌套查询使用示例
# 获取索引mapping
get session_index/_mapping
# 添加映射,映射一旦添加 不能修改,只能增字段
PUT session_index/_mapping/session
{
"properties": {
"id":{
"type": "keyword"
},
"channel":{
"type": "keyword"
},
"skill_group_id":{
"type": "keyword"
},
"user_id":{
"type": "keyword"
}
}
}
# 查下type keyword和string的区别
PUT session_index/_mapping/session
{
"properties": {
"session_event":{
"type": "nested",
"properties": {
"id":{"type":"keyword"},
"user_id":{"type":"keyword"},
"message":{"type":"keyword"}
}
}
}
}
update session_index/session
{
"id":"2",
"channel":"hz_h5",
"user_id":"666666",
"skill_group_id":"15",
"session_event":[
{
"id":"2-1",
"user_id":"666666",
"message":"第三个聊天消息"
},
{
"id":"2-2",
"user_id":"666666",
"message":"第二个聊天消息"
}
]
}
}
# 精确匹配
GET session_index/_search
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "session_event",
"query": {
"bool": {
"must": [
{"match": {
"session_event.message": "第一个聊天消息"
}}
]
}
}
}
}
]
}
}
}
# 试一下模糊匹配
GET session_index/_search
{
"query": {
"match_phrase": {
"FIELD": "PHRASE"
}
}
}