ElasticSearch的安装及Kibana5的使用

1为什么要使用ElasticSearch

简单好用底层封装了Lucene以及支持其他语言的使用,操作简单,支持分布式实时搜索引擎,扩展服务器处理PB级别非结构化数据及结构化数据

2安装

ES服务只依赖于JDK,推荐使用JDK1.7+

官方下载地址:https://www.elastic.co/downloads/elasticsearch

 3修改

可以修改配置文件中的conf中的jvm.options文件

 

 为你电脑内存分配的大小,但是不能够太小,如果电脑不太行就512m可以了

找到bin中的 

 点击即可

只能够使用Restful风格

Restful的典型特征:

² Server提供的RESTful API中,URL中只使用名词来指定资源

“资源”是REST架构或者说整个网络处理的核心。比如:

GET http://api.itsource.cn/emp/323: 获取323号员工的基本资料;

GET http://api.itsource.cn/emps: 获取源码时代所有员工资料列表;

² REST 是面向资源的,这个概念非常重要,而资源是通过 URI 进行暴露

URI 的设计只要负责把资源通过合理方式暴露出来就可以了。对资源的操作与它无关,所以REST 通过 URI 暴露资源时,会强调不要在 URI 中出现动词。

比如:左边是错误的设计,而右边是正确的

GET /rest/api/getDogs -> GET /rest/api/dogs 获取所有小狗狗

GET /rest/api/getDogById?id=1 -> GET /rest/api/dog/1 获取所有小狗狗

GET /rest/api/addDogs -> POST /rest/api/dogs 添加一个小狗狗

POST /rest/api/editDogs/12 -> PUT /rest/api/dogs/12 修改一个小狗狗

POST /rest/api/deleteDogs/12 ->DELETE /rest/api/dogs/12 删除一个小狗

左边的这种设计,很明显不符合REST风格,URI 只负责准确无误的暴露资源,而 getDogs/addDogs...已经包含了对资源的操作,这是不对的。相反右边却满足了,它的操作是使用标准的HTTP动词来体现。

③ 用HTTP协议里的动词来实现资源的添加,修改,删除等操作。

即通过HTTP动词来实现资源的状态扭转:

GET 用来获取资源,

POST 用来新建资源(也可以用于更新资源),

PUT 用来更新资源,

DELETE 用来删除资源。

比如:

GET http://api.itsource.cn/emp/323

POST http://api.itsource.cn/emp/232: 修改一个员工

PUT http://api.itsource.cn/emp: 添加员工资料

DELETE http://api.itsource.cn/emp/323: 删除323号员工

2 Kibana5

1安装

https://www.elastic.co/downloads/kibana

可视化界面找到bin中的bat文件和ES的使用一样的点击就可以了,不过这里不能狗点击ES的文件,而且必须需要先打开ES并成功运行

访问localhost:5601即可到

 点击Dev Tools文件

2.简单的使用crud

GET _search


#添加员工如果没有就添加如果有就修改
PUT /pethome/employee/1
{
  "id":"1",
  "username":"jiedada",
  "password":"123456"
}

#获取员工对象
GET /pethome/employee/1

#修改员工对象
PUT /pethome/employee/1
{
  "id":"1",
  "username":"jiedada",
  "password":"123456",
  "sex":true
}

#修改部分内容
POST /pethome/employee/1/_update
{
  "doc": {
     "id":"1",
  "username":"jie"
  }
}

#删除内容
DELETE /pethome/employee/1

#批量操作
POST _bulk
{ "delete": { "_index": "itsource", "_type": "employee", "_id": "123" }}
{ "create": { "_index": "itsource", "_type": "blog", "_id": "123" }}
{ "title": "我发布的博客" }
{ "index": { "_index": "itsource", "_type": "blog" }}
{ "title": "我的第二博客" }

#删除全部
DELETE _all

GET /itsource/blog/123

#方式1:GET _mget获得不同index中的多个
GET _mget 
{
  "docs" : [
    {
      "_index" : "itsource",
      "_type" : "blog",
      "_id" : 2
    },
    {
      "_index" : "itsource",
      "_type" : "employee",
      "_id" : 1,
      "_source": "email,age"
    }
  ]
}

#查询多个
GET itsource/blog/_mget
{
  "ids" : [ "2", "1" ]
}

#空搜索
GET _search

#分页查询size分页的大小,from从多少条开始(从0开始)
GET _search?size=2&from=0

#分页查询size分页的大小,from从多少条开始(从0开始)
GET _search?size=2&from=0
View Code

3.DSL查询与过滤

#DSL查询与过滤

PUT /pethome/employee/8
{
  "id":"1",
  "username":"jieaa",
  "password":"123456",
  "age":16
}
#多查询的另一种方式match的意思就是分词查询还可以使用term不分词
GET pethome/employee/_search
{
  "query" : {
     "term" : { 
             "username" : "jie"
    }
  }
}

#通配符查询
GET pethome/employee/_search
{
  "query": {
      "wildcard" : { 
          "name" : "*j*" }
      }
}

#完整的查询带有分页排序_source我们需要的数据type,sort排序方式
GET pethome/employee/_search
{
  "query": {
     "match": {
       "username": "jie"
     }
  },
  "from": 0, 
  "size": 5,
  "_source": ["username", "age", "password"],
  "sort": {"age": "desc"}
}

#查询和过滤bool表示先过滤后查询,term表示不分词精确匹配
GET pethome/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "username": "jie"
          }
        }
      ],
      "filter": {
        "term": {
          "password": "123456"
        }
      }
    }
  }, 
  "from": 0, 
  "size": 5,
  "_source": ["username", "age", "password"],
  "sort": {"age": "desc"}
}

#查询和过滤bool表示先过滤后查询,range表示范围过滤gt表示大于gte大于等于lt小于lte表示小于等于
GET pethome/employee/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "username": "jie"
          }
        }
      ],
      "filter": {
        "range": {
          "age":{
            "lt":15
          }
        }
      }
    }
  }, 
  "from": 0, 
  "size": 5,
  "_source": ["username", "age", "password"],
  "sort": {"age": "desc"}
}
View Code

4.简单映射

#简单映射properties属性值analyzer分词器类型search_analyzer查询使用的分词器type文本text分词并且保存到数据区
POST pethome/employee/_mapping
{
  "employee":{
    "properties":{
      "id":{
        "type":"text"
      },
      "username":{
         "type": "text",
          "analyzer": "ik_smart",
          "search_analyzer":"ik_smart"
      }
    }
  }
}
View Code
原文地址:https://www.cnblogs.com/xiaoruirui/p/13606729.html