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
- 谷歌浏览器提供了插件: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
}
}
]
}
}
-
-
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