elasticsearch简单入门
Java环境
vim ~/.bash_profile
# java
export JAVA_HOME="/jdk1.8.0_202/bin"
export PATH=$PATH:$JAVA_HOME/bin
elasticsearch
https://www.elastic.co/guide/cn/elasticsearch/guide/current/running-elasticsearch.html
安装elasticsearch
下载:http://www.elastic.co
vim ~/.bash_profile
# elasticsearch
export ES_HOME="/elasticsearch-6.3.1"
export PATH=$PATH:$ES_HOME/bin
source ~/.bash_profile
启动:elasticsearch
守护进程在后台运行: elasticsearch -d
访问测试:http://127.0.0.1:9200/
或者:curl ‘http://localhost:9200/?pretty’
windows下载:http://curl.haxx.se/download.html
外网访问配置
vim config/elasticsearch.yml
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
# network.host: 192.168.0.1
network.host: 0.0.0.0
安装kibana
# kibana
export KIBANA_HOME="kibana-6.3.1-darwin-x86_64"
export PATH=$PATH:$KIBANA_HOME/bin
启动kibana: kibana
后台启动
nohup ./bin/kibana &
访问:http://localhost:5601
配置外网访问
vim config/kibana.yml
#server.host: "localhost"
server.host: "0.0.0.0"
报错解决
请求错误,需要添加请求头
{
“error” : “Content-Type header [application/x-www-form-urlencoded] is not supported”,
“status” : 406
}
-H “Content-Type: application/json”
请求测试:
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}
'
curl -X GET "localhost:9200/_count" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
测试
1、添加数据
路径 /megacorp/employee/1 包含了三部分的信息:
megacorp 索引名称
employee 类型名称
1 特定雇员的ID
curl -X PUT "localhost:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
'
curl -X PUT "localhost:9200/megacorp/employee/2" -H 'Content-Type: application/json' -d'
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
'
curl -X PUT "localhost:9200/megacorp/employee/3" -H 'Content-Type: application/json' -d'
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
'
2、查询数据
# Query-string
curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith"
# 查询表达式
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
'
Elasticsearch和传统关系型数据库区别:
- Elasticsearch中的 相关性
- 传统关系型数据库中的一条记录要么匹配要么不匹配
支持搜索方式
- 轻量搜索 _search?q=key:value
- 全文搜索 match
- 短语搜索 match_phrase
- 高亮搜索 highlight
聚合(aggregations)
集群健康
GET /_cluster/health
status 字段
- green 所有的主分片和副本分片都正常运行
- yellow 所有的主分片都正常运行,但不是所有的副本分片都正常运行
- red 有主分片没能正常运行
单机启动2个节点
https://jingyan.baidu.com/article/86fae3461e40cb3c48121a5b.html
文档元数据
_index
文档在哪存放, 索引名,这个名字必须小写,不能以下划线开头,不能包含逗号
_type
文档表示的对象类别
命名可以是大写或者小写,但是不能以下划线或者句号开头,不应该包含逗号, 并且长度限制为256个字符
_id
文档唯一标识
要么提供自己的 id ,要么让 Elasticsearch 帮你生成
创建文档
自定义id:PUT 谓词(“使用这个 URL 存储这个文档”)
PUT /{index}/{type}/{id}
自动生成id:POST 谓词(“存储文档在这个 URL 命名空间下”)
POST /{index}/{type}/
创建新文档,不存在时才创建
PUT /website/blog/123/_create
PUT /website/blog/123?op_type=create
取回文档
GET /website/blog/123?pretty
GET /website/blog/123?_source=title,text
GET /website/blog/123/_source
更新文档
PUT /website/blog/123
删除文档
DELETE /website/blog/123
指定外部版本号
PUT /website/blog/2?version=5&version_type=external
文档是不可变的:他们不能被修改,只能被替换
文档的部分更新
POST /website/blog/1/_update
取回多个文档
GET /_mget
批量操作
POST /_bulk
create: 如果文档不存在,那么就创建它
index: 创建一个新文档或者替换一个现有的文档
update: 部分更新一个文档
delete: 删除一个文档
协调节点(coordinating node)
分片计算
shard = hash(routing) % number_of_primary_shards
number_of_replicas 指的是在索引设置中的设定副本分片数
搜索
映射(Mapping):描述数据在每个字段内如何存储
分析(Analysis): 全文是如何处理使之可以被搜索的
领域特定查询语言(Query DSL): Elasticsearch 中强大灵活的查询语言
GET /_search
返回结果
total 匹配到的文档总数
took 搜索请求耗费(毫秒)
shards 查询中参与分片的总数
timed_out 查询是否超时
搜索参数
size 返回的结果数量,默认是 10
from 跳过的初始结果数量,默认是 0
分页深度
将搜索引擎和所有其他数据库区别:
最大的差异在于 代表 精确值 (它包括 string 字段)的字段和代表 全文 的字段
- 前缀表明这个词必须存在
倒排索引由文档中所有不重复词的列表构成,对于其中每个词,有一个包含它的文档列表
分析器
GET /_analyze
{
"analyzer": "standard",
"text": "Text to analyze"
}
字符串: string
整数 : byte, short, integer, long
浮点数: float, double
布尔型: boolean
日期: date
查看映射
GET /<_index>/_mapping/<_type>
Lucene 不理解内部对象。 Lucene 文档是由一组键值对列表组成的
过滤情况 时,查询被设置成一个“不评分”或者“过滤”查询
查询情况 时,查询就变成了一个“评分”的查询
使用 查询(query)语句来进行 全文 搜索或者其它任何需要影响 相关性得分 的搜索。除此以外的情况都使用过滤(filters)。
查询
match_all 匹配所有文档 默认的查询
match 标准查询
multi_match
range 指定区间
gt 大于
gte 大于等于
lt 小于
lte 小于等于
term 查询被用于精确值 匹配
terms 多值进行匹配
exists 查询和 missing 指定字段中有值或无值
多查询组合
must 文档 必须 匹配
must_not 文档 必须不 匹配
should 如果满足这些语句中的任意语句,将增加 score ,否则,无任何影响
filter 必须 匹配,但它以不评分、过滤模式来进行
验证查询
GET /_validate/query?explain
Elasticsearch 的相似度算法
检索词频率/反向文档频率, TF/IDF
增-删-改-查 (CRUD)
深分页(Deep Pagination)
number_of_shards 每个索引的主分片数,默认值是 5 。这个配置在索引创建后不能修改。
number_of_replicas 每个主分片的副本数,默认值是 1 。
其他
查看当前节点的所有 Index
$ curl -X GET 'http://localhost:9200/_cat/indices?v'
列出每个 Index 所包含的 Type
$ curl 'localhost:9200/_mapping?pretty=true'
拓展文章
Elasticsearch-基础介绍及索引原理分析
https://www.cnblogs.com/dreamroute/p/8484457.html
Elasticsearch数据库
https://blog.csdn.net/aisemi/article/details/80212836
全文搜索引擎 Elasticsearch 入门教程
http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
相关文章
- ElasticSearch环境配置
- elasticsearch 的入门
- ElasticSearch 7.x 简体/繁体搜索
- 关闭Elasticsearch健康检查
- ElasticSearch入门 第三篇:索引
- Elasticsearch——查询//过滤详细总结
- Python编程:elasticsearch库操作Elasticsearch
- elasticsearch pinyin 拼音分词器
- 分布式搜索引擎Elasticsearch基础入门学习
- elasticsearch 亿级数据检索案例与原理
- 掌握 analyze API,一举搞定 Elasticsearch 分词难题
- 【ElasticSearch】SpringDataElasticSearch 通过注解@Document自动创建索引失败
- ElasticSearch从入门到精通,史上最全(持续更新,未完待续,每天一点点)
- Elasticsearch教程(9) Mapping 字段类型 keyword text date numeric
- elasticsearch系列二:索引详解(快速入门、索引管理、映射详解、索引别名)
- Elasticsearch 6.x 入门测试
- Elasticsearch:Elasticsearch 开发入门 - Nodejs
- 【檀越剑指大厂--ElasticSearch】ElasticSearch入门