由于elasticSearch版本更新频繁,此笔记适用ES版本为 7.10.2
此笔记摘录自《Elasticsearch搜索引擎构建入门与实战》第一版
文中涉及代码适用于kibana开发工具,其他如es-head则语法会不太相同
elasticSearch学习笔记02 - 搜索功能
es的搜索检索功能十分强大且复杂,此篇来详细了解
1.返回指定字段
_source里传入一个数组指定返回的字段值
GET /fzy_test_hotel/_search
{
"_source": [
"title",
"city"
],
"query": {
"term": {
"字段": {
"value": "字段值"
}
}
}
}
// 例子
GET /fzy_test_hotel/_search
{
"_source": [
"title",
"city"
],
"query": {
"term": {
"字段": {
"value": "字段值"
}
}
}
}
2.计数
类似sql中的count(*)
不过用处不大,普通查询的时候es也会直接返回命中数量
GET /fzy_test_hotel/_count
{
"query": {
"term": {
"字段": {
"value": "北京"
}
}
}
}
// 返回值,count字段返回命中数量
{
"count" : 2,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
}
}
3.分页
“from”: 0, “size”: 5, 控制分页
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"term": {
"city": {
"value": "北京"
}
}
}
}
默认es最多分页查询一万条,修改最大查询数量如下
PUT /fzy_test_hotel/_settings
{
"index": {
"max_result_window": 200
}
}
4.性能分析
“profile”:true, 直接在查询语句上加上 “profile”:true 开启性能分析
会占用资源,也不常用,不做赘述了
GET /fzy_test_hotel/_search
{
"profile":true,
"from": 0,
"size": 5,
"query": {
"term": {
"city": {
"value": "北京"
}
}
}
}
5.全部查询
类似于sql中的 select * from xxxxxx
“boost”: 1 说明所有的结果权重分数都是1
等价于不写请求体默认也是返回全部,不过请求体可以拓展写出更复杂的语法
GET /fzy_test_hotel/_search
{
"query": {
"match_all": {
"boost": 1
}
}
}
// 等价于不写请求体默认也是返回全部
GET /fzy_test_hotel/_search
6.term精准查询
es对term查询不会分词,只会全字匹配, term里能放一个字段的一个值检索,想要多个检索 请看terms
另外查询中时间格式需要和mapping中对应或者处理,不赘述了
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"term": {
"city": {
"value": "北京"
}
}
}
}
6.1 terms
terms支持传一个数组
GET /fzy_test_hotel/_search
{
"profile":true,
"from": 0,
"size": 5,
"query": {
"terms": {
"city": ["北京" ,"上海" ]
}
}
}
7.范围查询range
范围查询只能查询double 和日期类型
gte 大于等于
lte 小于等于
gt 大于
lt 小于
GET /fzy_test_hotel/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 20
}
}
}
}
8.exists不为空查询
“exists” 只能查到不为空的文档,如果为空则不会命中,类似sql中的not null
field 后面填写字段名
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"exists": {
"field": "不为空的字段名"
}
}
}
9.Boolean布尔查询
布尔查询是查询中的重头戏,此博客为笔记,就不详细讲解了;
注意,其中传入的是一个数组,每个功能在数组中可以设置多个条件
其中包含如下几个功能
must -------------------与----------匹配must中的全部----------类似sql中的and
should-----------------或----------其中有一个匹配上就行----------类似sql中的or
must_not--------------非----------必须不匹配----------类似sql中的 不等于
filter -----------------必须匹配过滤条件,且不进行打分计算
GET /fzy_test_hotel/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"price": {
"gte": 0,
"lte": 1000000
}
}
},
{
"term": {
"city": {
"value": "北京"
}
}
}
],
"should": [
{
"term": {
"city": {
"value": "北京"
}
}
},
{
"term": {
"city": {
"value": "天津"
}
}
}
],
"must_not": [
{
"term": {
"city": {
"value": "上海"
}
}
}
]
}
}
}
9.1 filter 查询
filter查询时boolean中比较特殊的查询,就像一个特殊的must,filter查询不对结果进行打分计算,但会对部分查询结果进行缓存,提升下一次查询速度。
GET /fzy_test_hotel/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"city": "北京"
}
},
{
"range": {
"price": {
"gte": 0,
"lte": 1000000
}
}
}
]
}
}
}
9.2 filter + constant_score 查询
默认的filter是没有打分的,但是却可以使用constant_score 手动打分,boost设置所有命中的得分
GET /fzy_test_hotel/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"city": "北京"
}
},
"boost": 1.2 // 所有命中的得分都是1.2
}
}
}
9.2 function_score查询
可以自定义打分算法 根据自定义的打分函数来计算得分进行排序,语法比较古怪
function_score 中包含有 functions 和 score_mode
GET /fzy_test_hotel/_search
{
"query": {
"function_score": {
"query": {
"term": {
"city": {
"value": "天津"
}
}
},
"functions": [
{
"random_score": {}
}
],
"score_mode": "sum"
}
}
}
10 match 查询
match查询会根据es中的分词器进行分词后进行检索
es首先会把你的检索字符串进行分词拆分,然后再把检索的数据text格式的字段进行分词,并把两者相匹配,并根据匹配度进行打分
网上讲es match 查询分词的很多,此处不再赘述了,只做一个语法记录;
需要注意的是 用match也能检索keyword ;用term也能检索text; 只不过都会变成精准检索。
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"match": {
"city": "北京大学" // 会检索出包含 北京 和 大学 的文档,而两个词没必要挨在一起也能被检索出
}
}
}
multi_match可以同时在多个字段检索,等同于加了一个should
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"multi_match": {
"query": "北京" ,
"fields": ["city","title"]
}
}
}
match_phrase 就不分词检索了,又变成了完全匹配,es好多语法都是重复的
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"match_phrase": {
"title": "北京大学"
}
}
}
11 地理位置查询
es 内部集成了可检索坐标范围检索算法
此代码查询了距离坐标位置为圆心5公里内的所有匹配结果
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"geo_distance": {
"distance": "5km",
"location":{
"lat": 40.73,
"lon": -74.1
}
}
}
}
geo_bounding_box 和上面不同的是矩形查询
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.73,
"lon": -75.1
}
}
}
}
}
12 搜索建议查询
搜索建议的字段类型必须为 completion 类型
其实就是一个前缀搜索,每次输入前缀会返回匹配前缀的完整字段
例如你搜【如家】返回的是【如家精选酒店】【如家快捷酒店】
返回值的封装并不在hit里这块自己尝试看看
GET /fzy_test_hotel/_search
{
"from": 0,
"size": 5,
"suggest": {
"hahahaha": {
"prefix": "北",
"completion": {
"field": "title"
}
}
}
}
13 排序
类似于sql中的order by
但es默认是会根据相关性打分排序
可以根据多个字段排序,这里代码不演示了;
多个字段排序的优先级和字段先后顺序相同
GET /fzy_test_hotel/_search
{
"profile": true,
"from": 0,
"size": 5,
"query": {
"terms": {
"city": [
"北京",
"上海"
]
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
还有根据地理位置排序的功能,太麻烦,这里不再赘述了
END