Windows安装ELK,ElasticSearch查询操作

简介

ELK是ElasticSearch ,Logstash,Kibana的缩写

下载

elastic的官网打不开,推荐使用国内镜像,如华为云:华为云镜像地址

ElasticSearch的安装

  1. 找到想要下载的版本点击下载,配置conf文件下的elasticsearch.yml的端口和跨域,加入下配置:
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"

在bin文件夹下的地址栏输入CMD,指令窗口输入:elasticsearch.bat 启动

  1. 将elasticsearch加入到windows服务
安装服务
elasticsearch-service.bat install
当failed to .. 执行移除服务
elasticsearch-service.bat remove
或者win+R输入regedit打开注册表找到如下,删除后重启
计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\elasticsearch-service-x64

由于java版本的问题,一直安装服务失败或者是安装服务无法启动,找到解决方案如下:

将elasticsearch-env.bat里面的if(JAVA_HOME)…else代码块替换为如下:

set JAVA="%ES_HOME%\jdk\bin\java.exe"
set JAVA_HOME="%ES_HOME%\jdk"
set JAVA_TYPE=bundled jdk

替换完毕再次执行elasticsearch-service.bat install后,即可正常启动windows服务。浏览器输入localhost:9200进行测试。

Kibana和Logstash

  1. 控制台启动
    在bin文件夹下的地址栏输入CMD,指令窗口输入:kibana.bat /logstash.bat启动
  2. 加入windows服务自启动
    下载NSSM,官网地址
    在下载的文件夹的exe文件夹打开CMD,输入
nssm install kibana
nssm install logstash

logstash的服务安装不同之处在于Argument选项填入logstash/config/logstash-sample.conf,其他相似。

ElasticSearch的简单DSL语言查询

索引名称:test

1.match、match_all、match_parse、match_parase_prefix和term、query_string的区别对比

match:模糊匹配,需要指定字段名,但是输入会进行分词,是一个部分匹配的模糊查询。查询条件相对来说比较宽松。
match_all:查询所有
match_parse:会对输入做分词,但是需要结果中也包含所有的分词,而且顺序要求一样
match_parse_prefix:查询某字段以什么为前缀
term:查询时不会进行分词,会查找是否有查询条件的字样。
query_string:和match类似,但是match需要指定字段名,query_string是在所有字段中搜索,范围更广泛。

一、query查询

1.查询所有的索引

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

2.查询test索引下所有文档

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

3.带分页和根据number排序的查询

GET /test/_search
{
    
    
  "query": {
    
    
    "match_all": {
    
    }
  },
  "sort": [
    {
    
    
      "number": {
    
    
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 20
}

4.带条件和分页排序的查询,sort排序,order倒序顺序

GET /test/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "email": "abc.com"
    }
  },
  "sort": [
    {
    
    
      "number": {
    
    
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 20
}

5.查询balance范围,filter筛选条件,range范围

GET /test/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": 
        {
    
    
          "match_all": {
    
    }
        }, 
		"filter": [
          {
    
    "range": {
    
    
            "balance": {
    
    
              "gte": 1000,
              "lte": 2000
            }
          }}
        ]
    }
  }
}

6.分组并计算统计,group_by_state分组,aggs用来指定聚合条件,size返回的命中结果中具体结果数组显示0个

GET /test/_search
{
    
    
  "size": 0,
  "aggs": {
    
    
    "group_by_state": {
    
    
      "terms": {
    
    
        "field": "city.keyword"
      },
      "aggs": {
    
    
        "平均工资": {
    
    
          "sum": {
    
    
            "field": "balance"
          }
        }
      }
    }
  }
}

7.term进行字段精确值查询

GET /test/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "email": {
    
    
        "value": "[email protected]"
      }
    }
  }
}

8.query_string的使用,可设置默认查询字段

GET /test/_search
{
    
    
  "query": {
    
    
    "query_string": {
    
    
      "default_field": "email",
      "query": "[email protected]"
    }
  }
}

9.match_parse_prefix的使用,查询以ab前缀的email字段

GET /test/_search
{
    
    
  "query": {
    
    
    "match_phrase_prefix": {
    
    
      "email": "am"
    }
  }
}

二、query下的bool查询

bool下有must,must_not,should必须,必须不,应该。

1.多条件查询,注意match等条件外面的{}

GET /test/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "email": "[email protected]"
          }
        },
        {
    
    
          "match_phrase": {
    
    
            "lastname": "Duke"
          }
        }
      ]
    }
  }
}

2.must、must_not、should的同时使用

GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "balance": "39225"
          }
        }
      ],
      "must_not": [
        {
    
    
          "match": {
    
    
            "firstname": "cccc"
          }
        }
      ],
      "should": [
        {
    
    
          "match": {
    
    
            "age": "3"
          }
        }
      ]
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_41941497/article/details/127984561