一、Elasticsearch简介
- 基于Lucene的开源分布式搜索引擎,大幅降低了PB级海量数据存储、检索、分析门槛
- 特点:
- 分布式实时文件存储、检索、分析
- 零配置、集群自动发现
- 索引自动分片、副本机制
- RESTful风格接口
- 多数据源
- 自动搜索负载
……
二、安装ElasticSearch的知识点
- 解除Linux系统的最大文件打开数限制
#编辑/etc/security/limits.conf,追加以下内容:
* - nofile 65536 #当前系统针对所有用户最大打开文件数为65536
- 修改Linux系统一个进程能拥有的最大虚拟内存区域
#编辑/etc/sysctl.conf文件,追加以下内容:
vm.max_map_count=262144 #Elasticsearch最低要求
$ sysctl -p #保存使生效
- 修改网络地址
#编辑/opt/es/config/elasticsearch.yml,修改以下内容:
network.host: 0.0.0.0
- 启动ElasticSearch
#切换至非root用户
#前台运行,停止方式CTRL+C
/opt/es/bin/elasticsearch
#后台运行,停止方式 kill -9 pid
/opt/es/bin/elasticsearch -d
- 访问Elasticsearch RESTful数据接口
http://HOSTNAME:9200
http://HOSTNAME:9200/_cat
- 安装elasticsearch-head插件
$ git clone git://github.com/mobz/elasticsearch-head.git
$ cd elasticsearch-head
$ yum install npm
$ npm install
$ npm run start
- 修改elasticsearch.yml允许es-head插件跨域访问
#/opt/es/config/elasticsearch.yml末尾追加以下两个配置:
http.cors.enabled: true
http.cors.allow-origin: "*"
- 重启ES后访问es-head
重启ES后访问es-head
三、ES数据模型
- Index:索引,由多个Document组成
- Type:索引类型,6.x中仅支持一个,以后将逐渐被移除
- Document:文档,由多个Field组成
- Field:字段,包括字段名与字段值
RDBMS | ElasticSearch |
---|---|
Database | Index |
Table | Type |
Row | Document |
Column | Field |
四、文档管理
文档是ES最小数据单元
-
原始数据
- _source:原始JSON格式文档
-
文档元数据
- _index:索引名
- _type:索引类型
- _id:文档编号
- _version:文档版本号用于并发控制
- _score:在搜索结果中的评分
-
增加数据
#第一种方式
PUT /索引/类型
#第二种方式
PUT /index123/doc/1
{
"name":"Edmund",
"age":40
}
注:put /索引名/类型名/id
#第三种方式
PUT twitter123/_mapping/_doc
{
"properties": {
"email": {
"type": "keyword"
}
}
}
- 删除数据
DELETE /索引
- 更改数据
POST /索引/类型/1/_update
{
"doc":{
列名:值//精准修改其中某个列
}
}
- 查询数据
GET /索引/类型/_search 查询所有
GET /索引/类型/_search
{"query":{"match_all":{}}}
GET /索引/类型/1?_source=字段列表
GET /索引/类型/_search
{"query":{
"match":{json串}
}
}
GET /def/_search
{
"query": {
"multi_match": {
"query": "elasticsearch basketball",
"fields": ["interest"]
}
}
, "size": 1,
"from": 0,
"highlight": {
"fields": {
"interest": {}
}
}
}
#bool查询
GET /def/_search
{
"query": {
"bool": {
"must": {"match": {"name": "John Kerry"}}
, "must_not": {"match": {"name": "Isaac Newton"}}
"should":[
{"match": {"interest": "basketball"}}
]
}
}
}
注释:must_not和should不可以同时用