01 elasticsearch 安装使用

本文详细介绍了Elasticsearch的安装步骤、核心概念与数据库对应关系,以及如何通过RESTAPI进行索引库创建、索引与文档操作、查询与高级搜索、批量操作等常用功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

elasticsearch是基于lucene分布式的全文搜索引擎,对外提供/http://www.xxx.com:9200/index/type/delete(get,post,put,head)这种REST风格的接口。2012年出的比solr晚出。使用场景,lucene和solr搞不定的数据量的索引项目。

elasticsearch的核心概念和数据库的对应关系

数据库 elasticsearch

database index

table type

row document

column field


elasticsearch安装步骤(1.4.4)

环境:操作系统Linux,jdk 1.7

1: 解压elasticsearch.tar.gz包

tar -zxvf elasticsearch-1.4.4

2:启动elasticsearch

elasticsearch1.4.4/bin/elasticsearch -d

http://192.168.xxx.xxx:9200 端口对外开放REST api

3:jps 。。。kill 。。。。

elasticsearch插件elasticsearch-servicewrapper.zip

1:解压

unzip elasticsearch-servicewrapper.zip

2:拷贝service文件夹到elasticsearch的bin目录下

cp -r service bin

3:使用插件

elasticsearch/bin/service/elasticsearch start 后台启动

stop 关闭

console 前台启动

install 开机启动

remove 取消开机启动

elasticsearch rest api 使用

curl语法

curl -X (指定http的请求方式,HEAD,GET,POST,PUT,DELETE) -D(请求的数据) -i(获取头文件) 

1:创建索引库, 名字mosiindex,名字得小写,不能包含特殊字符,不能以_作为开头第一个字。

curl -XPUT 'http://localhost:9200/mosiindex

2:创建索引, 在mosiindex的索引库里,创建一个 type:person,id:701,name:mosi,age25

curl -XPUT(POST) 'http://localhost:9200/mosiindex/person/701' -d ‘{"name":"mosi",age:25}’ 

PUT用于更新,必须指定id是多少。?op_type='create',/_create,强行创建,如果id存在,则报错,更新是加入一个新文档,把旧的做一个删除标志。

POST用于新增,不需要指定id也可以新增,id由elasticsearch服务端自动生成。如果id存在,可以进行局部更新。

3 根据id,进行简单查询

http://localhost:9200/mosiindex/person/701?pretty

"_index":"mosiindex",

"_type":"person",

"_version":7,

"found":true,

"_source":{"name":"mosi",age:25}

http://localhost:9200/mosiindex/person/701?_source=age,name&pretty

http://localhost:9200/mosiindex/person/701/_source?pretty


4:/_search,不带ID,进行高级查询

http://localhost:9200/mosiindex/person/_search?pretty
http://localhost:9200/mosiindex/_search?pretty

http://localhost:9200/_search?pretty

5:带query条件进行高级查询。配合DELETE,可以进行条件删除

1:http://localhost:9200/mosiindex/person/_search?q=name:mosi

2:DSL

{"query":

{"match":

{"name":"mosi"}

}

}

6:多个query组合mget,根据found:true和false来判断

7:删除索引

curl -XDELETE ‘http://localhost:9200/mosiindex/person/701’

删除仅仅是做了一个删除标记,_version + 1(乐观),后面elasticsearch来对标记的文档做真正的删除操作

8:批量操作

一般1000~5000个文档,5M~15M。根据实际情况来调节










http://localhost:9200/mosiindex/person/701?_source=age,name&pretty
http://localhost:9200/mosiindex/person/701?_source=age,name&pretty
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值