Elasticsearch的搭建

Elasticsearch的搭建

简介

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch 是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。

环境

Elasticsearch是用java开发的,可支持跨平台,Window、Linux、Mac等平台都可以安装,安装前需要先配置好java环境

Github地址

中文下载地址

安装

下载好安装包后,直接打开即可运行

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" : "测试数据写入"
        }
      }
    ]
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值