《图解TCP/IP》

从事B/S相关的软件开发逃不掉和协议打交道,也应该较为全面和系统的学习理解,TCP/IP 是易懂难精的网络通讯协议群,也是一项基本内功。

封面

这边书讲的范围很大,但是有的细节不够细。

主要偏入门讲个大概笼统的知识。

网络编程这一块还是要去啃《TCP/IP详解 卷2》,可以巩固大学时教的那些网络知识。

子网划分,网络分层模型,TCP/UDP 滑动窗口 写得比较生易懂。

只做了前几章的总结大致如下


协议

协议的出现,目的是为了统一标准。让不同厂商不同设备之前实现互通。

OSI产考模型

OSI 协议是为了让异构的设备之间实现通信而存在的保准化的网络体系结构

分层名称 功能
应用层 针对应用的协议
表示层 设备固有数据格式,和网络标准格式转换
会话层 通信连接管理,负责建立和断开连接
传输层 管理2个节点间的数据传输
网络层 管理地址和路由选择
数据链路层 互连设备之间传送和识别数据帧
物理层 0,1信号传输

应用层

为应用程序提供规定的相关协议,比如redis的通信协议,http协议等都是在应用层

表示层

将设备固有格式转化为网络标准格式,或讲下层的网络标准格式转为上层的设备固有格式

会话层

负责建立和断开通信连接,以及数据的分割等数据传输相关管理

传输层

起到可靠传输的作用,只在通信双方节点上进行处理,而无需在路由器上处理。是物理上的传输逻辑,有重发机制

网络层

将数据传输到目标地址。负责寻址,路由选择。交换机在这一层和下一层

数据链路层

数据帧的生成与接收,负责把0,1转换成电脉冲。网络层负责整个数据发送给目标,数据链路只负责一个分段的数据

物理层

负责0,1比特流

传输方式分类

有连接

TCP是比较典型的有连接方式,会2端对答后发生数据

无连接

UDP是比较典型的无连接方式,直接抛出数据

电路交互&&分组交换

电路交换

  • 交换机直接建立双端通信,直到连接断开
  • 不支持多端通信

分组交换

  • 在交换路由上有缓存队列
  • 多个数据包发送到对机,不直接连接对端而是通过路由交换转发数据

#地址

IP/MAC 地址都具有唯一性,但是只有IP地址具有层次性,MAC地址是真正负责通信的地址。他们通过路由映射表关联起来。

TCP/IP协议

TCP/IP最早用在军业用途,具有开放性和实用性,它的协议繁指协议群包括。
OSI之所以没普及是因为没有尽快制定可行性较强的协议,没有提出应对技术快速革新的协议,以及没有及时后期改良

应用协议

* HTTP
* SMTP
* FTP
* TELNET
* SNMP

传输协议

* TCP
* UDP

网际协议

* IP
* ICMP
* ARP

路由控制协议

* RIP
* OSPF
* BGP

RFC

是一系列以编号排定的文件。文件收集了有关互联网相关信息,以及UNIX和互联网社区的软件文件

包,帧,数据报,段,消息

* 包 
    全能术语
* 帧
    表示数据链路中包的单位
* 数据报
    IP/UDP等网络层以上的包单位
* 段
    TCP数据流中的信息
* 消息
    协议中数据的单位
    

数据收发包

包流动时,从前往后依次附加 以太网包首部,IP包首部,TCP/UDP 包首部, 应用的包首部,数据
每个包首部至少包含两个信息 1.发生接收地址 2.上层协议类型 。经过每个协议分层时,必须有识别包发送端和接收端的信息


以太网帧格式

以太网帧本体前端是以太网首部占14字节
6个字节的目标MAC,6个字节源MAC 2字节类型
46~1500字节的数据
尾部一个FCS(帧验证序列)4字节

常见类型如下


TCP/IP 协议

TCP/IP在OSI参考模型中的 第3层-网络层
网络层的目的是为了实现点对点通信
他的下一层数据链路层是为了互连同一种数据链路的节点之间进行包投递
需要跨不同的数据链路就需要借助网络层

数据链路层负责直连两个网络进行通信传输
网络层IP负责在没有直连的两个网络之间进行通信

IP 采用面向无连接通信,原因有2,1为了简化 2为了提速

IP地址的定义

IP 地址由32位 每8位一组进行表示 ,及一类IP地址有 2 的 32立方个。用子网掩码的方式来进行分组标识

IP地址分类

  • A类

IP地址首位”0”开头 1-8位是网络标识。十进制表示 0.0.0.0 ~ 127.0.0.1,后24位是主机标识

  • B类

IP地址首2位”10” 开头 1-16位是网络表示。十进制表示 128.0.0.1 ~ 191.255.0.0 后 16位表示主机标识

  • C类

IP地址首3位”110” 开头 1-24位是网络表示。十进制表示 192.168.0.0 ~ 239.255.255.0 后 8位表示主机标识

  • D类

IP地址首3位”1110” 开头 1-32位是网络表示。十进制表示 224.0.0.0 ~ 239.255.255.255 没有主机标识,通常用于广播

ps:主机标识位不能全部为0或1,全部为0表示对应信息不可获知,全部为1表示广播

《GO WEB编程》

一本讲GO语言应用的书,本书主要围绕了GO的几个web编程中常用的库展开,帮助读者更好的去理解GO的特性

GO WEB编程 封面

优点

  • 介绍HTTP协议的时候感觉一些描述很到位
1
HTTP 是一种吴状态,由文本构成的请求-响应协议,这种协议使用的是客户端服务器计算模型
  • 如果跟着书去实践去敲代码可以很好的去了解GO的特性

  • 后面的马赛克图片很有意思,可以很好的理解Goroutine的概念和channel

缺点

  • 讲json的时候对注解这块的知识点没提及到
  • 涉及到的库有很多,但是除了http以外的库讲的都不够深和细
  • 因为是新加坡人写的最后一章讲部署的时候未涉及到国内的云厂商
  • 包的引入一些特性书中没涉及到,建议再去刷《GO 语言实战》

ps.放一些自己跟着书练习的小demo,因为是练习所以没注意命名解耦等工业标准,go新手也不了解一些套路 勿喷

demo

《GO 语言实战》

学习一门语言书,文档,实战是最高效的学习方式,刚好这本书讲的《GO 语言实战》命中二者,而且公司刚好有这本书,就借来读了一下

GO 语言实站 封面

Go语言的入门书籍

此书好处是没细纠结于语法和类库直接上代码讲特性

坏处也是语法没细讲需要结合文档来阅读

整体来说这是本入门了解Go特性的书籍非常适合初学者

但是读完这本后需要结合文档,案例或者视频进一步去理解

下面2篇也是就此书写出的一些笔记

go语言的数组切片和映射

Go语言的类型系统-结构体与方法

推荐初学者或者想入门建议阅读它和《GO WEB编程》一本讲基础特性,一本讲实际应用

Mac OS X Mojave 下安装 imagemagick

imagemagick 是处理图片比较优秀的一个C拓展,它为很多语言提供了调用接口。项目组中也刚好用到了 imagemagick 这个C库做一些事情,突然想用GO起个图片处理的服务类似又拍的动态裁图。遇见了下面的问题于是就记录了下来

目前需求

  • 开发环境 Mac OS X Mojave
  • 语言 GO && PHP

不管是哪种语言我们都需要的是安装上 imagemagick这一个工具

在执行

1
go get gopkg.in/gographics/imagick.v2/imagick

抛出如下的异常

1
2
3
4
5
6
7
 ~/code/go/http/14-goroutine  go get gopkg.in/gographics/imagick.v2/imagick
# gopkg.in/gographics/imagick.v2/imagick
../../src/gopkg.in/gographics/imagick.v2/imagick/affine_matrix.go:8:10: fatal error: 'wand/MagickWand.h' file not found
#include <wand/MagickWand.h>
^~~~~~~~~~~~~~~~~~~
1 error generated.

回看 imagick go的文档有这样一段的描述

1
2
3
master (tag v2.x.x): 6.9.0-2 <= ImageMagick <= 6.9.9-35
im-7 (tag v3.x.x): 7.x <= ImageMagick <= 7.x
legacy (tag v1.x.x): 6.7.x <= ImageMagick <= 6.8.9-10

那么我们通过 brew 安装下看下 ImageMagick 的版本

ImageMagick 版本

那么可以认定因为版本原因造成了上述的报错,我们只用调用相应版本的库就好

1
2
3
brew install ImageMagick

go get gopkg.in/gographics/imagick.v3/imagick

源码安Iimagick

imagemagick安装包

1
2
3
4
5
6
curl -O ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar -zxf ImageMagick.tar.gz
cd ImageMagick--7.0.7-21/
./configure --prefix=/usr/local/ImageMagick
make
sudo make install

如果 Mac OS X 下会抛出如下的错误信息,这种头文件丢失的原因是

1
<libxml/parser.h> no such file or directory

Mac os X 10.14已经停止将包含库放置在它们通常的位置,/usr/include只将它们保存在XCode目录中。

但是它保留了一个遗留安装程序可以再老位置安装头文件我们把它放到桌面并且运行

1
2
cp /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg ~/Desktop

然后重新

1
2
make
sudo make install

最后把执行文件的软链接放到相应系统目录就好


参考文档内容

https://imagemagick.org/script/command-line-processing.php

https://silvae86.github.io/sysadmin/mac/osx/mojave/beta/libxml2/2018/07/05/fixing-missing-headers-for-homebrew-in-mac-osx-mojave/

https://github.com/gographics/imagick

《小狗钱钱》

从另外一本理财类书籍转过来的,它推荐这本。读完后发现是一本理财类的少儿读物,打算以后有了孩子讲给儿子听听。

小狗钱钱

这本书和其他理财类书籍一样,通俗的讲明了几个观点

  • 记下成功笔记增加自我信心
  • 做好财务规划准备梦想储存罐
  • 按比例合理分配收入
  • 银行利率是跑不过通货膨胀的
  • 基金/股票是回报率比较大的理财方式
  • 基金/股票 都有风险,并且占用较大精力
  • 长期定投基金有不错的收益,占用精力较小
  • 做自己喜欢的事擅长的事,以此赚钱

总的来说,这本书适合给小朋友讲,或者没有任何理财经验的成年人。同类书推荐《工作前5年,决定你一生的财富》 一个豆瓣博主出的书,是这本书的成人版。

elasticseach自定义analyzer分析器

我们可以自定义一个analyzer,在此之前我们首先应该搞懂, char filter tokenizerfilter。自定义分词器是一个上游的组装容器,拼接这3个方法,包括过滤关键字的规则,如何分词,怎么过滤特征词等等

#多字段属性

  • 厂商名字实现精确匹配
    • 增加一个keyword字段
  • 使用不同的analyzer
    • 不同语言
    • Pinyin字段搜索
    • 为索引和索引指定不同的analyzer

Exact Values 和 Full Text

  • Exact Values 不分词
    • elasticseach 中的 keyword
  • 全文本,非结构化的文本数据 会分词匹配
    • elasticseach 中的 text

自定义分词

当elasticsearch自带的分词器无法满足时,可以自定义分词器。通过自组合不同的组件实现,分析器是三个顺序执行的组件的结合(字符过滤器、分词器、表征过滤器)

  • char_filter 字符过滤器
  • tokenizer 分词器
  • filter 特征过滤器

char_filter

  • 在Tokenizer之前对文本进行处理,例如增加删除及替换字符。可以配置多个Character Filters。会影响Tokenizer的position和offset信息。

  • 一些自带的 Character Filters

    • HTML strip 去除html标签
    1
    2
    3
    4
    5
    6
    7
    POST _analyze
    {
    "tokenizer": "standard"
    , "char_filter": ["html_strip"]

    , "text": ["<a href='#'>aaaa</a>"]
    }
    • Mapping 字符串替换
1
2
3
4
5
6
7
8
9
10
11
 POST _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type":"mapping",
"mappings":["-=>_"]
}
],
"text":"123-asdsd!"
}
* Pattern replace 正则匹配替换
1
2
3
4
5
6
7
8
9
10
11
12
 POST _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type" : "pattern_replace",
"pattern" : "http://(.*)",
"replacement":"$1"
}
],
"text": "http://www.qq.com"
}

Tokenizer (分词器)

  • 将原始的文本按照一定规则,切分为词(trem or token)elasticsearch 内置的 tokenizers如下

    • whitespace 空格分词

      1
      2
      3
      4
      5
      6
      POST _analyze
      {
      "tokenizer": "whitespace",
      "filter": ["lowercase"],
      "text": "xxxooo ooo 111"
      }
    • standard 标准

    • uax_url_email emil/url 方式分词

    • pattern 正则分词

  • keyword 字符串不分词查找

  • path hierarchy 路径查找

     
    1
    2
    3
    4
    5
    POST _analyze
    {
    "tokenizer": "path_hierarchy",
    "text": "a/b/c/d/e/f"
    }

Filter

  • 将 Tokenizer输出的单词(term),进行增加,修改,删除
  • 自带的Token Filters

自定义analyzer

官方文档

固定格式

1
2
3
4
5
6
7
8
9
10
11
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": { 字符过滤器 },
"tokenizer": { 分词器 },
"filter": { 特征过滤器 },
"analyzer": { 组合 }
}
}
}

eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PUT my_test_index
{
"settings": {
"analysis": {
"char_filter":{
"&_to_and": {
"type" : "mapping",
"mappings" : [ "&=> and " ]
}

},
"filter": {
"my_fil":{
"type":"stop",
"stopwords": [ "the", "a" ]
}
},
"analyzer":{
"my_analyzer" : {
"type": "custom",
"char_filter":["&_to_and"],
"tokenizer": "standard",
"filter" : ["my_fil"]
}
}
}
}
}

测试

1
2
3
4
5
POST my_test_index/_analyze
{
"analyzer": "my_analyzer",
"text": "I love PHP&JAVA the ZJJ"
}

  • & 转换成 and
  • the 过滤掉

实验成功

elasticsearch Mapping 设置

elasticsearch 中的 Mapping 类似关系型数据库的schema,也是可以自己进行类型上的设置的。合理的设置可以减小查询速度,和占用空间,也可以保护一些敏感字段不被检索到

Mapping

  • Mapping 类似数据库的 schema 的定义

    • 定义索引中的字段名称
    • 字段的数据类型
    • 字段,倒排索引的相关配置
  • Mapping 会把JSON文档映射成Lucenes所需要的扁平格式

  • 一个Mapping属于一个索引的Type

    • 每个文档都属于一个Type
    • 一个Type有一个Mapping定义
    • 7.0开始,不需要在Mapping定义中指定type信息

    字段的数据类型

    • 简单类型
      • text/keyword
      • Date
      • integer/floating
      • boolean
      • IPV4&IPV6
    • 复杂类型-对象和嵌套对象
      • 对象类型/嵌套类型
    • 特殊类型
      • geo_point & geo_shape /percolator

Dynamic Mapping

 * 在写文档的时候,如果索引不存在,会自动创建索引
 * Dynamic Mapping 的机制,使得我们无需手动定义Mappings。Elasticsearch会自动根据文档信息,推算字段类型
 * 但是有时候会推算不对,比如地理位置
 * 当类型如果设置不对时,会导致一些功能无法正常使用
 

类型自动识别

json类型 elasticsearch类型
字符串 * 匹配日期格式,设置成Date
* 配置数字设置为 float 或者 log 默认关闭
* 设置text,并且增加keyword子字段
布尔值 boolean
浮点数 faloat
整数 long
对象 object
数组 由第一个非空数值的类型决定
空值 忽略

能否更改Mapping字段类型

  • 两种情况
    • Dynamic 设置为true时,一旦有新增字段的文档写入,Mapping也同时被更新
    • Dynamic 设置为false,Mapping不会被更新,新增字段的数据无法被索引,但是信息会出现在_source中
    • Dynamic 设置成为strict,文档写入失败
  • 对已有字段,一旦已经有数据写入,就不会再自持修改自定义
    • Lucene实现的倒排索引,一旦完成就不允许修改
  • 如果希望改变字段类型,必须Reindex API,重建索引
1
2
3
4
5
6
7
8
//设置
PUT users/_mapping
{
"dynamic":false
}

//查看
GET users/_mapping

显示定义Mapping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 PUT users_text
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
},
"tel":{
"type": "text",
"index": false
}
}
}
}

ps:建议建立临时index写入样本创建,避免出错。

禁止查询字段

  • "index": false 不会被索引

index options

  • 四种不同级别的 index options 配置,可以控制倒排索引记录内容

    • docs-记录 doc id
    • freqs-记录 doc id 和 trem frequencies
    • positions-记录 doc id / trem frequencies/term position
    • offsets- doc id / trem frequencies/term position/character offects
  • text 类型默认 positions,其他默认doc id

  • 记录越多,越栈空间

null_value

  • 写入时也需要写入null
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PUT users_text2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
},
"tel":{
"type": "keyword",
"null_value": "NULL"
}
}
}
}

写入

1
2
3
4
5
6
PUT users_text2/_create/2
{
"name":"zjj",
"age":25,
"tel":null
}

copt_to设置

  • _all 在7中被 cpoy_to代替
  • 满足一些特定的搜索需求
  • copy_to 讲字段的数组拷贝到目标字段,实现类似_all作用
  • copy_to的目标字段不出现在_source中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PUT users_3
{
"mappings": {
"properties": {
"name":{
"type": "text",
"copy_to": "userinfo"
},
"age":{
"type": "text",
"copy_to": "userinfo"
}
}
}
}

数组

  • 自己理解这个数组只是逻辑上的加上json
1
2
3
4
5
PUT users_3/_create/2
{
"name":"zjj",
"age":["24","25"]
}

elasticsearch search api

使用 Elasticsearch 提供的,基于JSON格式的更加完备的 query domain specific language (DSL),这边记录的是url search的一些语法

  • 使用 Elasticsearch 提供的,基于JSON格式的更加完备的 query domain specific language (DSL)
  • 在URL中使用查询参数

指定查询索引

语法 范围
/_search 集群上所有的索引
/index1/_search index1
/index1,index-2/_search index1和index2
/index*/_search 以index开头的索引

q|指定查询语句
df|默认字段,不指定会对所有字段进行查询
sort|排序
from/size|分页
Profile|查看查询怎么执行

指定字段查询

1
2
3
4
GET /users/_search?q=zjj&df=name
{
"profile": "true"
}

返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{
"took" : 77,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"age" : "30",
"name" : "zjj"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[dg8Ap-yUQUWlMbgmg6RbdQ][users][0]",
"searches" : [
{
"query" : [
{
"type" : "TermQuery",
"description" : "name:zjj",
"time_in_nanos" : 1619976,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 5181,
"match" : 0,
"next_doc_count" : 2,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 6413,
"build_scorer_count" : 4,
"create_weight" : 73448,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 1534926
}
}
],
"rewrite_time" : 22423,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 1253068,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 15759
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}

范查询

1
2
3
4
GET /users/_search?q=zjj
{
"profile": "true"
}

返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
"took" : 53,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"age" : "30",
"name" : "zjj"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[dg8Ap-yUQUWlMbgmg6RbdQ][users][0]",
"searches" : [
{
"query" : [
{
"type" : "DisjunctionMaxQuery",
"description" : "(age.keyword:zjj | name:zjj | name.keyword:zjj | age:zjj)",
"time_in_nanos" : 14956224,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 2418833,
"match" : 0,
"next_doc_count" : 2,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 18436,
"build_scorer_count" : 4,
"create_weight" : 7110812,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 5408135
},
"children" : [
{
"type" : "TermQuery",
"description" : "age.keyword:zjj",
"time_in_nanos" : 1841882,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 0,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 0,
"build_scorer_count" : 3,
"create_weight" : 1839895,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 1983
}
},
{
"type" : "TermQuery",
"description" : "name:zjj",
"time_in_nanos" : 4720557,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 2,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 1,
"compute_max_score_count" : 1,
"compute_max_score" : 2338296,
"advance" : 2467,
"advance_count" : 2,
"score" : 2636,
"build_scorer_count" : 4,
"create_weight" : 1785375,
"shallow_advance" : 574163,
"create_weight_count" : 1,
"build_scorer" : 17609
}
},
{
"type" : "TermQuery",
"description" : "name.keyword:zjj",
"time_in_nanos" : 35304,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 2,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 1,
"compute_max_score_count" : 1,
"compute_max_score" : 6788,
"advance" : 1100,
"advance_count" : 2,
"score" : 662,
"build_scorer_count" : 4,
"create_weight" : 20786,
"shallow_advance" : 1694,
"create_weight_count" : 1,
"build_scorer" : 4263
}
},
{
"type" : "TermQuery",
"description" : "age:zjj",
"time_in_nanos" : 29197,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 0,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 0,
"advance_count" : 0,
"score" : 0,
"build_scorer_count" : 3,
"create_weight" : 28483,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 710
}
}
]
}
],
"rewrite_time" : 86847,
"collector" : [
{
"name" : "CancellableCollector",
"reason" : "search_cancelled",
"time_in_nanos" : 32828,
"children" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 23114
}
]
}
]
}
],
"aggregations" : [ ]
}
]
}
}

term 与 phrase

  • (term) 苹果 笔 等效于 苹果 OR 笔
  • “phrase” “苹果 笔” 等效于 苹果 AND 笔,先后顺序保持一致
1
2
3
4
5
6
7
8
9
10
11
# 同时包含 Boys The
GET /moves/_search?q=title:"Boys The"
{
"profile": "true"
}

# Boys OR The
GET /moves/_search?q=title:(Boys The)
{
"profile": "true"
}

布尔操作

AND/OR/NOT 或者 &&/||/!

  • 必须大写
  • title:(a not b)
1
2
3
4
GET /moves/_search?q=title:(Perez AND Family)
{
"profile": "true"
}

分组

    • 表示 must
    • 表示 must_not
  • title:(+matrix AND -reloaded)
1
GET /moves/_search?q=title:(-Perez AND +Family)

范围查询

区间表示:[] 闭区间 ,{}开区间
* year : {2019 TO 2018}
* year : [* TO 2018]

算术符号

  • year:> 1980
  • year:(>2010&&<=2018)
  • year:(+>2010+<=2018)
1
GET /moves/_search?q=year:>2014

通配符查询(性能不好)

  • ? 代表1个字符,*代表0或多个字符
    • title:mi?d
    • title:be*
      1
      GET /moves/_search?q=title:c*

正则表

  • title:[bt]oy

模糊匹配与近似查询

  • title:befutifl~1 (1个冗余)
  • title:”lord rings”~2 (相隔位置)
1
GET /moves/_search?q=title:Woter~1

Request Body Search

Query DSL

1
2
3
4
5
6
7
POST /moves/_search
{
"profile": "true"
, "query": {
"match_all": {}
}
}

翻页

1
2
3
4
5
6
7
8
POST /moves/_search
{
"from": 0,
"size": 5,
"profile": "true"
, "query": {
"match_all": {}
}

排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
POST /moves/_search
{
"sort": [
{
"_id": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"profile": "true"
, "query": {
"match_all": {}
}
}

脚本字段

1
2
3
4
5
6
7
8
9
10
11
POST /moves/_search
{
"script_fields": {
"FIELD": {
"script": {
"lang": "painless",
"source": "doc['year'].value + '_aaaa'"
}
}
}
}

表达式(Match)

1
2
3
4
5
6
7
8
9
10
11
POST /moves/_search
{
"query": {
"match": {
"title": {
"query": "dog cat"
, "operator": "and"
}
}
}
}

短语搜索

1
2
3
4
5
6
7
8
9
10
11
POST /moves/_search
{
"query": {
"match_phrase": {
"title": {
"query": "1 3"
, "slop": 1
}
}
}
}

query_string

1
2
3
4
5
6
7
8
9
10
11
12

POST users/_search
{
"profile": "true",
"query": {
"query_string": {
"default_field": "name",
"query": "zhang OR wang"
}
}

}

simple_query_string

  • 类似 query string,但是会忽略错误语法,同时值支持部分查询语法
  • 不支持 AND OR NOT,会当做字符来处理
  • Term 默认关系是OR,可以指定 operator
  • 自持
      • 代替AND
    • | 代替OR
      • 代替 NOT
1
2
3
4
5
6
7
8
9
10
11
POST users/_search
{
"profile": "true",
"query": {
"simple_query_string": {
"query": "zhang wang",
"fields": ["name"],
"default_operator": "AND"
}
}
}

Elasticsearch的CRUD

CRUD

动作 API
index PUT my_index/_doc/1
{“a”:”mike”,”b”,”bb”}
Create PUT my_index/_create/1
{…}
Read GET my_index/_doc/1
Update POST my_index/_update/1
{}
DELETE DELETE my_index/_doc/1

Create

1
2
3
4
5
PUT users/_create/1
{
"name":"zjj",
"age":"25"
}

  • 支持自动生成文档ID和指定文档ID2种方式
  • 调用 POST 动作系统会自动生成document id
  • 使用HTTP PUT 创建时,URL中显示指定 _create,如果ID文档存在,操作失败

GET

1
GET users/_doc/1

  • 找到文档,返回http 200
    • 文档元信息
      • _index/_type/
      • 版本信息,同一个ID的文档,即使被删除Version号也会不断真加
      • _souece 中默认包含了文档的所有原始信息
  • 找不到文档,返回http 404

Index

1
2
3
4
PUT users/_doc/1
{
"age": "30"
}

  • index 和 create 不一样的地方是,create如果存在该数据会报错,index会覆盖。

Update

1
2
3
4
5
6
POST users/_update/1
{
"doc":{
"name":"zjj"
}
}

  • update 和 index的区别是,index是覆盖操作,update会保留原来的数据

Bulk API

  • 在一次API调用中,对不同的索引进行操作
  • 支持CUD和INDEX操作
  • 单条失败其他不影响
  • 返回结果包括了每一条执行
1
2
3
4
5
6
POST _bulk
{ "create":{"_index":"users","_id":"5"}}
{ "name":"dx","age":"25"}
{"update":{"_index":"users","_id":"5"}}
{"name":"dxx"}
{"delete":{"_index":"users","_id":"2"}}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{
"took" : 61,
"errors" : false,
"items" : [
{
"create" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 14,
"_primary_term" : 1,
"status" : 201
}
},
{
"update" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 15,
"_primary_term" : 1,
"status" : 200
}
},
{
"delete" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 16,
"_primary_term" : 1,
"status" : 200
}
}
]
}

mget

  • 批量按照ID获取数据
  • 有一些简洁的写法可以看文档
1
2
3
4
5
6
7
8
9
10
GET /_mget
{
"docs":[
{"_index":"users","_id":"1"},
{"_index":"users","_id":"2"},
{"_index":"users","_id":"3"},
{"_index":"users","_id":"4"},
{"_index":"users","_id":"5"}
]
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
"docs" : [
{
"_index" : "users",
"_type" : "_doc",
"_id" : "1",
"_version" : 12,
"_seq_no" : 11,
"_primary_term" : 1,
"found" : true,
"_source" : {
"age" : "30",
"name" : "zjj"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"found" : false
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"_seq_no" : 13,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "dx",
"age" : "25"
}
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "4",
"found" : false
},
{
"_index" : "users",
"_type" : "_doc",
"_id" : "5",
"_version" : 2,
"_seq_no" : 15,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "dxx",
"age" : "25"
}
}
]
}

msearch

技术群里的菲律宾招聘,待遇真的这样好吗?

博主潜伏技术群多年,经常看见下面这样的一些招聘

PS:主要下方视频为主,其他全是个人观点

菲律宾招聘

菲律宾招聘

不由的羡慕待遇真尼玛的好,在二线城市这样的待遇可以说是翻了2-3倍。诱惑呀,有木有~

据多方了解,他们给出这些高待遇主要是去菲律宾做的一些大陆不让做的网站,他们的盈利方式也很简单,宪法上有的他们都干,比如什么棋牌,赌博,情色,性感荷官在线发牌巴拉巴拉的,然后针对中国市场狠捞一波。

但是下面的疑问来了

  • 在国外大陆管不了,回国会不会落地抓
  • 人在异国他乡合法权利怎么保证?
  • 出了问题会不会如下图这位老哥,毕竟国外劳动法不保护中国公民呀~

额,接下来….


----

最后不由的说一句,这些所谓的HR猎头市场上的钱这么多,咱们赚点体面点的不好吗?

请我喝杯咖啡吧~