文章目录
关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns)
Elasticsearch ⇒ 索引(Index) ⇒ 类型(type) ⇒ 文档(Docments) ⇒ 字段(Fields)
download :https://www.elastic.co/cn/downloads/elasticsearch
全文搜索
结构化数据------>关系型数据库存储,查找
非结构化数据—>1.顺序扫描法(从头到尾) 2. 全文搜索(建立文本库,创建索引搜索)
全文搜索实现ElasticSearch
1.Getting start
ES本身使用的是UDP协议, 该协议没有握手机制,也无法保证通讯过程中数据不丢失
9300 是tcp通讯端口,集群间和TCPClient都走的它
9200 是http协议的RESTful接口
basic concepts
- 基于java语言,Lucene引擎
- 高度可扩展的开源全文搜索和分析引擎
- 快速,近实时(near realtime NRT)的对大数据的存储和搜索、分析
- 异步写入
- 自身带有分布式,solr需要依赖zookeeper ,solr支持xml,json等格式
基于HTTP协议,以JSON为数据交互格式的RESTful API
数据存储在安装目录data下
测试ES启动成功 http://localhost:9200?pretty
搜索原理
插入这些数据到Elasticsearch的同时,Elasticsearch还为数据的每个字段建立索引–倒排索引
倒排索引(inverted index) 的结构来做快速的全文搜索
倒排索引由在文档中出现的唯一的单词列表,以及对于每个单词在文档中的位置组成。
倒排索引原理:
https://es.xiaoleilu.com/052_Mapping_Analysis/35_Inverted_index.html
倒排索引中的所有词项对应一个或多个文档
倒排索引中的词项根据字典顺序升序排列
对索引
List All Indicescurl localhost:9200/_cat/indices?v
添加索引curl -X PUT http://0.0.0.0:9200/customer?pretty
删除索引curl -X DELETE 'localhost:9200/customer?pretty'
https://blog.youkuaiyun.com/zhangbin666/article/details/73332538
对文档
添加文档
curl -H "Content-Type:application/json" -H "Data_Type:msg" -X POST --data '{
"first_name" : "Zhao",
"last_name" : "Jianyu",
"age" : 22,
"about" : "I love to go rock climbing",
"interests": [ "music" ]
}' localhost:9200/megacorp/employee/2
搜索文档/删除GET/delete /megacorp/employee/1
DSL删除文档_delete_by_query
curl -H "Content-Type:application/json" -X POST --data '{
"query": {
"bool": {
"filter": [
{
"exists": {
"field": "record_file",
"boost": 1
}
},
{
"term": {
"account_login_name": {
"value": "3004703",
"boost": 1
}
}
},
{
"range": {
"channel_time": {
"from": 1565280000,
"to": 1565971199,
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "asr_customer",
"boost": 1
}
},
{
"exists": {
"field": "asr_agent",
"boost": 1
}
},
{
"exists": {
"field": "stat_silence_rate",
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"bool": {
"should": [
{
"terms": {
"cdr_type": [
"cdr_ib",
"cdr_ob_agent",
"cdr_ob_customer"
],
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}' 'localhost:9200/index_20190809_alias%2Cindex_20190810_alias/type/_delete_by_query'
更新文档POST
替换文档PUT
查询总数
curl -XGET http://localhost:9200/_count?pretty
http://**.**.***.**:9200/_count?pretty `?pretty`使请求返回参数将JSON数据
2. 搜索Search API
六种搜索:
phrase search
highlight search
2.1 query string search
搜索全部文档 GET /megacorp/employee/_search
条件搜索 curl 'localhost:9200/megacorp/employee/_search?q=first_name:zhao'
按照价格降序搜索(不常用)GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
2.2 DSL查询(生产环境使用)
DSL(Domain Specific Language特定领域语言)
“query”: {
match 进行分词
,包含这三个词中的一个或多个的文档就会被搜索出来。
match_phrase 完全匹配,包含所有
multi_match 对多个字段进行匹配
term 是代表完全匹配
,即不进行分词器分析,文档中必须包含整个搜索的词汇(数值型,日期型)
terms
bool 联合查询: must,should,must_not
https://blog.youkuaiyun.com/tanga842428/article/details/75127418
https://www.jianshu.com/p/0e503c6dcf89
match语句–查询类型之一
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
curl -H "Content-Type:application/json" -H "Data_Type:msg" -X GET --data '
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}' 'localhost:9200/megacorp/employee/_search'
2.3 带过滤的搜索query filter
过滤器(filter)用于执行区间搜索
GET /megacorp/employee/_search
{
"query" : {
"filtered" : {
"filter" : {
"range" :