Elasticsearch整理笔记(二)
安装
- docker部署
搜索镜像
docker search elasticsearch
查看镜像&运行容器
docker images
docker run -d --name es2 -p
9200
:
9200
-p
9300
:
9300
-e
"discovery.type=single-node"
5acf0e8da90b
- elasticsearch-head插件查看ElasticSearch内部信息
- 谷歌浏览器提供了插件:ElasticSearchHead
使用
概念
索引:相当于database 一个数据库。
类型:相当于table,一个表。
文档:相当于一行记录。
常用API
创建索引
PUT http://localhost:9200/order_server
{
"settings"
: {
"index"
: {
"number_of_shards"
:
"1"
,
"number_of_replicas"
:
"5"
}
}
}
{
"acknowledged"
:
true
,
"shards_acknowledged"
:
true
,
"index"
:
"order_server"
}
- 添加mapping
- POST http://localhost:9200/order_server/_mapping/order?include_type_name=true
{
"properties"
: {
"context"
: {
"type"
:
"text"
,
"index"
:
false
}
}
}
{
"acknowledged"
:
true
}
- POST http://localhost:9200/order_server/_mapping/order?include_type_name=true
- 查看mapping
- GET http://localhost:9200/order_server/_mapping
{
"order_server"
: {
"mappings"
: {
"properties"
: {
"context"
: {
"type"
:
"text"
,
"index"
:
false
},
"extras"
: {
"properties"
: {
"class"
: {
"type"
:
"text"
,
"fields"
: {
"keyword"
: {
"type"
:
"keyword"
,
"ignore_above"
:
256
}
}
},
"grade"
: {
"type"
:
"text"
,
"fields"
: {
"keyword"
: {
"type"
:
"keyword"
,
"ignore_above"
:
256
}
}
},
"school_name"
: {
"type"
:
"text"
,
"fields"
: {
"keyword"
: {
"type"
:
"keyword"
,
"ignore_above"
:
256
}
}
},
"student_name"
: {
"type"
:
"text"
,
"fields"
: {
"keyword"
: {
"type"
:
"keyword"
,
"ignore_above"
:
256
}
}
}
}
},
"status"
: {
"type"
:
"long"
}
}
}
}
}
- GET http://localhost:9200/order_server/_mapping
插入数据
POST http://localhost:9200/order_server/order/10009
{
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_version"
:
3
,
"result"
:
"created"
//如果_id相同,会更新原数据,该字段返回"updated",
"_shards"
: {
"total"
:
6
,
"successful"
:
1
,
"failed"
:
0
},
"_seq_no"
:
7
,
"_primary_term"
:
1
}
修改数据
PUT http://localhost:9200/order_server/order/10001
{
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_version"
:
4
,
"result"
:
"updated"
,
"_shards"
: {
"total"
:
6
,
"successful"
:
1
,
"failed"
:
0
},
"_seq_no"
:
9
,
"_primary_term"
:
1
}
删除数据
DELETE http://localhost:9200/order_server/order/10001
{
}
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_version"
:
2
,
"result"
:
"deleted"
,
"_shards"
: {
"total"
:
6
,
"successful"
:
1
,
"failed"
:
0
},
"_seq_no"
:
4
,
"_primary_term"
:
1
}
id查询
GET http://localhost:9200/order_server/order/10001
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_version"
:
4
,
"_seq_no"
:
9
,
"_primary_term"
:
1
,
"found"
:
true
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
}
条件查询
POST http://localhost:9200/order_server/order/_search
{
"query"
:{
"match"
:{
"extras.student_name"
:
"张三"
}
}
}
{
"took"
:
26
,
"timed_out"
:
false
,
"_shards"
: {
"total"
:
1
,
"successful"
:
1
,
"skipped"
:
0
,
"failed"
:
0
},
"hits"
: {
"total"
: {
"value"
:
1
,
"relation"
:
"eq"
},
"max_score"
:
1.9616582
,
"hits"
: [
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_score"
:
1.9616582
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
}
]
}
}
精准查询
POST http://localhost:9200/order_server/order/_search
{
"query"
:{
"term"
:{
"status"
:
1
}
}
}
{
"took"
:
4
,
"timed_out"
:
false
,
"_shards"
: {
"total"
:
1
,
"successful"
:
1
,
"skipped"
:
0
,
"failed"
:
0
},
"hits"
: {
"total"
: {
"value"
:
4
,
"relation"
:
"eq"
},
"max_score"
:
1.0
,
"hits"
: [
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10002"
,
"_score"
:
1.0
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"李四"
},
"status"
:
1
}
},
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10009"
,
"_score"
:
1.0
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
},
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_score"
:
1.0
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
},
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10008"
,
"_score"
:
1.0
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第二中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"老王"
},
"status"
:
1
}
}
]
}
}
模糊查询
POST http://localhost:9200/order_server/order/_search
{
"query"
: {
"match"
: {
"extras.school_name"
: {
"query"
:
"佳木斯 中学"
,
"fuzziness"
:
"AUTO"
,
"operator"
:
"and"
}
}
}
}
{
"took"
:
34
,
"timed_out"
:
false
,
"_shards"
: {
"total"
:
1
,
"successful"
:
1
,
"skipped"
:
0
,
"failed"
:
0
},
"hits"
: {
"total"
: {
"value"
:
2
,
"relation"
:
"eq"
},
"max_score"
:
0.9116078
,
"hits"
: [
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10002"
,
"_score"
:
0.9116078
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"李四"
},
"status"
:
1
}
},
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10001"
,
"_score"
:
0.9116078
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第一中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"张三"
},
"status"
:
1
}
}
]
}
}
跨字段查询
POST http://localhost:9200/order_server/order/_search
{
"query"
: {
"multi_match"
: {
"query"
:
"佳 二"
,
"operator"
:
"and"
}
}
}
{
"took"
:
1626
,
"timed_out"
:
false
,
"_shards"
: {
"total"
:
1
,
"successful"
:
1
,
"skipped"
:
0
,
"failed"
:
0
},
"hits"
: {
"total"
: {
"value"
:
1
,
"relation"
:
"eq"
},
"max_score"
:
1.3093333
,
"hits"
: [
{
"_index"
:
"order_server"
,
"_type"
:
"order"
,
"_id"
:
"10008"
,
"_score"
:
1.3093333
,
"_source"
: {
"extras"
: {
"school_name"
:
"佳木斯第二中学"
,
"grade"
:
"一年级"
,
"class"
:
"二班"
,
"student_name"
:
"老王"
},
"status"
:
1
}
}
]
}
}
更多API Elasticsearch文档 Elasticsearch文档(旧)
mongo-connector同步
- mongo-connector 是一个 python 编写的,用来复制 mongodb 中数据到各种搜索数据库的工具,支持 elasticsearch。依赖python
mongo-connector工具创建一个从MongoDB簇到一个或多个目标系统的管道,目标系统包括:Solr,Elasticsearch,或MongoDB簇。
该工具在MongoDB与目标系统间同步数据,并跟踪MongoDB的oplog,保持操作与MongoDB的实时同步。mongo-connector要求mongo运行在replica-set模式,且需要 elastic2_doc_manager将数据写入ES。
- mongo-connector supports Python 3.4+ and MongoDB versions 3.4 and 3.6
docker 安装
#构建自定义镜像
docker run -it --name mongo-connector python:
3.6
bash
#安装mongo-connector
pip install
'mongo-connector[elastic5]'
#安装pymongo
pip install pymongo
#安装docmanager
pip install
'elastic2-doc-manager[elastic5]'
设置副本集
#启动mongod
sudo ./mongod --replSet
"rs0"
#另一个终端运行mongo
sudo ./mongo
#副本集初始化
rs.initiate()
""
"返回
{
"info2"
:
"no configuration specified. Using a default configuration for the set"
,
"me"
:
"localhost:27017"
,
"ok"
:
1
,
"operationTime"
: Timestamp(
1599449053
,
1
),
"$clusterTime"
: {
"clusterTime"
: Timestamp(
1599449053
,
1
),
"signature"
: {
"hash"
: BinData(
0
,
"AAAAAAAAAAAAAAAAAAAAAAAAAAA="
),
"keyId"
: NumberLong(
0
)
}
}
}
""
"
#验证初始化副本集的配置
rs.conf()
""
"返回
{
"_id"
:
"rs0"
,
"version"
:
1
,
"protocolVersion"
: NumberLong(
1
),
"writeConcernMajorityJournalDefault"
:
true
,
"members"
: [
{
"_id"
:
0
,
"host"
:
"localhost:27017"
,
"arbiterOnly"
:
false
,
"buildIndexes"
:
true
,
"hidden"
:
false
,
"priority"
:
1
,
"tags"
: {
},
"slaveDelay"
: NumberLong(
0
),
"votes"
:
1
}
],
"settings"
: {
"chainingAllowed"
:
true
,
"heartbeatIntervalMillis"
:
2000
,
"heartbeatTimeoutSecs"
:
10
,
"electionTimeoutMillis"
:
10000
,
"catchUpTimeoutMillis"
: -
1
,
"catchUpTakeoverDelayMillis"
:
30000
,
"getLastErrorModes"
: {
},
"getLastErrorDefaults"
: {
"w"
:
1
,
"wtimeout"
:
0
},
"replicaSetId"
: ObjectId(
"5f55a7dd29ddc8eeb2ffcca8"
)
}
}
""
"
#验证副本集的状态
rs.status()
""
"返回
{
"set"
:
"rs0"
,
"date"
: ISODate(
"2020-09-07T03:37:53.317Z"
),
"myState"
:
1
,
"term"
: NumberLong(
1
),
"syncingTo"
:
""
,
"syncSourceHost"
:
""
,
"syncSourceId"
: -
1
,
"heartbeatIntervalMillis"
: NumberLong(
2000
),
"optimes"
: {
"lastCommittedOpTime"
: {
"ts"
: Timestamp(
1599449865
,
1
),
"t"
: NumberLong(
1
)
},
"readConcernMajorityOpTime"
: {
"ts"
: Timestamp(
1599449865
,
1
),
"t"
: NumberLong(
1
)
},
"appliedOpTime"
: {
"ts"
: Timestamp(
1599449865
,
1
),
"t"
: NumberLong(
1
)
},
"durableOpTime"
: {
"ts"
: Timestamp(
1599449865
,
1
),
"t"
: NumberLong(
1
)
}
},
"lastStableCheckpointTimestamp"
: Timestamp(
1599449835
,
1
),
"members"
: [
{
"_id"
:
0
,
"name"
:
"localhost:27017"
,
"health"
:
1
,
"state"
:
1
,
"stateStr"
:
"PRIMARY"
,
"uptime"
:
920
,
"optime"
: {
"ts"
: Timestamp(
1599449865
,
1
),
"t"
: NumberLong(
1
)
},
"optimeDate"
: ISODate(
"2020-09-07T03:37:45Z"
),
"syncingTo"
:
""
,
"syncSourceHost"
:
""
,
"syncSourceId"
: -
1
,
"infoMessage"
:
""
,
"electionTime"
: Timestamp(
1599449053
,
2
),
"electionDate"
: ISODate(
"2020-09-07T03:24:13Z"
),
"configVersion"
:
1
,
"self"
:
true
,
"lastHeartbeatMessage"
:
""
}
],
"ok"
:
1
,
"operationTime"
: Timestamp(
1599449865
,
1
),
"$clusterTime"
: {
"clusterTime"
: Timestamp(
1599449865
,
1
),
"signature"
: {
"hash"
: BinData(
0
,
"AAAAAAAAAAAAAAAAAAAAAAAAAAA="
),
"keyId"
: NumberLong(
0
)
}
}
}
""
"
#ES端同步操作
mongo-connector -m localhost:
27017
-t localhost:
9200
-n runoob.student -d elastic2_doc_manager
Elasticsearch整理笔记(二)相关推荐
- Elasticsearch整理笔记(一)
Elasticsearch定义 elastic(弹性.灵活)+search(搜索) Elasticsearch 是一个支持分布式.高扩展.高实时的高效搜索与数据分析引擎. 支持分布式实时文件存储. 支 ...
- ElasticSearch学习笔记二 初识Elasticsearch
初识Elasticsearch Elasticsearch简介 Elasticsearch术语及概念 文本(Text) 索引词/精确值(term) 精确值 VS 全文 分析(Analysis)与分析器 ...
- 数据结构与算法 整理笔记---二叉搜索树
二叉搜索树 查找问题是计算机中非常重要的基础问题 二分查找法 对于有序数组,才能使用二分查找法(排序作用) public class BinarySearch {public static int b ...
- ElasticSearch学习笔记(二)—结构了解和索引文档增删改
前面学习了ElasticSearch的概况以及一些配套插件的安装.这篇旨在记录对ES结构的了解和一些基本的操作. ElasticSearch结构: 对于ES来说,有几个专有名词.比如索引,类型,id这 ...
- Elasticsearch整理笔记(五)
es6以上版本需要注意的: 1.elasticsearch-head 在连接6.x以上版本时会报406错误: Content-Type header [application/x-www-form-u ...
- 2018/2/11 ELK技术栈之ElasticSearch学习笔记二
终于有时间记录一下最近学习的知识了,其实除了写下的这些还有很多很多,但懒得一一写下了: ElasticSearch添加修改删除原理: ElasticSearch的倒排索引和文档一旦生成就不允许修改(其 ...
- Elasticsearch整理笔记(四)
filter组合查询: POST http://localhost:9200/order_server/order/_search { "_source":false," ...
- Elasticsearch整理笔记(三)
部分内容转载于 https://www.cnblogs.com/fengda/p/10348616.html https://blog.csdn.net/ctwy291314/article/deta ...
- elasticsearch学习笔记——二.querystring查询document方式与dsl查询document方式
// 1.创建test_serach索引,指定主要分片数量,副本分片数量,文档中字段的数据类型,分词器,是否作为搜索条件 PUT test_search { "settings": ...
最新文章
- udacity模型优缺点
- 5G 发展报告:以四项技术为基础,广泛应用还需十年
- php重构ifelse,php - 重构条件语句PHP - SO中文参考 - www.soinside.com
- MySQL row_format引发的案例一则
- 固定资产增值和减值操作
- 生物科学数据分析和数据管理本体论
- zoj 3841 Cards
- 如何在 C# 中使用 Attribute
- 大三、研二的秋招备战路线(Java、大数据)
- git.exe 启动 慢_户外慢生活节来了!南京固城湖水慢城开启春日度假模式
- cvtres.exe无法正常启动_小猿圈讲解Linux系统启动故障解决方案(新手指南)
- 谈一谈chrome浏览器使用
- 优化UITableViewCell高度计算的那些事
- 【CCNA考试】2010-05-24-武汉-872(PASS)
- 项目4:抽奖程序 分时间段(按时段设置的奖品数为概率)
- 自定义View之仿虾米音乐TabLayout
- UIView的bounds、frame、center/position、anchorPoint的关系
- xgboost缺失值处理
- 币小秘:如何才能减少被套,降低风险!
- 【c语言】两个队列实现一个栈
热门文章
- numpy求逆矩阵_线性代数精华2——逆矩阵的推导过程
- GhostNet网络
- python选定区域设置边框_Python教程:巧用openpyxl为指定区域设置边框为粗匣框线...
- js if判断多个条件_五、if 条件判断
- C++:利用sort()对vector中的数据自定义排序
- 【炼丹技巧】指数移动平均(EMA)【在一定程度上提高最终模型在测试数据上的表现(例如accuracy、FID、泛化能力...)】
- 后台获取数据排序后在网页显示(Comparator)
- vue项目:this.function()中关于:this指针失效的问题
- 修复版GEP宝塔内嵌版全解源码
- python 倒数_【IT专家】python实现文件倒数N行读取