1)写数据
2)读数据。
写数据,实际上都写到磁盘文件里去了,查询的时候,操作系统会将磁盘文件里的数据自动缓存到 filesystem cache 里面去。es 的搜索引擎严重依赖于底层的 filesystem cache,你如果给 filesystem cache 更多的内存,尽量让内存可以容纳所有的 idx segment file 索引数据文件,那么你搜索的时候就基本都是走内存的,性能会非常高。所以,最佳的情况下,就是你的机器的内存,至少可以容纳你的总数据量的一半。建议用 es + hbase 这么一个架构。
3)搜索数据。
其实 es 第一是准实时的,数据写入 1 秒后可以搜索到;可能会丢失数据的。有 5 秒的数据,停留在 buffer、translog os cache、segment file os cache 中,而不在磁盘上,此时如果宕机,会导致 5 秒的数据丢失。数据写入 segment file 之后,同时就建立好了倒排索引。
4)删除/更新数据 如果是删除操作,commit 的时候会生成一个 .del 文件,里面将某个 doc 标识为 deleted 状态,那么搜索的时候根据 .del 文件就知道这个 doc 是否被删除了。
如果是更新操作,就是将原来的 doc 标识为 deleted 状态,然后新写入一条数据。
buffer 每次 refresh 一次,就会产生一个 segment file,所以默认情况下是 1 秒钟一个 segment file,这样下来 segment file 会越来越多,此时会定期执行 merge。每次 merge 的时候,会将多个 segment file 合并成一个,同时这里会将标识为 deleted 的 doc 给物理删除掉,然后将新的 segment file 写入磁盘,这里会写一个 commit point,标识所有新的 segment file,然后打开 segment file 供搜索使用,同时删除旧的 segment file。
1、1.5.x版本之后,需要分词的字段需要设定text类型和对应的analyzer
;仅需要精确匹配的可直接设置为keyword类型。
2、长文本高亮需要在text类型的基础上,设置fast-vector-highlighter
高亮方式,高亮效率能提升20倍以上。
官网下载地址:官网
1、安装elasticsearch-jdbc中间键
wget http://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.4.0/elasticsearch-jdbc-2.3.4.0-dist.zip
unzip elasticsearch-jdbc-2.3.4.0-dist.zip
2、安装dos2unix
yum install dos2unix
参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.3/sql-getting-started.html
命令 | 说明 |
---|---|
DESC table | 查看该索引的字段和元数据 |
SHOW COLUMNS | 功能同上,只是别名 |
SHOW FUNCTIONS | 列出支持的函数列表,支持通配符过滤 |
SHOW TABLES | 返回索引列表 |
SELECT .. FROM table_name WHERE .. GROUP BY .. HAVING .. ORDER BY .. LIMIT .. | 用来执行查询的命令 |
此外,还支持通配符查询,只是通配符目前只支持%
和 _
参考:https://www.elastic.co/guide/en/elasticsearch/reference/6.3/sql-getting-started.html
命令 | 说明 |
---|---|
DESC table | 查看该索引的字段和元数据 |
SHOW COLUMNS | 功能同上,只是别名 |
SHOW FUNCTIONS | 列出支持的函数列表,支持通配符过滤 |
SHOW TABLES | 返回索引列表 |
SELECT .. FROM table_name WHERE .. GROUP BY .. HAVING .. ORDER BY .. LIMIT .. | 用来执行查询的命令 |
此外,还支持通配符查询,只是通配符目前只支持%
和 _
PUT demo
{
"mappings":{
"doc":{
"properties":{
"title":{
"type":"text",
"analyzer": "ik_max_word"
},
"content":{
"type":"text",
"analyzer": "ik_max_word"
},
"uniqueId":{
"type":"keyword",
"index":"not_analyzed"
},
"created": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
}
}
}
},
"settings":{
"number_of_shards":3,
"number_of_replicas":1
}
}
本文用到的
_*
只是用来代指某一个值,没有特殊含义。
查询里面必须的两个东西是
SearchRequest
和SearchSourceBuilder
,且必须将SearchSourceBuilder
加入到SearchRequest
SearchRequest searchRequest = new SearchRequest(); //定义查询请求,可带索引参数
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //定义请求体
searchSourceBuilder.query(QueryBuilders.matchAllQuery()); //请求体内容为 match_all
searchRequest.source(searchSourceBuilder); //将请求体放入查询请求中
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //设置超时时间,60s
支持的范围类型:
integer_range
--- 一系列带符号的32位整数,其范围在 -2^31 ~ 2^31-1
float_range
--- 一系列单精度32位的浮点值。
long_range
--- 一系列带符号的64位整数,其范围在 -2^63 ~ 2^63-1
double_range
--- 一系列双精度64位的浮点值。
date_range
--- 一系列日期值,无符号64位整数,单位毫秒。
ip_range
--- 支持IPv4或 IPv6(或混合)地址的一系列ip值。
可接受的参数:
coerce
--- 尝试将字符串转换为数字并截断整数的分数。接受true(默认)和false。
boost
--- 映射字段级查询时间提升。接受一个浮点数,默认为 1.0。
include_in_all
--- 字段值是否应该包含在 all 字段中?接受 true 或 false。默认为 false 。if index 设置为 false,
或者父 object 字段设置 include_in_all 为 false。否则,默认为 true。
index
--- 该字段是否应该搜索?接受 true(默认)和 false。
store
--- 字段值是否应该与 source 字段分开存储和检索。接受 true 或 false (默认)。
PUT twitter
{
"settings" : {
"index" : {
"refresh_interval":"60s"
}
}
}
设置为 -1s ,即不刷新。
地理坐标点不能被动态映射自动检测,需要显式声明对应字段类型为
geo-point
PUT /attractions
{
"mappings": {
"restaurant": {
"properties": {
"name": {
"type": "text"
},
"location": {
"type": "geo_point"
}
}
}
}
}
经纬度信息的形式可以是字符串、数组、对象或者geohash,所以对应有四种插入方法:
PUT /attractions/restaurant/1
{
"name": "Chipotle Mexican Grill",
"location": "40.715, -74.011" #字符串形式
}
PUT /attractions/restaurant/2
{
"name": "Pala Pizza",
"location": { #对象形式
"lat": 40.722,
"lon": -73.989
}
}
PUT /attractions/restaurant/3
{
"name": "Mini Munchies Pizza",
"location": [ -73.983, 40.719 ] #数组形式
}
PUT /attractions/restaurant/4
{
"name": "Geo-point as a geohash",
"location": "drm3btev3e86" #geohash形式
}