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。根据实际情况来调节