es是什么?
独立的网络上的一个或一组进程节点:
- 中间件- 可独立部署的应用程序- 支持分布式- 可以以组为单位管理的进程节点
对外提供搜索服务(http或transport协议):
对内就是一个搜索数据库:
名词定义:
索引=数据库
类型=表(es7已废弃)
文档=行数据
| Relational database | 名词解释 | Elasticsearch | 名词解释 |
|---|---|---|---|
| Database | 数据库 | Index | 索引 |
| Table | 表 | Type | 类型 |
| Row | 行 | Document | 文档 |
| Column | 列 | Field | 字段 |
| Schema | 表结构 | Mapping | 类型映射 |
| Index | 索引 | Everything is indexed | 所有字段都可以作为索引 |
| SQL | Query DSL |
索引:
搜索中的数据库或表定义
构建文档时的索引创建
分词:
搜索是以词为单位做基本的搜索单元
依靠分词器构建分词
用分词器构建倒排索引
TF-IDF打分:
TF:词频
这个document文档包含了多少个这个词,包含越多表明越相关
DF:文档频率
包含该词的文档总数
IDF:DF取反

分布式原理:
分片
主从(主从分片)
路由
设置副本数为1

自动负载均衡
对称集群:NODE1、NODE2、NODE3是完全对称的,都拥有全部副本的能力
关闭一个节点后
新建索引
取回文档**
analyze:分析=分词的过程:
字符过滤器
字符处理
分词过滤(分词转换)
类型:
Text:被分享索引的字符串类型
Keyword:不能被分析,只能被精确匹配的字符串类型
Date:日期类型,可以配合format一起使用
数字类型:不能被分词,有long、integer、short、double等
boolean:true、false
Array:[“one”, “two”]
Object:json嵌套
Ip类型
Geo_point:地理位置
结构化创建索引:
PUT /movie{"settings": {"number_of_shards": 1,"number_of_replicas": 1},"mappings": {"properties": {"title":{"type": "text","analyzer": "english"},"tagline":{"type": "text","analyzer": "english"},"release_date":{"type": "date","format": "8yyyy/MM/dd||yyyy/M/dd||yyyy/MM/d||yyyy/M/d" //匹配日期格式设置},"popularity":{"type": "double"},"overview":{"type": "text","analyzer": "english"},"cast":{"type": "object","properties": {"character":{"type": "text","analyzer":"standard"},"name":{"type": "text","analyzer":"standard"} } } } }}
match和term查询的区别:
match:按照字段上定义的分词分析后去索引内查询
term:不进行分词分析,直接去索引内查询
最小词匹配项(minimum_should_match):
GET /movie/_search{"query": {"match": {"title": {"query": "basketball love aliens","operator": "or", //使用or操作符"minimum_should_match": 2 //至少匹配两个分词项}}}}
短语查询(match_phrase):
GET /movie/_search{"query": {"match_phrase": {"title": "steve zissou"}}}
多字段查询(multi_match):
GET /movie/_search{"query": {"multi_match": {"query": "basketball with cartoom aliens","fields": ["title", "overview"]}}}
TF/IDF/TFNORM:
TF:token frequency
如basketball这样的一个分词在document字段(待搜索字段)中出现的次数
IDF:inverse document frequency
逆文档频率,代表basketball这样的一个分词在整个文档中出现的频率,取反
TFNORM:token frequency nomalized
词频归一化
安装ik分词器:
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
GET _analyze?pretty{"analyzer": "ik_smart","text": "中华人民共和国国歌"}
(2)最大化分词ik_max_word
GET _analyze?pretty{"analyzer": "ik_max_word","text": "中华人民共和国国歌"}
logstash-input-jdbc:
(1)安装:
bin\logstash-plugin install logstash-input-jdbc报错使用:logstash-plugin install --no-verify logstash-input-jdbc
(2)配置文件:
jdbc.conf
input {jdbc {#设置timezonejdbc_default_timezone => "Asia/Shanghai"# mysql 数据库链接,db_dianping为数据库名jdbc_connection_string => "jdbc:mysql://47.97.176.30:3306/db_dianping"# 用户名和密码jdbc_user => "root"jdbc_password => "980517"# 驱动jdbc_driver_library => "D:\Elasticsearch\logstash-7.2.0\logstash-7.2.0\bin\mysql\mysql-connector-java-8.0.17.jar"# 驱动类名jdbc_driver_class => "com.mysql.jdbc.Driver"jdbc_paging_enabled => "true"jdbc_page_size => "50000"last_run_metadata_path => "D:\Elasticsearch\logstash-7.2.0\logstash-7.2.0\bin\mysql\last_value_meta"# 执行的sql 文件路径+名称statement_filepath => "D:\Elasticsearch\logstash-7.2.0\logstash-7.2.0\bin\mysql\jdbc.sql"# 设置监听间隔 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新schedule => "* * * * *"}}output {elasticsearch {# ES的IP地址及端口hosts => ["localhost:9200"]# 索引名称index => "shop"document_type => "_doc"# 自增ID 需要关联的数据库中有有一个id字段,对应索引的id号document_id => "%{id}"}stdout {# JSON格式输出codec => json_lines}}
jdbc.sql
selecta.id,a.name,a.tags,concat(a.latitude,',',a.longitude) as location,a.remark_score,a.price_per_man,a.category_id,b.name as category_name,a.seller_id,c.remark_score as seller_remark_score,c.disabled_flag as seller_disabled_flagfrom t_shop a innerjoin t_category b on a.category_id = b.id innerjoin t_seller c on c.id = a.seller_idwhere a.updated_at > :sql_last_valueor b.updated_at > :sql_last_value or c.updated_at > :sql_last_value
last_value_meta
2020-02-29 12:00:00
(3)启动并指定配置文件:
logstash -f mysql\jdbc.conf
综合查询:
GET /shop/_search{"_source": "*","script_fields": {"distance": {"script": {"source": "haversin(lat, lon, doc['location'].lat, doc['location'].lon)","lang": "expression","params": {"lat":31.23916171,"lon":121.48789949}}}},"query": {"function_score": {"query": {"bool": {"must": [{"match": {"name": {"query": "凯悦","boost": 0.1}}},{"term": {"seller_disabled_flag": 0}}]}},"functions": [{"gauss": {"location": {"origin": "31.23916171,121.48789949","scale": "100km","offset": "0km","decay": 0.5}},"weight": 9},{"field_value_factor": {"field": "remark_score"},"weight": 0.2},{"field_value_factor": {"field": "seller_remark_score"},"weight": 0.1}],"score_mode": "sum","boost_mode": "sum"}},"sort": [{"_score":{"order": "desc"}}],"aggs": {"group_by_tags": {"terms": {"field": "tags"}}}}
gauss衰减:

java接入es方式:
(1)Node接入 node client
(2)Transport接入 transport client
(3)Http接入 rest client
Logstash重建索引:
logstash -f mysql/jdbc.conf
last_value_meta格式:
2020-01-29 00:00:00
mysql开启binlog:
修改my.cnf文件
server-id = 1 //节点id:mysql开启binlog是为了分布式主从同步数据使用的binlog_format = ROW //binlog存储方式:此处是使用行方式log_bin = mysql_bin //binlog文件存放路径,相对路径,存储在mysql安装目录下data下的mysql_bin中
查看是否开启binlog:
show variables like 'log_bin';


使用同义词:
PUT /shop{"settings": {"number_of_shards": 1,"number_of_replicas": 1,"analysis": {"filter": {"my_synonym_filter":{"type":"synonym","synonyms_path":"analysis/synonyms.txt"}},"analyzer":{"ik_syno":{"type":"custom","tokenizer":"ik_smart","filter":["my_synonym_filter"]},"ik_syno_max":{"type":"custom","tokenizer":"ik_max_word","filter":["my_synonym_filter"]}}}},"mappings": {"properties": {"id":{"type": "integer"},"name":{"type": "text","analyzer": "ik_syno_max","search_analyzer": "ik_syno"},"tags":{"type": "text","analyzer": "whitespace","fielddata": true},"location":{"type": "geo_point"},"remark_score":{"type": "double"},"price_per_man":{"type": "integer"},"category_id":{"type": "integer"},"category_name":{"type": "keyword"},"seller_id":{"type": "integer"},"seller_remark_score":{"type": "double"},"seller_disabled_flag":{"type": "integer"}}}}

