Skip to content

ElasticSearch

优点: - 每个字段都被索引(ES中一切字段皆为索引),而且采用的是高效的倒排索引 - 有丰富的分词插件,支持自定义打分和排序,能够实现任意复杂条件的全文检索 - 支持分布式存储

结构

  • Index: 顶层单位
  • Document: 单条记录。许多 Document 构成一个 Index
  • mapping
    • 注意:不能删除 mapping,需要重新生成 index
  • type:
    • Text vs. Keyword: 前者在 indexing 的时候会先被分析拆散
    • Boolean: 也可以用 byte 对应 1, 0

MySql 到 ES 的数据同步

  • 同步双写
    • 关键 bug: 数据一致性,需要放到事务中,影响效率
  • 异步双写: 通过 mq 来写
    • 关键bug: 时效性
  • 定时任务调度:定时轮询 updated 变化的数据
    • 关键bug: 时效性、对数据库的轮询要放在从库上
    • 工具:logstash
  • BinLog 同步
    • 优点:无代码侵入和硬编码
    • 工具:cannal(伪装自己是 slave)、DTS

kibana

api 查询

# 查看索引信息
GET _cat/indices?v

# 查询映射
GET /{index}/_mapping

# 搜索
GET /{index}/_doc/_search

GET /{index}/_doc/_search
{
  "query" : { "match" : { "desc" : "软件" }},
  "sort": [
    { "account_number": "asc" }
    ],
  "from": 1,
  "size": 1
}

# 详情
GET /{index}/_doc/{id}

业务使用

实际上还会有很多细节问题,参考 理解ElasticSearch工作原理 - 简书