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 ,才会展示实际数量)
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 194,
"max_score": 1,
"hits": [{}, {}]
}
}
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效果,同样也是不能分词:
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": [
{
"term": {
"supply_id": {
"value": "93",
"boost": 1
}
}
},
{
"match": {
"item_title": {
"query": "康巴",
"operator": "OR",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1
}
}
},
{
"term": {
"style_code": {
"value": "QATD210112172836000000339",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}