windows下安装elasticsearch教程

windows下安装elasticsearch

Posted by MaxBear 3 years ago

1. 安装jdk

ElasticSearch是基于lucence开发的,需要java jdk支持。所以要先安装JAVA环境。

由于ElasticSearch 5.x 往后依赖于JDK 1.8的,所以现在我们下载JDK 1.8或者更高版本。 下载JDK1.8,下载完成后安装,因为我本地已安装过了java环境这里就不演示了。

2. 安装elasticsearch

因为后面会用的中文分词,我们直接使用别人打包好的。

点击链接下载zip包(建议使用zip包,文件较大) https://github.com/medcl/elasticsearch-rtf

或者使用git命令

git clone http://github.com/medcl/elasticsearch-rtf.git

启动elasticsearch cd es文件目录 执行bin\elasticsearch.bat

启动

浏览器访问 http://localhost:9200/

浏览

3. 安装Elasticsearch-Head

https://github.com/mobz/elasticsearch-head 下载ZIP包。 或者使用git命令git clone http://github.com/mobz/elasticsearch-head.git

注意:这里得先安装node环境哦。

cd elasticsearch-head-master目录 执行npm install 如果你使用的的cnpm,那这里就用cnpm install

install

npm run start

start

浏览器访问 http://localhost:9100

浏览

这里可以看到显示未连接,我们点击一下连接没反应。这里怎么回事呢? F12打开浏览器调试模式,console里面有报错

Access to XMLHttpRequest at 'http://localhost:9200/' from origin 'http://localhost:9100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

跨域 提示跨域了。

这里我们修改一下ES配置文件config/elsaticsearch.yml 在最下面增加

http.cors.allow-origin: "*"
http.cors.enabled: true

配置

然后重启一下ES cd elasticsearch-head-master目录 执行 bin\elasticsearch.bat

然后刷新一下页面 http://localhost:9100/ 这里看下集群健康显示绿色的,这样就表示正常了。 正常

4. 测试一下

创建索引

curl -XPUT "http://127.0.0.1:9200/consumer?pretty"

索引

写入文档

curl -XPOST 'http://127.0.0.1:9200/consumer/user/1?pretty' -H 'Content-Type: application/json' -d '{"name": "shhengongzi","age":22,"sex":1}'

windows下面使用

curl -XPOST "http://127.0.0.1:9200/consumer/user/1?pretty" -H "Content-Type: application/json" -d "{"""name""":"""shhengongzi""","""age""":22,"""sex""":1}"

写入

插入多条数据之后我们做刷选 主界面

查询 查询性别为2的数据

curl -XGET http://127.0.0.1:9200/consumer/user/_search?q=sex="2"

windows使用命令

curl -XGET "http://127.0.0.1:9200/consumer/user/_search?pretty" -d "{"query":{"bool":{"must":[{"term":{"sex":"2"}}]}}}"

也可以浏览器访问 http://127.0.0.1:9200/consumer/user/_search?q=sex=2&pretty=true

查询2

ES-head查询

原始数据

查询结果