Elasticsearch的搭建
简介
Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch 是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。
环境
Elasticsearch是用java开发的,可支持跨平台,Window、Linux、Mac等平台都可以安装,安装前需要先配置好java环境
安装
下载好安装包后,直接打开即可运行
Linux下:./bin/elasticsearch
Windows下:bin\elasticsearch.bat
Windows下可以把安装包.bin路径配置在环境变量中,就可以使用命令行启动elasticsearch了。
例如在系统变量Path中加入我的文件路径:D:\Program Files\elasticsearch-7.5.0\bin
查看是否安装成功
Linux:
curl -X GET http://localhost:9200/
Windows:
浏览器访问 http://localhost:9200/
安装成功则会返回类似以下信息
{
"name": "node-1",
"cluster_name": "my_es_cluster",
"cluster_uuid": "hCKjYSnqSR6he_a2k6jssw",
"version": {
"number": "7.3.0",
"build_flavor": "default",
"build_type": "tar",
"build_hash": "de777fa",
"build_date": "2019-07-24T18:30:11.767338Z",
"build_snapshot": false,
"lucene_version": "8.1.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
简单使用
写入
curl -XPUT 'http://localhost:9200/my_index/_doc/1?pretty' -H 'Content-Type: application/json' -d '
{
"user": "icehill",
"message": "测试数据写入"
}'
curl -XPUT 'http://localhost:9200/my_index/_doc/2?pretty' -H 'Content-Type: application/json' -d '
{
"user": "bita",
"message": "我是中国人"
}'
curl -XPUT 'http://localhost:9200/my_index/_doc/3?pretty' -H 'Content-Type: application/json' -d '
{
"user": "programmer",
"message": "我是一个程序员"
}'
返回示例:
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询
curl -XGET 'http://localhost:9200/my_index/_doc/1?pretty=true'
curl -XGET 'http://localhost:9200/my_index/_doc/2?pretty=true'
curl -XGET 'http://localhost:9200/my_index/_doc/3?pretty=true'
返回示例:
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "icehill",
"message" : "测试数据写入"
}
}
搜索
curl -XGET 'http://localhost:9200/my_index/_search?pretty=true' -H 'Content-Type: application/json' -d '
{
"query" : {
"match" : { "user": "icehill" }
}
}'
返回示例:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"user" : "icehill",
"message" : "测试数据写入"
}
}
]
}
}