Elasticsearch的安装与简单使用

戏子无情 提交于 2020-03-08 20:19:54

一、安装

Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。

(一)Elasticsearch下载与安装

1、Elasticsearch的下载

官网下载有时太慢,这里分享一个链接:链接:https://pan.baidu.com/s/1u-3M4yr7zTjioYQZQKCeYQ 提取码:vfr3

 下载完成后进行解压,进入到如下目录:

 可以看到里面有一个jdk的文件夹,所以你先需要将jdk的环境配置好:

2、配置jdk环境

  • JAVA_HOME配置

在系统环境变量中配置JAVA_HOME变量

  • 配置classpath
.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar

 

  •  编辑Path
%JAVA_HOME%\bin\;%JAVA_HOME%\jre\bin;

  • 测试是否安装成功

3、启动Elasticsearch

可以进入到其bin目录下,点击elasticsearch.bat直接运行:

 但是不出意外的话,应该很大可能失败,此时我们需要看看错误信息,需要在cmd串口中进行运行,错误信息有以下情况:

  •  错误一
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in ve
rsion 9.0 and will likely be removed in a future release.

此时,我们需要修改elasticsearch目录下的congfig目录下的jvm.options文件:

 将-XX:+UseConcMarkSweepGC修改为-XX:+UseG1GC即可。

  • 错误二
Error occurred during initialization of VM
Initial heap size set to a larger value than the maximum heap size

此时我们还是修改上述配置文件,只需要将堆容量设置的小一些就可以了:

 这里我将之前的1G修改为500M,然后保存文件并重新启动就ok了,成功启动后我们可以进行验证,访问9200端口:

其详细配置信息位于config目录下的elasticsearch.yml文件中。

(二)kibana的下载与安装

  kibana是elasticsearch的web版客户端,我们可以通过它来连接已经启动的elasticsearch,这样操作起来就会更方便,注意的是kibana的版本必须与elasticsearch的版本保持一致,这里提供了资源地址:

链接:https://pan.baidu.com/s/1sBaGUzxYxe5nX8SKPK9AKQ
提取码:6ba4
下载完成并且进行解压,然后进入到bin目录下:

直接点击kibana.bat就会自动启动运行,它会自动连接elasticsearch服务器,我们通过默认端口5601可以进行访问:

kibana的详细配置在其config目录的kibana.yml文件中,包括连接elasticsearch的地址等信息。

二、简单使用

(一)cued操作

1、增加数据

#格式:

PUT 索引名称/类型/文档id
{
"name":"zhangsan",
"age":23
}

例如:

PUT crm/user/1
{
  "name":"zhangsan",
  "age":23
}

执行结果为:

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the 
typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
  "_index" : "crm",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
View Code

2、删除数据

  • 删除指定文档
#格式:

DELETE 索引名称/类型名称/文档id

例如:

DELETE crm/user/1

执行结果:

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{
  "_index" : "crm",
  "_type" : "user",
  "_id" : "1",
  "_version" : 2,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
View Code
  • 删除索引
# 格式

DELETE 索引名称

例如:

DELETE crm

执行结果为:

{
  "acknowledged" : true
}
View Code

3、更新数据

# 格式

POST 索引名称/类型/文档id/_update

{
    "doc":{"age":25}  
}

例如:

POST crm/user/1/_update
{
  "doc":{
    "age":25
  }
}

执行结果:

#! Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
{
  "_index" : "crm",
  "_type" : "user",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
View Code

4、查询

 查询有两种方式,其一为参数查询(query string),其二为结构化查询(DSL)。

  • query string
# 格式

GET 索引名/类型/_search?q=age:25

实例:

GET crm/user/_search?q=age:25

执行结果为:

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "crm",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 25
        }
      }
    ]
  }
}
View Code
  • DSL
# 格式

GET 索引名/类型/_search

{
  “query”:{
    "match":{"age":25}
    }  
}

实例:

GET crm/user/_search
{
  "query":{
    "match":{"age":25}
  }
}

执行结果:

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "crm",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 25
        }
      }
    ]
  }
}
View Code

(二)通用查询操作

1、判断索引是否存在

# 格式

HEAD 索引名称

例如:

#输入
HEAD crm

#输出
200 - OK

2、查询所有索引

#命令

GET _cat/indices

例如:

#输入
GET _cat/indices

#输出
yellow open crm                  Z2cstTIpSvi0nmjHSgMjYA 1 1 1 0  8.3kb  8.3kb
green  open .kibana_1            2MWTky02QrqSJLVtfPD9aA 1 0 4 0 17.5kb 17.5kb
green  open .kibana_task_manager Yw1dT1Q_RtyqW2huAUkROQ 1 0 2 0 45.4kb 45.4kb

3、查询所有内容

# 命令

GET _search
{
"query": {
"match_all": {}
    }
}

例如:

GET _search
{
  "query": {
    "match_all": {}
  }
}

输出:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : ".kibana_1",
        "_type" : "_doc",
        "_id" : "space:default",
        "_score" : 1.0,
        "_source" : {
          "space" : {
            "name" : "Default",
            "description" : "This is your default space!",
            "color" : "#00bfb3",
            "_reserved" : true
          },
          "type" : "space",
          "references" : [ ],
          "updated_at" : "2020-01-09T15:21:14.811Z"
        }
      },
      {
        "_index" : ".kibana_1",
        "_type" : "_doc",
        "_id" : "config:7.0.0",
        "_score" : 1.0,
        "_source" : {
          "config" : {
            "buildNum" : 23117
          },
          "type" : "config",
          "references" : [ ],
          "updated_at" : "2020-01-09T15:21:41.656Z"
        }
      },
      {
        "_index" : ".kibana_1",
        "_type" : "_doc",
        "_id" : "telemetry:telemetry",
        "_score" : 1.0,
        "_source" : {
          "telemetry" : {
            "enabled" : false
          },
          "type" : "telemetry",
          "references" : [ ],
          "updated_at" : "2020-01-09T15:29:26.961Z"
        }
      },
      {
        "_index" : ".kibana_1",
        "_type" : "_doc",
        "_id" : "maps-telemetry:maps-telemetry",
        "_score" : 1.0,
        "_source" : {
          "maps-telemetry" : {
            "mapsTotalCount" : 0,
            "timeCaptured" : "2020-01-11T11:51:14.760Z",
            "attributesPerMap" : {
              "dataSourcesCount" : {
                "min" : 0,
                "max" : 0,
                "avg" : 0
              },
              "layersCount" : {
                "min" : 0,
                "max" : 0,
                "avg" : 0
              },
              "layerTypesCount" : { },
              "emsVectorLayersCount" : { }
            }
          },
          "type" : "maps-telemetry",
          "references" : [ ],
          "updated_at" : "2020-01-11T11:51:14.761Z"
        }
      },
      {
        "_index" : ".kibana_task_manager",
        "_type" : "_doc",
        "_id" : "Maps-maps_telemetry",
        "_score" : 1.0,
        "_source" : {
          "type" : "task",
          "task" : {
            "taskType" : "maps_telemetry",
            "state" : """{"runs":1,"stats":{"mapsTotalCount":0,"timeCaptured":"2020-01-11T11:51:14.760Z","attributesPerMap":{"dataSourcesCount":{"min":0,"max":0,"avg":0},"layersCount":{"min":0,"max":0,"avg":0},"layerTypesCount":{},"emsVectorLayersCount":{}}}}""",
            "params" : "{}",
            "attempts" : 0,
            "scheduledAt" : "2020-01-11T11:51:13.743Z",
            "runAt" : "2020-01-11T16:00:00.000Z",
            "status" : "idle"
          },
          "kibana" : {
            "uuid" : "68f24dc0-cb43-430d-b601-005e146852c8",
            "version" : 7000099,
            "apiVersion" : 1
          }
        }
      },
      {
        "_index" : ".kibana_task_manager",
        "_type" : "_doc",
        "_id" : "oss_telemetry-vis_telemetry",
        "_score" : 1.0,
        "_source" : {
          "type" : "task",
          "task" : {
            "taskType" : "vis_telemetry",
            "state" : """{"runs":1}""",
            "params" : "{}",
            "attempts" : 1,
            "scheduledAt" : "2020-01-11T11:51:14.185Z",
            "runAt" : "2020-01-11T16:00:00.000Z",
            "status" : "idle"
          },
          "kibana" : {
            "uuid" : "68f24dc0-cb43-430d-b601-005e146852c8",
            "version" : 7000099,
            "apiVersion" : 1
          }
        }
      },
      {
        "_index" : "crm",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "age" : 25
        }
      }
    ]
  }
}
View Code 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!