es是什么?

独立的网络上的一个或一组进程节点:

  1. - 中间件
  2. - 可独立部署的应用程序
  3. - 支持分布式
  4. - 可以以组为单位管理的进程节点

对外提供搜索服务(http或transport协议):

对内就是一个搜索数据库:

名词定义:

索引=数据库

类型=表(es7已废弃)

文档=行数据

Relational database 名词解释 Elasticsearch 名词解释
Database 数据库 Index 索引
Table 表 Type 类型
Row 行 Document 文档
Column 列 Field 字段
Schema 表结构 Mapping 类型映射
Index 索引 Everything is indexed 所有字段都可以作为索引
SQL Query DSL

索引:

搜索中的数据库或表定义

构建文档时的索引创建

image.png
image.png

分词:

搜索是以词为单位做基本的搜索单元

依靠分词器构建分词

用分词器构建倒排索引

TF-IDF打分:

TF:词频

这个document文档包含了多少个这个词,包含越多表明越相关

DF:文档频率

包含该词的文档总数

IDF:DF取反

image.png

分布式原理:

分片

主从(主从分片)

路由

设置副本数为1
image.png
image.png
自动负载均衡
image.png
对称集群:NODE1、NODE2、NODE3是完全对称的,都拥有全部副本的能力
关闭一个节点后
image.png
新建索引
image.png
取回文档**
image.png

analyze:分析=分词的过程:

字符过滤器

字符处理

分词过滤(分词转换)

类型:

Text:被分享索引的字符串类型

Keyword:不能被分析,只能被精确匹配的字符串类型

Date:日期类型,可以配合format一起使用

数字类型:不能被分词,有long、integer、short、double等

boolean:true、false

Array:[“one”, “two”]

Object:json嵌套

Ip类型

Geo_point:地理位置

结构化创建索引:

  1. PUT /movie
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1
  6. },
  7. "mappings": {
  8. "properties": {
  9. "title":{
  10. "type": "text",
  11. "analyzer": "english"
  12. },
  13. "tagline":{
  14. "type": "text",
  15. "analyzer": "english"
  16. },
  17. "release_date":{
  18. "type": "date",
  19. "format": "8yyyy/MM/dd||yyyy/M/dd||yyyy/MM/d||yyyy/M/d" //匹配日期格式设置
  20. },
  21. "popularity":{
  22. "type": "double"
  23. },
  24. "overview":{
  25. "type": "text",
  26. "analyzer": "english"
  27. },
  28. "cast":{
  29. "type": "object",
  30. "properties": {
  31. "character":{
  32. "type": "text",
  33. "analyzer":"standard"
  34. },
  35. "name":{
  36. "type": "text",
  37. "analyzer":"standard"
  38. } } } } }}

match和term查询的区别:

match:按照字段上定义的分词分析后去索引内查询

term:不进行分词分析,直接去索引内查询

最小词匹配项(minimum_should_match):

  1. GET /movie/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "basketball love aliens",
  7. "operator": "or", //使用or操作符
  8. "minimum_should_match": 2 //至少匹配两个分词项
  9. }
  10. }
  11. }
  12. }

短语查询(match_phrase):

  1. GET /movie/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": "steve zissou"
  6. }
  7. }
  8. }

多字段查询(multi_match):

  1. GET /movie/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "basketball with cartoom aliens",
  6. "fields": ["title", "overview"]
  7. }
  8. }
  9. }

TF/IDF/TFNORM:

TF:token frequency

如basketball这样的一个分词在document字段(待搜索字段)中出现的次数

IDF:inverse document frequency

逆文档频率,代表basketball这样的一个分词在整个文档中出现的频率,取反

TFNORM:token frequency nomalized

词频归一化

安装ik分词器:

  1. elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.2.0/elasticsearch-analysis-ik-7.2.0.zip

(1)智能分词ik_smart

  1. GET _analyze?pretty
  2. {
  3. "analyzer": "ik_smart",
  4. "text": "中华人民共和国国歌"
  5. }

(2)最大化分词ik_max_word

  1. GET _analyze?pretty
  2. {
  3. "analyzer": "ik_max_word",
  4. "text": "中华人民共和国国歌"
  5. }

logstash-input-jdbc:

(1)安装:

  1. bin\
  2. logstash-plugin install logstash-input-jdbc
  3. 报错使用:
  4. logstash-plugin install --no-verify logstash-input-jdbc

(2)配置文件:

jdbc.conf

  1. input {
  2. jdbc {
  3. #设置timezone
  4. jdbc_default_timezone => "Asia/Shanghai"
  5. # mysql 数据库链接,db_dianping为数据库名
  6. jdbc_connection_string => "jdbc:mysql://47.97.176.30:3306/db_dianping"
  7. # 用户名和密码
  8. jdbc_user => "root"
  9. jdbc_password => "980517"
  10. # 驱动
  11. jdbc_driver_library => "D:\Elasticsearch\logstash-7.2.0\logstash-7.2.0\bin\mysql\mysql-connector-java-8.0.17.jar"
  12. # 驱动类名
  13. jdbc_driver_class => "com.mysql.jdbc.Driver"
  14. jdbc_paging_enabled => "true"
  15. jdbc_page_size => "50000"
  16. last_run_metadata_path => "D:\Elasticsearch\logstash-7.2.0\logstash-7.2.0\bin\mysql\last_value_meta"
  17. # 执行的sql 文件路径+名称
  18. statement_filepath => "D:\Elasticsearch\logstash-7.2.0\logstash-7.2.0\bin\mysql\jdbc.sql"
  19. # 设置监听间隔 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新
  20. schedule => "* * * * *"
  21. }
  22. }
  23. output {
  24. elasticsearch {
  25. # ES的IP地址及端口
  26. hosts => ["localhost:9200"]
  27. # 索引名称
  28. index => "shop"
  29. document_type => "_doc"
  30. # 自增ID 需要关联的数据库中有有一个id字段,对应索引的id号
  31. document_id => "%{id}"
  32. }
  33. stdout {
  34. # JSON格式输出
  35. codec => json_lines
  36. }
  37. }

jdbc.sql

  1. select
  2. a.id,a.name,a.tags,concat(a.latitude,',',a.longitude) as location,
  3. a.remark_score,a.price_per_man,a.category_id,b.name as category_name,
  4. a.seller_id,c.remark_score as seller_remark_score,
  5. c.disabled_flag as seller_disabled_flag
  6. from t_shop a inner
  7. join t_category b on a.category_id = b.id inner
  8. join t_seller c on c.id = a.seller_id
  9. where a.updated_at > :sql_last_value
  10. or b.updated_at > :sql_last_value or c.updated_at > :sql_last_value

last_value_meta

  1. 2020-02-29 12:00:00

(3)启动并指定配置文件:

  1. logstash -f mysql\jdbc.conf

综合查询:

  1. GET /shop/_search
  2. {
  3. "_source": "*",
  4. "script_fields": {
  5. "distance": {
  6. "script": {
  7. "source": "haversin(lat, lon, doc['location'].lat, doc['location'].lon)",
  8. "lang": "expression",
  9. "params": {
  10. "lat":31.23916171,
  11. "lon":121.48789949
  12. }
  13. }
  14. }
  15. },
  16. "query": {
  17. "function_score": {
  18. "query": {
  19. "bool": {
  20. "must": [
  21. {
  22. "match": {
  23. "name": {
  24. "query": "凯悦",
  25. "boost": 0.1
  26. }
  27. }
  28. },
  29. {
  30. "term": {
  31. "seller_disabled_flag": 0
  32. }
  33. }
  34. ]
  35. }
  36. },
  37. "functions": [
  38. {
  39. "gauss": {
  40. "location": {
  41. "origin": "31.23916171,121.48789949",
  42. "scale": "100km",
  43. "offset": "0km",
  44. "decay": 0.5
  45. }
  46. },
  47. "weight": 9
  48. },
  49. {
  50. "field_value_factor": {
  51. "field": "remark_score"
  52. },
  53. "weight": 0.2
  54. },
  55. {
  56. "field_value_factor": {
  57. "field": "seller_remark_score"
  58. },
  59. "weight": 0.1
  60. }
  61. ],
  62. "score_mode": "sum",
  63. "boost_mode": "sum"
  64. }
  65. },
  66. "sort": [
  67. {
  68. "_score":{
  69. "order": "desc"
  70. }
  71. }
  72. ],
  73. "aggs": {
  74. "group_by_tags": {
  75. "terms": {
  76. "field": "tags"
  77. }
  78. }
  79. }
  80. }

gauss衰减:

image.png

java接入es方式:

(1)Node接入 node client

(2)Transport接入 transport client

(3)Http接入 rest client

Logstash重建索引:

  1. logstash -f mysql/jdbc.conf

last_value_meta格式:

  1. 2020-01-29 00:00:00

mysql开启binlog:

修改my.cnf文件

  1. server-id = 1 //节点id:mysql开启binlog是为了分布式主从同步数据使用的
  2. binlog_format = ROW //binlog存储方式:此处是使用行方式
  3. log_bin = mysql_bin //binlog文件存放路径,相对路径,存储在mysql安装目录下data下的mysql_bin中

查看是否开启binlog:

  1. show variables like 'log_bin';

image.png
image.png

使用同义词:

  1. PUT /shop
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1,
  6. "analysis": {
  7. "filter": {
  8. "my_synonym_filter":{
  9. "type":"synonym",
  10. "synonyms_path":"analysis/synonyms.txt"
  11. }
  12. },
  13. "analyzer":{
  14. "ik_syno":{
  15. "type":"custom",
  16. "tokenizer":"ik_smart",
  17. "filter":["my_synonym_filter"]
  18. },
  19. "ik_syno_max":{
  20. "type":"custom",
  21. "tokenizer":"ik_max_word",
  22. "filter":["my_synonym_filter"]
  23. }
  24. }
  25. }
  26. },
  27. "mappings": {
  28. "properties": {
  29. "id":{
  30. "type": "integer"
  31. },
  32. "name":{
  33. "type": "text",
  34. "analyzer": "ik_syno_max",
  35. "search_analyzer": "ik_syno"
  36. },
  37. "tags":{
  38. "type": "text",
  39. "analyzer": "whitespace",
  40. "fielddata": true
  41. },
  42. "location":{
  43. "type": "geo_point"
  44. },
  45. "remark_score":{
  46. "type": "double"
  47. },
  48. "price_per_man":{
  49. "type": "integer"
  50. },
  51. "category_id":{
  52. "type": "integer"
  53. },
  54. "category_name":{
  55. "type": "keyword"
  56. },
  57. "seller_id":{
  58. "type": "integer"
  59. },
  60. "seller_remark_score":{
  61. "type": "double"
  62. },
  63. "seller_disabled_flag":{
  64. "type": "integer"
  65. }
  66. }
  67. }
  68. }