你的位置:首页 > 数据库

[数据库]Elasticsearch


Elasticsearch

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

 

Elasticsearch Reference

参考文档:

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

 

ES安装

下载elasticsearch-2.2.0.tar.gz,解压,执行/bin/elasticsearch;

访问地址:http://node1:9200

 

安装插件 head

执行bin/plugin install mobz/elasticsearch-head

访问地址:http://node1:9200/_plugin/head

如果用root用户执行启动脚本会提示:

Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.

创建elasticsearch用户执行

adduser –U elasticsearch

然后以 elasticsearch用户登录,启动程序。

 

集群部署

部署三台机器,部署程序,然后修改配置文件 config/elasticsearch.yml

配置:

cluster.name: my-applicationnode.name: node1discovery.zen.ping.multicast.enabled: falsediscovery.zen.ping_timeout: 120sclient.transport.ping_timeout: 60sdiscovery.zen.ping.unicast.hosts: ["node1","node2", "node3"]

集群名字cluster.name相同才能组成一个集群,修改node.name对应的主机名。

 

防止脑裂配置

discovery.zen.minimum_master_nodes=2

 

Curl

通过curl操作Elasticsearch数据:

curl -XPUT ‘http://node1:9200/index1/’ -d ‘传输的数据’

-X指定http请求方法:HEAD, PUT, GET, PST, DELETE

-d 指定传输的数据

 

–GET:获取对象的当前状态;

–PUT:改变对象的状态;      幂等

–POST:创建对象;

–DELETE:删除对象;          幂等

–HEAD:获取头信息。

 

Elasticsearch与关系型数据库的对比

DatabaseIndex 
Table  Type
Row  Document
ColumnField

 

 

 

 

 

创建Index

curl -XPUT 'http://node1:9200/test/'

 

创建Type

curl -XPOST http://node1:9200/test/emp/ -d '{"name" : "John"}' 自动创建id,也可以自己指定idcurl -XPUT http://node1:9200/test/emp/1 -d '{"name" : "Tom"}' 需要指定id

 

查询

curl -XGET http://node1:9200/test/emp/1

{"_index":"test","_type":"emp","_id":"1","_version":2,"found":true,"_source":{"name" : "Jim"}}

curl -XGET http://node1:9200/test/emp/1?pretty

{ "_index" : "test", "_type" : "emp", "_id" : "1", "_version" : 2, "found" : true, "_source" : {  "name" : "Jim" }}

pretty 格式化结果,更好看

 

查询带报文头

curl -i http://node1:9200/test/emp/1?pretty

HTTP/1.1 200 OKContent-Type: application/json; charset=UTF-8Content-Length: 134{ "_index" : "test", "_type" : "emp", "_id" : "1", "_version" : 2, "found" : true, "_source" : {  "name" : "Jim" }}

 

查询_source

curl -XGET http://node1:9200/test/emp/1/_source

{"name" : "Jim"}

 

查询指定field的信息

curl -XGET http://node1:9200/test/emp/1?_source=name

 

查询type信息

curl -XGET http://node1:9200/test/emp/_search?pretty

{ "took" : 7, "timed_out" : false, "_shards" : {  "total" : 5,  "successful" : 5,  "failed" : 0 }, "hits" : {  "total" : 3,  "max_score" : 1.0,  "hits" : [ {   "_index" : "test",   "_type" : "emp",   "_id" : "AVYw5zrHBTthl-73fXCK",   "_score" : 1.0,   "_source" : {    "name" : "John"   }  }, {   "_index" : "test",   "_type" : "emp",   "_id" : "2",   "_score" : 1.0,   "_source" : {    "name" : "John"   }  }, {   "_index" : "test",   "_type" : "emp",   "_id" : "1",   "_score" : 1.0,   "_source" : {    "name" : "Jim"   }  } ] }}

 

查询指定条件数据

curl -XGET http://node1:9200/test/emp/_search?q=name:Jim

 

 

Domain Specific Language DSL 查询

curl -XGET http://node1:9200/test/emp/_search?pretty -d \'{"query":  {"match":    {"name":"Jim"}  }}'

 

获取多个文档

curl -XGET http://node1:9200/_mget?pretty -d \'{"docs":[{"_index":"test","_type":"emp","_id":1},{"_index":"test","_type":"emp","_id":2}]}'

curl -XGET http://node1:9200/test/emp/_mget?pretty -d \'{"ids":["1","2"]}'

 

检查文档是否存在,只返回报文头

curl -i -XHEAD http://node1:9200/test/emp/1?pretty

 

更新字段信息

curl -XPUT http://node1:9200/test/emp/1?pretty -d \'{"name":"Tom",}'

 

局部更新(增加first_name字段) 只能用POST

curl -XPOST http://node1:9200/test/emp/1/_update -d '{"doc":{"first_name":"Jim"}}'

 

删除文档

curl -XDELETE http://node1:9200/test/emp/1

指定id

 

查询设置 副本数量,分片数量

curl -XGET http://node1:9200/test/_settings?pretty

{ "test" : {  "settings" : {   "index" : {    "creation_date" : "1469698597831",    "uuid" : "VFew5vmlQUC3Uieu62w23Q",    "number_of_replicas" : "1",    "number_of_shards" : "5",    "version" : {     "created" : "2020099"    }   }  } }}

 

设置分片及副本数

curl -XPUT http://node1:9200/test/_settings -d'{"settings":{"number_of_shards":3,"number_of_replicas":2}}'

 

查询mapping信息

curl -XGET http://node1:9200/test/_mapping?pretty

 

Java API

官方文档:

https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.2/index.html

 

测试demo