ElasticSearch--安装及Restful API使用

Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎。Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。想要使用它,你必须使用Java来作为开发语言并将其直接集成到你的应用中,更糟糕的是,Lucene非常复杂,你需要深入了解检索的相关知识来理解它是如何工作的。Elasticsearch使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。主要概念如下:

   index ==》索引 ==》Mysql中的一个库,库里面可以建立很多表,存储不同类型的数据,而表在ES中就是type。

   type ==》类型 ==》相当于Mysql中的一张表,存储json类型的数据

   document  ==》文档 ==》一个文档相当于Mysql一行的数据

   field ==》列 ==》相当于mysql中的列,也就是一个属性

使用Windows环境来安装,参考地址:

https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html

使用单机部署形式,下载压缩包,解压,修改配置文件config/elasticsearch.yml

# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: es
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.

配置集群名字以及开放外部访问端口,否则es只支持127.0.0.1IP访问

启动es .\bin\elasticsearch.bat

启动成功http://127.0.0.1:9200/

                                            

es支持restful风格API以及各种客户端连接,在此首先使用http来访问,其次使用java client来访问。

Resutful API

链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-cluster-health.html

查看集群状态,查看节点状态

http://127.0.0.1:9200/_cat/health?v

                                              

http://127.0.0.1:9200/_cat/nodes?v

                                           

RESTful接口URL的格式:
http://127.0.0.1:9200/<index>/<type>/[<id>]
其中index、type是必须提供的。id是可选的,不提供es会自动生成。
index、type将信息进行分层,利于管理。index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。

下面使用curl来模拟http  get put update  delete请求,需要安装curl

https://www.cnblogs.com/zhuzhenwei918/p/6781314.html

可以使用curl命令来进行一下增删改查操作

查看索引信息

http://127.0.0.1:9200/_cat/indices?v

添加操作

#向store索引中添加一些书籍
curl -XPUT 'http://127.0.0.1:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2015-02-06",
  "price":"49.99"
}'

查询操作

curl -XGET 'http://192.168.10.18:9200/store/books/1'

查询指定字段

# 通过_source获取指定的字段
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source=title'
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source=title,price'
curl -XGET 'http://192.168.10.16:9200/store/books/1?_source'

更新操作

#可以通过覆盖的方式更新
curl -XPUT 'http://192.168.10.16:9200/store/books/1' -d '{
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2016-02-06",
  "price":"99.99"
}'

# 或者通过 _update  API的方式单独更新你想要更新的
curl -XPOST 'http://192.168.10.16:9200/store/books/1/_update' -d '{
  "doc": {
     "price" : 88.88
  }
}'

删除操作

#删除一个文档
curl -XDELETE 'http://192.168.10.16:9200/store/books/1'

其余各种复杂查询可以参考API

https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html

https://es.xiaoleilu.com/030_Data/10_Index.html

接下来研究一下Java API,SQL以及JDBC连接等。

JAVA API

链接: https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.3/java-search-terminate-after.html

SQL 插件

Head插件

https://www.cnblogs.com/guozp/archive/2018/04/02/8686904.html

猜你喜欢

转载自blog.csdn.net/u014106644/article/details/88766252