原创

Elasticsearch安装教程以及增删改查操作

1.下载安装

直接去找适合自己的版本即可
官网下载
我选的darwin tar包下载

[2019-06-27T18:54:57,799][INFO ][o.e.c.s.ClusterApplierService] [ZBMAC-C02WD12BH.local] master node changed {previous [], current [{ZBMAC-C02WD12BH.local}{YnWWA4nFRKOyrZ-b1s77pA}{J9CijQkVTuWSos6ak-VSYw}{127.0.0.1}{127.0.0.1:9300}{ml.machine_memory=17179869184, xpack.installed=true, ml.max_open_jobs=20}]}, term: 1, version: 1, reason: Publication{term=1, version=1}
[2019-06-27T18:54:57,851][INFO ][o.e.h.AbstractHttpServerTransport] [ZBMAC-C02WD12BH.local] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}

出现这些就是成功了,然后再新开一个终端用curl来测试吧,比较是支持REST的 ,利用curl体现的淋漓尽致

2.利用crul进行CURD操作

(1创建

在ElasticSearch索引中,对应于CRUD中的“创建”和“更新” - 如果对具有给定类型的文档进行索引,并且要插入原先不存在的ID。 如果具有相同类型和ID的文档已存在,则会被覆盖。
要索引第一个JSON对象,我们对REST API创建一个PUT请求到一个由索引名称,类型名称和ID组成的URL。 也就是:http://localhost:9200///[]。
索引和类型是必需的,而id部分是可选的。如果不指定ID,ElasticSearch会为我们生成一个ID。 但是,如果不指定id,应该使用HTTP的POST而不是PUT请求。
索引名称是任意的。如果服务器上没有此名称的索引,则将使用默认配置来创建一个索引。
至于类型名称,它也是任意的。 它有几个用途,包括:

  • 每种类型都有自己的ID空间。
  • 不同类型具有不同的映射(“模式”,定义属性/字段应如何编制索引)。
  • 搜索多种类型是可以的,并且也很常见,但很容易搜索一种或多种指定类型。
ZBMAC-C02WD12BH:~ lishihao$ curl -H "Content-Type: application/json" -XPUT -d '{"title": "hello"}' "http://localhost:9200/lsh/a/1"
{"_index":"lsh","_type":"a","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
  • 其中 -H 是为了表明请求体的文本类型, -d是请求体内容
  • 最后买的是url 第一个uri lsh为索引,二段uri为类型

创建索引也可以分部创建,先创建索引在填充类型数据

ZBMAC-C02WD12BH:~ lishihao$ curl -XPUT "http://localhost:9200/zmm"
{"acknowledged":true,"shards_acknowledged":true,"index":"zmm"}

ZBMAC-C02WD12BH:~ lishihao$ curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/zmm/a" -d '{"title":"aaa"}'
{"_index":"zmm","_type":"a","_id":"t526mGsBeD7yDNwM3WwK","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1

在es中不同类型的文档没有物理上的分离,在同一个索引中的所有文档,无论何种类型,都是存储在属于相同分片的同一组文件中,一份分片就是一个Lucene的索引,类型的名称是Lucene索引中的一个字段,所有映射的所有字段都是Lucene索引中的字段

(2根据索引查询

ZBMAC-C02WD12BH:~ lishihao$ curl -XGET "http://localhost:9200/lsh/a/1"
{"_index":"lsh","_type":"a","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"title": "hello"}}

(3更新

ZBMAC-C02WD12BH:~ lishihao$ curl -H "Content-Type: application/json" -XPUT -d '{"title": "hello","act":"这就更新了"}' "http://localhost:9200/lsh/a/1"
{"_index":"lsh","_type":"a","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

ZBMAC-C02WDXGET "http://localhost:9200/lsh/a/1"
{"_index":"lsh","_type":"a","_id":"1","_version":2,"_seq_no":1,"_primary_term":1,"found":true,"_source":{"title": "hello","act":"这就更新了"}}

(4删除

ZBMAC-C02WD12BH:~ lishihao$ curl -XDELETE "http://localhost:9200/lsh/a/1"
{"_index":"lsh","_type":"a","_id":"1","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}

ZBMAC-C02WD1GETlishihao$ curl -XDELETE "http://localhost:9200/lsh/a/1"
{"_index":"lsh","_type":"a","_id":"1","found":false}
正文到此结束
本文目录