Elasticsearch安装和简单使用

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,一个超好用的可视化插件

  1. chrome商店里面直接搜 elasticsearch-head 安装就行
  2. 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"
    }
  }
}

image-20200705145250003

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值