安装elasticsearch
安装elasticsearch
下载elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.16.tar.gz
解压缩elasticsearch
tar -zxf elasticsearch-5.6.16.tar.gz
拷贝到wwwroot目录
mv elasticsearch-5.6.16/ /data/wwwroot/
新建专用程序账户
groupadd sulwan
把用户添加到专有群组
useradd -g sulwan sulwan -p 123456
切换到新用户
su sulwan
切换到程序目录
cd /data/wwwroot/elasticsearch-5.6.16/bin/
编写pm2运行脚本
{
"name": "elas",
"script": "/bin/bash",
"args": [
"/data/wwwroot/elasticsearch-5.6.16/bin/elasticsearch"
],
"watch": false,
"ignore_watch": [
"logs"
],
"error_file": "./logs/elasticsearch-err.log",
"out_file": "./logs/elasticsearch-out.log",
"exec_interpreter": "",
"exec_mode": "fork"
}
运行脚本
pm2 start elas.json
设置开机启动
pm2 save
生成开机
pm2 startup centos
启用开机自启
systemctl enable pm2-root
开启远程访问
vi /data/wwwroot/elasticsearch-5.6.16/config/elasticsearch.yml
// IP填写的是本机IP
network.host: 192.168.168.10 #### 0.0.0.0
从启服务
pm2 reload elas
如果启动不了执行如下
vi /etc/sysctl.conf 文件最后添加一行
vm.max_map_count=262144
sysctl -p
elasticsearch简明教程
_index // 文档所属的索引名
_type // 文档所属的类型名。
_id // 文档唯一键id
_source // 原始json数据
_all // 整合所有字段内容到该字段,7.x已废弃
_version // 文档的版本信息
_score // 相关性分数
【注意】:_type字段,6.x已废弃,7.x已删除。从7.0开始,一个索引只能创建一个类型,也就是_doc。
索引
索引是相似文档的集合或容器。索引(Index)更多是体现逻辑空间的概念,而分片(Shard)更多是体现物理空间的概念。索引主要包括两大类,Mapping和Setting。
- Mapping,定义文档字段的类型
- Setting,定义不同的数据分布,包括Shard
【注意】:索引这个词在不同的上下文环境,有不同的语义。例如:
- 名词:一个Elasticsearch可以创建多个索引
- 动词:保存一个文档到索引的过程
- 其他:关系型数据中的B+树索引,或者Elasticsearch的倒排索引
Elasticsearch和RDBMS的类比
其实,Elasticsearch的各种概念和RDBMS(如Mysql)是完全不同的,但是如果我们硬要对他们进行比较的话,可以简单地做以下类比。
RDBMS | Elasticsearch |
---|---|
Table | Index |
Row | Document |
Column | Field |
Schema | Mapping |
SQL | DSL |
创建单个索引类型ID
PUT http://192.168.168.10:9200/movies/movie/1
{
"title": "The Godfather",
"director": "Francis Ford Coppola",
"year": 1972
}
批量同步索引
POST http://192.168.168.10:9200/schools/_bulk
{"index":{"_index":"schools", "_type":"school", "_id":"1"}}
{"name":"Central School","description":"CBSE Affiliation","street":"Nagan","city":"paprola", "state":"HP", "zip":"176115", "location":[31.8955385,76.8380405],"fees":2000, "tags":["Senior Secondary", "beautiful campus"], "rating":"3.5"}
{"index":{"_index":"schools", "_type":"school", "_id":"2"}}
{"name":"Saint Paul School", "description":"ICSE Afiliation", "street":"Dawarka", "city":"Delhi", "state":"Delhi", "zip":"110075","location":[28.5733056,77.0122136], "fees":5000,"tags":["Good Faculty", "Great Sports"], "rating":"4.5"}
{"index":{"_index":"schools", "_type":"school", "_id":"3"}}
{"name":"Crescent School", "description":"State Board Affiliation", "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114","location":[26.8535922, 75.7923988],"fees":2500, "tags":["Well equipped labs"], "rating":"4.5"}
查询索引
GET http://192.168.168.10:9200/movies/movie/1
删除索引
DELETE http://192.168.168.10:9200/movies/movie/1
搜索所有索引和所有类型
GET http://192.168.168.10:9200/_search
在电影索引中搜索所有类型
GET http://192.168.168.10:9200/_search
在电影索引中搜索所有类型
GET http://192.168.168.10:9200/movies/_search
在电影索引中显示搜索电影类型
GET http://192.168.168.10:9200/movies/movie/_search
基本自由文本搜索
POST http://192.168.168.10:9200/_search
{
"query": {
"query_string": {
"query": "kill"
}
}
}
指定搜索的字段
POST http://192.168.168.10:9200/_search
{
"query": {
"query_string": {
"query": "ford",
"fields": ["title"]
}
}
}
过滤器搜索
POST http://192.168.168.10:9200/_search
{
"query": {
"bool": {
"must": {
"query_string": {
"query": "drama"
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}
无需查询即可进行过滤
POST http://192.168.168.10:9200/_search
{
"query": {
"bool": {
"must": {
"match_all": {
}
},
"filter": {
"term": { "year": 1962 }
}
}
}
}
无需查询即可进行过滤(简单)
POST http://192.168.168.10:9200/_search
{
"query": {
"constant_score": {
"filter": {
"term": { "year": 1962 }
}
}
}
}
多索引查询
POST http://192.168.168.10:9200/school*,movies/_search?ignore_unavailable=true { "query":{ "query_string":{ "query":"Bill" } } }
分页
POST http://192.168.168.10:9200/school*,movies/_search?ignore_unavailable=true { "from":10, "size":20, "query":{ "query_string":{ "query":"Bill" } } }
排序
POST http://192.168.168.10:9200/movies/_search { "sort":[{"year":"desc"}], "query":{ "match_all": {} } }
指定返回特定字段
POST http://192.168.168.10:9200/movies/_search
{
"_source":["title"],
"sort":[{"year":"desc"}],
"query":{
"match_all": {}
}
}