elasticsearch系列笔记(四) 搜索

路由文档到分片

1
shard = hash(routing) % number_of_primary_shards

replication

请求是否需要等待复制分区的成功响应,默认为sync等待,async异步不等待。 

consistency

文档在写入的时候,需要规定一定的分区更新成功之后,才会返回客户端成功,如果节点不够,则不能删除或者索引任何文件

int((primary + number_of_replicas) / 2) + 1

空搜索

1
2
3
4
5
6
7
8
9
GET /_search
hits:
total 总文档数
hits 匹配到的数量
max_score 最大相关性
took:请求时间
_shards: 用到的分片大小和类型
GET /_search?size={size}&from={from}
from开始最多size个内容返回

简易搜索

1
2
3
GET /_all/{type}/_search?q={field}:{key}
GET /_all/{type}/_search?q=`urlencode(+{field}:{key}-{field}:{key})` +条件满足 -条件不满足
GET /_all/{type}/_search?q={value} 查询包含value的文档

新建索引,设置分析器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PUT /{index}
{
"mappings": {
"{type}": {
"properties": {
"field": {
"type": "", //string, date, long, object(嵌套)
"index": "", // analyzed, not_analyzed, no
"analyzer": "", // english, space
}
}
}
}
}

测试分析器

1
GET /{index}/_analyze?field={field}&text={value}