欢迎访问宙启技术站
智能推送

Linux系统:Centos7下搭建ElasticSearch中间件,常用接口演示

发布时间:2023-05-17 22:16:01

ElasticSearch是一款基于Apache Lucene的全文搜索引擎,是实时分布式搜索和分析引擎,具有良好的扩展性、容错性和高可用性,在大数据处理、日志分析、全文搜索等领域非常流行。

本文以Centos7系统为例,介绍如何搭建ElasticSearch中间件,并介绍常用的接口演示。

一、搭建ElasticSearch中间件

1.安装java环境

ElasticSearch需要Java运行环境,首先需要安装Java。可以使用命令yum search java | grep -i --color JDK安装Java。

2.下载并安装ElasticSearch

可以从ElasticSearch官网下载最新的版本,也可以使用命令wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-x86_64.rpm下载ElasticSearch安装包。

下载完成后,使用命令rpm -ivh elasticsearch-7.6.0-x86_64.rpm进行安装。

3.启动ElasticSearch

使用命令systemctl start elasticsearch.service启动ElasticSearch服务,使用命令systemctl enable elasticsearch.service设置开机自启。

4.检测ElasticSearch是否正常启动

使用命令curl http://localhost:9200/检测ElasticSearch是否正常启动。如果看到类似下面的输出,则说明ElasticSearch已经正常启动:

{

  "name" : "centos",

  "cluster_name" : "elasticsearch",

  "cluster_uuid" : "a1I9nhBtTq6inOjeg7AJcw",

  "version" : {

    "number" : "7.6.0",

    "build_flavor" : "default",

    "build_type" : "rpm",

    "build_hash" : "7f634e9f44834fbc12724506cc1da681b0c3b1e3",

    "build_date" : "2020-02-06T00:09:00.449973Z",

    "build_snapshot" : false,

    "lucene_version" : "8.4.0",

    "minimum_wire_compatibility_version" : "6.8.0",

    "minimum_index_compatibility_version" : "6.0.0-beta1"

  },

  "tagline" : "You Know, for Search"

}

二、常用接口演示

1.创建索引

使用PUT命令创建索引:

curl -XPUT http://localhost:9200/test_index

其中test_index是索引的名称。

2.添加文档

使用PUT命令添加文档:

curl -XPUT http://localhost:9200/test_index/_doc/1 -d '{"title":"test","content":"this is a test"}'

其中test_index是索引的名称,_doc是文档类型,1是文档ID,-d后面的是文档内容。

3.查询文档

使用GET命令查询文档:

curl -XGET http://localhost:9200/test_index/_doc/1

其中test_index是索引的名称,_doc是文档类型,1是文档ID。

4.搜索文档

使用POST命令搜索文档:

curl -XPOST http://localhost:9200/test_index/_search -d '{"query":{"match":{"content":"test"}}}'

其中test_index是索引的名称,_search表示搜索操作,-d后面的是搜索条件。

5.删除文档

使用DELETE命令删除文档:

curl -XDELETE http://localhost:9200/test_index/_doc/1

其中test_index是索引的名称,_doc是文档类型,1是文档ID。

6.删除索引

使用DELETE命令删除索引:

curl -XDELETE http://localhost:9200/test_index

其中test_index是索引的名称。

以上是ElasticSearch的常用接口演示,开发人员可以根据实际需求使用不同的接口操作数据。