基本概念

Node 与 Cluster

Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。

单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

Index ( 对应数据库的表 )

Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。

所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是表的同义词。每个 Index 的名字必须是小写。

下面的命令可以查看当前节点的所有 Index。

curl -X GET 'http://localhost:9200/_cat/indices?v'

ElasticSearch 默认开启9200、9300端口,9200端口供http访问,9300供tcp访问,ElasticSearch通过9300端口通信,

可以直接通过 http://localhost:9200 访问 ElasticSearch,为简化示例,后续都是通过curl方式演示相关操作

Document (对应数据库表的行)

Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。

Document 使用 JSON 格式表示,下面是一个例子。

同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。

{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}

Type(分类,elasticsearch 7.0之后弃用)

Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。

不同的 Type 应该有相似的结构(schema),举例来说,id字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如productslogs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。

下面的命令可以列出每个 Index 所包含的 Type。

curl 'localhost:9200/_mapping?pretty=true'

Mapping(类似数据库中的表结构)

查看当前所有mapping

curl 'localhost:9200/_mapping?pretty=true'

 

基本操作

查看当前节点的所有Index

curl -X GET 'http://localhost:9200/_cat/indices?v'

列出每个 Index 所包含的 Type

curl 'localhost:9200/_mapping?pretty=true'

新建Index,下面命令,首先新建一个名称为news的 Index,里面有一个名称为newscontent的 Type。news有一个字段 content 。content字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。

创建index同时创建mapping

curl -X PUT http://localhost:9200/news -H 'Content-Type:application/json' -d'
{
"mappings": {
"newscontent": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'

  

删除Index

curl -X DELETE 'localhost:9200/news'

新增数据

curl -XPOST http://localhost:9200/news/newscontent/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
' curl -XPOST http://localhost:9200/news/newscontent/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
' curl -XPOST http://localhost:9200/news/newscontent/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
' curl -XPOST http://localhost:9200/news/newscontent/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'

  

查看某ID的数据

curl 'localhost:9200/news/newscontent/1?pretty=true'

返回所有记录

curl -XPOST http://localhost:9200/news/_search

返回搜索记录,并高亮显示 hightlight

curl -XPOST http://localhost:9200/news/_search  -H 'Content-Type:application/json' -d'
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<red>"],
"post_tags" : ["</red>"],
"fields" : {
"content" : {}
}
}
}
'
返回结果示例
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2,
"hits": [
{
"_index": "news",
"_type": "newscontent",
"_id": "4",
"_score": 2,
"_source": {
"content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
},
"highlight": {
"content": [
"<red>中国</red>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
]
}
},
{
"_index": "news",
"_type": "newscontent",
"_id": "3",
"_score": 2,
"_source": {
"content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
},
"highlight": {
"content": [
"均每天扣1艘<red>中国</red>渔船 "
]
}
}
]
}
}

  

参考资料

全文搜索引擎 Elasticsearch 入门教程

作者: 阮一峰

http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html

中文分词 elasticsearch-analysis-ik

https://github.com/medcl/elasticsearch-analysis-ik

CentOS7安装Elasticsearch

腾讯云实验室

https://www.cnblogs.com/gezifeiyang/p/11007727.html

Elasticsearch: 权威指南(官方文档)

https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

最新文章

  1. sql 多级内查询
  2. MySql 存储过程及调用方法
  3. delphi SPCOMM串口控件
  4. realmswift的使用
  5. jQuery下拉框插件8种效果
  6. 如何用火车头采集当前页面url网址
  7. JAVA 大数据内存耗用测试
  8. HTML5自学笔记[ 19 ]canvas绘图实例之炫彩时钟
  9. sql server中局部变量与全局变量的 申明与赋值(转)
  10. RandomAccessFile
  11. sql语句使用游标修改表中数据
  12. 方法:查询MongoDB数据库中最新一条数据(JAVA)
  13. Unity3d 协程的注意问题(新手须注意,老手须加勉)
  14. C# 模拟用户登录
  15. JQuery控制input的readonly和disabled属性
  16. Spring OAuth2 GitHub 自定义登录信息
  17. flask微服务框架的初步接触
  18. #学号 20175201张驰 《Java程序设计》第3周学习总结
  19. mac下mysql安装及配置启动
  20. DataTable序列化

热门文章

  1. Linux记录-SVN+Jenkins+jdk+maven自动化集成部署
  2. 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
  3. Ideal 使用帮助手册
  4. sublime text 3 前端开发常用插件
  5. PHPStudy后门事件分析
  6. iOS-打印控件
  7. 【tensorflow】softmax_cross_entropy_with_logits与sparse_softmax_cross_entropy_with_logits
  8. 【OpenGL开发】GLUT/freeglut 是什么? OpenGL 和它们有什么关系?
  9. Python机器学习基础教程-第1章-鸢尾花的例子KNN
  10. npm i node-sass 报错&amp;npm 镜像切换