备注:
1、query及sort等条件为json格式的一级条件
1、单条件查询
query 中放筛选类型,match为单条件查询,match内部放筛选条件
{
"query": {
"match": {
"seller.tax_code": "92510100MA6C5AH52D"
}
}
}
2、查询结果排序
sort代表排序,与query同一级别,sort内部放排序条件和排序方式(desc 降序,asc 升序)
{
"query": {
"match": {
"seller.tax_code": "92510100MA6C5AH52D"
}
},
"sort": {
"created_at": "asc"
}
}
3、查询结果相关信息
took:查询花费时长(毫秒)
timed_out:请求是否超时
_shards:搜索了多少分片,成功、失败或者跳过了多个分片(明细)
hits.total.value : 查询结果条数 (数量大于10000条时,json中需要加上一个一级条件 "track_total_hits":true ,才会展示实际数量)
hits.total.relation : #查询的关系(我猜测的,哈哈)
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 468,
"relation": "eq"
},
"max_score": null,
"hits": [
{内部数据,
"created_at": 1558073693,
},
"sort": [
1558073693
]
}
]
}
}
4、分页查询
类似sql的limit,from和size都与query同级,from表示从第n-1个记录开始(mysql中第一条是0),size表示查询往后的n条记录。
{
"query": {
"match": {
"seller.tax_code": "92510100MA6C5AH52D"
}
},
"sort": {
"created_at": "asc"
},
"from": "0",
"size": "1"
}
5、多种match(匹配查询)
match 模糊搜索 (搜索中文时,容易出错),分词,包含任一分词就会展示
match_phrase 精准搜索(必须包含一模一样的串,才会返回),分词,包含所以分词就会展示
term查询与match的效果类似,区别是term不分词,match会分词
term查询和match_phrase都是不分词匹配,但是match_phrase经常用来匹配中文字段
terms查询类似于sql中的in效果,同样也是不能分词
可以参考:Elasticsearch match和term查询的区别_大白能的博客-CSDN博客_match查询和term查询区别
{
"query": {
"terms": {
"invoice.invoice_type": ["026", "007","028"]
}
}
}
6、多条件查询(bool联合查询: must,should,must_not,filter)可以相互嵌套使用
must :类似sql的 and
should :类似sql的or
must_not : 类似sql的 not
filter: 作用和must一样,但是仅过滤,不评分,所以性能更高(可以直接替换must,查询结果是一样的)
{
"query":{
"bool":{
"must":[
{"match":{"invoice.invoice_type":"026"}},
{"match":{"merchant.tax_code":"92510100MA6C5AH52D"}},
{"match_phrase":{"buyer.title":"金牛区慧宸"}}
]}},
"sort":{"created_at":"desc"}
}
相互嵌套示例:
{
"query":{
"bool":{
"must":[
{"match":{"merchant.tax_code":"91510100MA6CT7K03N"}},
{"match_phrase":{"buyer.title":"金牛区慧宸"}},
{"bool":{
"should":[
{"match":{"invoice.invoice_type":"026"}},
{"match":{"invoice.invoice_type":"007"}}
]
}}
]
}},
"sort":{"created_at":"desc"}
}