文章目录
- 前言
- 一、数据查询
- URL格式
- 6.1 返回所有记录
- 6.2 全文搜索
- 6.3 逻辑运算
- 常用查询:
- 下面是 demo:
- 全局匹配:(默认返回10条)
- match 匹配: title = “串串” 分页 from 10 共 size 10
- match 匹配: title = “串串” 排序 order = desc
- mutil_match 查询
- query_string 语法查询: “query”: “(关键字 and 关键字) or 关键字” 各种关键字 全局搜索
- query_string 可以限定 查询字段(默认查询所有字段)
- filter 查询:
- constant_score: 固定分数。 (指定 boost 匹配分数是 2 的结果,默认不填是 1)
- bool 查询
- must: 是 类似 and
- must_not :不等于
- should: 类似 or
- should 语法查询
- 不等于 must_not
- 不等于+等于双条件查询
- 不等于+不等于双条件查询
- 正则表达式过滤regexp
- ElasticSearch查询例子
- 不错文档
前言
一、数据查询
URL格式
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
这里的query代表一个查询对象,里面可以有不同的查询属性
- 查询类型:
例如:match_all, match,term , range 等等 - 查询条件:查询条件会根据类型的不同,写法也有差异
在URL中指定特殊的索引和类型进行多索引,多type搜索
- /_search:在所有的索引中搜索所有的类型
- /school/_search:在 school 索引中搜索所有的类型
- /school,ad/_search:在 school 和ad索引中搜索所有的类型
- /s*,a*/_search:在所有以g和a开头的索引中所有所有的类型
- /school/student/_search:在school索引中搜索student类型
- /school,ad/student,phone/_search:在school和ad索引上搜索student和phone类型
- /_all/student,phone/_search:在所有的索引中搜索student和phone类型
6.1 返回所有记录
使用 GET 方法,请求层级 /Index/Type/_search
$ curl 'localhost:9200/accounts/person/_search'
{
"took":2,
"timed_out":false,
"_shards":{"total":5,"successful":5,"failed":0},
"hits":{
"total":2,
"max_score":1.0,
"hits":[
{
"_index":"accounts",
"_type":"person",
"_id":"AV3qGfrC6jMbsbXb6k1p",
"_score":1.0,
"_source": {
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}
},
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_score":1.0,
"_source": {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理,软件开发"
}
}
]
}
}
上面代码中,里面子字段的含义如下。
took:查询花费时间,单位是毫秒
time_out:是否超时
_shards:分片信息
hits:搜索结果总览对象
total:搜索到的总条数
max_score:所有结果中文档得分的最高分
hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
_index:索引库
_type:文档类型
_id:文档id
_score:文档得分
_source:文档的源数据
6.2 全文搜索
Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" :
{ "match" :
{ "desc" : "软件" }
}
}
指定source中某字段查询
上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含"软件"这个词。返回结果如下。
{
"took":3,
"timed_out":false,
"_shards":{"total":5,"successful":5,"failed":0},
"hits":{
"total":1,
"max_score":0.28582606,
"hits":[
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_score":0.28582606,
"_source": {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理,软件开发"
}
}
]
}
}
size返回条数
Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"size": 1
}'
上面代码指定,每次只返回一条结果。
还可以通过from字段,指定位移。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"from": 1,
"size": 1
}'
上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。
6.3 逻辑运算
如果有多个搜索关键字, Elastic 认为它们是or关系。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "软件 系统" }}
}'
上面代码搜索的是 软件 or 系统。
如果要执行多个关键词的and搜索,必须使用布尔查询。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "软件" } },
{ "match": { "desc": "系统" } }
]
}
}
}'
常用查询:
全文本查询:针对文本
1、查询全部:match_all
2、模糊匹配: match (类似sql 的 like)
3、全句匹配: match_phrase (类似sql 的 = )
4、多字段匹配:muti_match (多属性查询)
5、语法查询:query_string (直接写需要配置的 关键字 )
6、字段查询 : term (针对某个属性的查询,这里注意 term 不会进行分词,比如 在 es 中 存了 “火锅” 会被分成 “火/锅” 当你用 term 去查询 “火时能查到”,但是查询 “火锅” 时,就什么都没有,而 match 就会将词语分成 “火/锅”去查)
7、范围查询:range ()
字段查询:针对结构化数据,如数字,日期 。。。
分页:
“from”: 10,
“size”: 10
constant_score: 固定分数。
filter: 查询: (query 属于类似就可以查出来,而 filter 类似 = 符号,要么成功,要么失败,没有中间值,查询速度比较快)
下面是 demo:
全局匹配:(默认返回10条)
127.0.0.1:9200/shop-index/_search
{
"query": { "match_all": {}}
}
POST 请求 ip:9200/shop/_search
match 匹配: title = “串串” 分页 from 10 共 size 10
{
"query": {
"match": {"title": "串"}
},
"from": 10,
"size": 10
}
POST 请求 ip:9200/shop/_search
match 匹配: title = “串串” 排序 order = desc
{
"query": {
"match": {"title": "串"}
},
"sort": [
{"id": {"order": "desc" }}
],
"from": 10,
"size": 10
}
mutil_match 查询
:“query”: “串串”, 为要查寻的关键字,“fields”: [ “title”, “tag”] 从 title 和 tag 属性中去找。有一个匹配就算成功。
{
"query": {
"multi_match": {
"query": "串串",
"fields": [ "title", "tag"]
}
}
}
query_string 语法查询: “query”: “(关键字 and 关键字) or 关键字” 各种关键字 全局搜索
{
"query": {
"query_string": {
"query": "(水煮肉 and 回锅肉) or 西葫芦"
}
}
}
query_string 可以限定 查询字段(默认查询所有字段)
{
"query": {
"query_string": {
"query": "(水煮肉 and 回锅肉) or 西葫芦",
"fields": ["title" ]
}
}
}
filter 查询:
{"query": {
"bool": {
"filter": {
"term": {"id": "13"}
}
}
}
}
constant_score: 固定分数。 (指定 boost 匹配分数是 2 的结果,默认不填是 1)
固定分数查询不支持 match 只支持 filter
{
"query": {
"constant_score": {
"filter": {
"match": {
"title": "火锅"
}
},
"boost": 2
}
}
}
bool 查询
must: 是 类似 and
must_not :不等于
should: 类似 or
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "火锅"
}
},
{
"match": {
"tag": "串串"
}
}
]
}
}
}
should 语法查询
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "串串"
}
},
{
"match": {
"tag": "西葫芦"
}
}
]
}
}
}
不等于 must_not
不等于+等于双条件查询
不等于+不等于双条件查询
正则表达式过滤regexp
注意插入.要用\.,表示选择要用,匹配16-32之间的整数,只能[16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31],不能[16-31],但是可以[0-9])
匹配10.xxx.xxx.xxx的局域网地址:10\…+
匹配172.16-31.xxx.xxx的局域网地址:172\.[16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31]\…+
匹配192.168.xxx.xxx的局域网地址:192\.168\…+
匹配127.xxx.xxx.xxx的本地地址:127\…+
ElasticSearch查询例子
1.中文模糊匹配,(分词匹配)
{
"query": {
"match": {
"name": "张三"//匹配的字段和关键字
}
},
"sort": {
"price": { //排序的字段
"order": "desc" //倒序
}
},
"size":20 //每页查询数量
}
2.中文模糊匹配,(不分词)
{
"query": {
"match_phrase": {
"name": "张三" //匹配的字段和关键字
}
},
"sort": {
"price": {
"order": "desc"
}
},
"size":20
}
3.布尔查询
{
"query": {
"bool": {
"must": [{//多字段匹配,两个必须同时匹配到
"match": {
"name":"屈"
}
}, {
"match": {
"bank":"中国"
}
}],
"filter": {//同时满足字段
"term": {//完全匹配
"pay_status":"4"
}
},
"must_not": [{//过滤匹配到这个关键字
"match": {
"phone":"2303"
}
}]
}
},
"sort": {
"price":{
"order": "desc"
}
}
}
4.布尔查询
{
"query": {
"bool": {
"should": [{//两个中满足一个即可匹配
"match": {
"name": "屈"
}
}, {
"match": {
"bank": "中国银行总行"
}
}],
"filter": {
"term": {
"pay_status": 3
}
}
}
},
"sort": {
"spend_price": {
"order": "desc"
}
}
}
5.多字段匹配,生成多个match查询
{
"query": {
"multi_match": {
"fields": ["bank", "name"],
"query": "工商",
"fuzziness": "AUTO"
}
}
}
6.聚合查询
{
"query": {//查询所有数据
"match_all": {}
},
"aggs": {
"group_by_name": {//根据pay_status分组
"terms": {
"field": "pay_status",
"size": 10000,
"order": {
"price": "desc"
}
},
"aggs": {
"price": {
"sum": {
"field": "price"//根据金额总和排序
}
}
}
}
}
}
7.范围筛选
{
"query": {
"bool": {
"must": [{
"match": {
"name": "屈"
}
}, {
"range": {
"price": {
"gte": 1,//金额大于等于1
"lte": 100金额小于等于100
}
}
}]
}
}
}