Elastic Stack--04-1--Kibana常用语法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


Kibana常用语法

在ElasticSearch中支持两种检索方式

  • 通过使用REST request URL 发送检索参数(uri+检索参数)
  • 通过使用 REST request body 来发送检索参数 (uri+请求体)

在这里插入图片描述

1.所有

GET arranged-lesson/all-lesson-match/_search
{

  "query": { "match_all": {} }

}

2.单个属性精确查询

GET arranged-lesson/all-lesson-match/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "lesMatchId": "9997163"
        }
      }
    }
  }
}

3.某个属性模糊匹配

GET /lesson/courses-teacher/_search
{
  "query": {
   "match": {
     "classType": "0"
   }
  }
}

4.某个属性是否存在

GET waiting-lesson/lesson-match/_search
{
    "query": {
        "bool": {
            "filter": {
                "exists": {
                    "field": "studyPurpose"
                }
            }
        }
    }
}

GET /lesson/courses-teacher/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "excessClassHourPush"
        }
      }
    }
  }
}

5. 多条件过滤

GET /lesson/courses-teacher/_search
{
    "query": {
        "bool": {
            "filter": [{
                "terms": {
                    "intTimeType": [1,2,4]
                }
            },{
              "term":{"bu":1}
            }]
            
        }
    }
}

6.多条件should/bool查询

GET lesson/courses-teacher/_search
{"query": {
  "bool": {
   
    "should": [
      {"bool": {
        "filter": [
          {"term": {"teaScope": 0}},
            {"term": {"excessClassHourPush": 0}},
          {"range": {"lesHoursLastWeek" : {"gte": 40}}}
          
          ]
        
      }},
           
          
           { "bool": {
             "filter": [
              {"term": {"teaScope": 1}},
                {"term": {"excessClassHourPush": 0}},
              {"range": {"lesHoursLastWeek" : {"gte": 40}}}
              
              ]
             
        
      }},
      
            { "bool": {
             "filter": [
              {"term": {"excessClassHourPush": 1}}
              ]
        
      }}
      ]
  }
}}

7.依据ID更新

POST /lesson/courses-teacher/105019/
{
          "teacherId": 105019,
          "teacherUserId": 1001766354,
          "teaMobile": "13900098436"
}

8.查看脚本

GET _scripts/auto-course

9.创建脚本

POST _scripts/auto-course
{
  "script": {
    "lang": "mustache",
    "source": "Math.log(_score * 2) + params.my_modifier"
  }
}

10.删除脚本

DELETE _scripts/auto-course

11.入参使用脚本

GET lesson/courses-teacher/_search/template
{
  "id": "auto-course",
  "params": {"regularLesCountPer":0.75,"beforeStartTime":0,"curGrade":"高一","subject":"语文","sWeekNewSignTLesNum":0,"clauses":"{\"bool\":{\"should\":[{\"bool\":{\"filter\":[{\"term\":{\"teaScope\":1}},{\"term\":{\"excessClassHourPush\":0}},{\"range\":{\"lesHours2\":{\"lte\":420}}},{\"range\":{\"lesMinute30Day\":{\"lte\":7800}}},{\"range\":{\"lesHoursLastWeek\":{\"lte\":1980}}},{\"range\":{\"lesHoursLastLastWeek\":{\"lte\":1980}}},{\"range\":{\"curWeekLesHours\":{\"lte\":1980}}}]}},{\"bool\":{\"filter\":[{\"term\":{\"teaScodpe\":2}},{\"term\":{\"excessClassHourPush\":0}},{\"range\":{\"lesHours2\":{\"lte\":420}}},{\"range\":{\"lesMinute30Day\":{\"lte\":7800}}},{\"range\":{\"lesHoursLastWeek\":{\"lte\":1980}}},{\"range\":{\"lesHoursLastLastWeek\":{\"lte\":1980}}},{\"range\":{\"curWeekLesHours\":{\"lte\":1980}}}]}},{\"bool\":{\"filter\":[{\"term\":{\"excessClassHourPush\":1}}]}}]}},{\"terms\":{\"teaLevels.keyword\": [\"高中-语文-4\",\"高中-语文-3\",\"高中-语文-2\",\"高中-语文-1\"]}},","AreaPer":0.45,"canPartTime":"全职","curMonthPer":0.6,"evaluation":"","freeDay":"lesHours2","localTeaRegion":"上海","sellerId":2001771012,"needTeaMark":"","stuCountPer":0.4,"sWeekEscapeRate":0.0,"maxStuCount":125,"rank":"普通","curSubject":"语文","from":0,"isChangeTeacher":0,"sellerGoodPer":0.05,"autoLesName":"autoLesStatus","area":"北京","baoheduRatio":0.35000000000000003,"score-script":"auto-course-score-part","couId":26624965,"remarkPer":0.5,"afterEndTime":0,"maxRemarkPoint":1.0,"isPaid":0,"size":20,"grade":"(\"高一\") OR (\"高中\")","ssWeekConRate":0.0,"studentName":"贾长江","maxOneWeekAgoMins":4830,"needTeaGender":"不限","clausesnot":"{\"terms\":{\"teaRegion.keyword\": [\"七台河\",\"上海\",\"杭州\"]}},{\"terms\":{\"teacherId\": [12887]}}","lessonType":"test-lesson","ccTransferPer":0.25,"areaRatio":0.4,"lesMatchId":9997889,"stuExtendFreeTimes":"","importance":true,"autoLesNum4Local":4,"duration":2400,"studentId":1500053641,"bu":1,"maxCurMonthRegMins":12030,"lesQRatio":0.35000000000000003,"end":1587020100,"freeDayMinsPer":0.0,"prov":"北京","chuzhong":99,"gaozhong":99,"refuseMsgEvent":"REFUSE_COURSE_MSG_SB","maxFreeDayMins":160,"start":1587017700,"maxRegularLesCount":2588,"oneWeekAgoMinsPer":0.4,"xiaoxue":99,"reCourse":"Y","maxCCTransfer":100,"autoLesNum":1,"maxTimes":76,"pushTimesRatio":0.25,"lesId":1120355232,"lesImpPoint":21.0,"ssWeekNewSignTLesNum":0,"isReintroduce":0,"levels":["\"高中-语文-4\"","\"高中-语文-3\"","\"高中-语文-2\"","\"高中-语文-1\""],"mark":0}  
}

12. 用JAVA API,入参.toString()后查询,在最外层加query

GET lesson/courses-teacher/_search
{
  "query":{
    
  "function_score":{"query":{"bool":{"filter":[{"term":{"gender":{"value":"男","boost":1.0}}},{"term":{"teachingStyle.keyword":{"value":"亲和","boost":1.0}}},{"query_string":{"query":"(\"初一\") OR (\"初中\")","default_field":"preferGrade","fields":[],"use_dis_max":true,"tie_breaker":0.0,"default_operator":"or","auto_generate_phrase_queries":false,"max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"split_on_whitespace":true,"boost":1.0}},{"terms":{"classType":[0,2],"boost":1.0}},{"terms":{"lessonMode":[3,4,5,6],"boost":1.0}},{"terms":{"fullTimeState":[5,40],"boost":1.0}},{"terms":{"bu":[1],"boost":1.0}},{"terms":{"intTimeType":[1,2],"boost":1.0}},{"term":{"rank.keyword":{"value":"普通","boost":1.0}}},{"query_string":{"query":"语文","fields":["subject.keyword^1.0"],"use_dis_max":true,"tie_breaker":0.0,"default_operator":"or","analyzer":"whitespace","auto_generate_phrase_queries":false,"max_determinized_states":10000,"enable_position_increments":true,"fuzziness":"AUTO","fuzzy_prefix_length":0,"fuzzy_max_expansions":50,"phrase_slop":0,"escape":false,"split_on_whitespace":true,"boost":1.0}},{"script":{"script":{"source":"String s=doc['preferGrade.keyword'].value;long stuCount=doc['stuCount'].value; if(s!=null){if(s.indexOf('高')>-1&&stuCount<params.gaozhong){return true;}if(s.indexOf('小')>-1&&stuCount<params.xiaoxue){return true;}if(s.indexOf('初')>-1&&stuCount<params.chuzhong){return true;}}","lang":"painless","params":{"chuzhong":99,"gaozhong":99,"xiaoxue":99}},"boost":1.0}},{"terms":{"teaLevels.keyword":["初中-语文-4","初中-语文-3","初中-语文-2","初中-语文-1"],"boost":1.0}},{"bool":{"should":[{"bool":{"filter":[{"term":{"teaScope":{"value":1,"boost":1.0}}},{"range":{"lesHours2":{"from":null,"to":420,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"lesMinute30Day":{"from":null,"to":7800,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"lesHoursLastWeek":{"from":null,"to":1980,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"lesHoursLastLastWeek":{"from":null,"to":1980,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"curWeekLesHours":{"from":null,"to":1980,"include_lower":true,"include_upper":true,"boost":1.0}}}],"disable_coord":false,"adjust_pure_negative":true,"boost":1.0}},{"bool":{"filter":[{"term":{"teaScope":{"value":2,"boost":1.0}}},{"range":{"lesHours2":{"from":null,"to":420,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"lesMinute30Day":{"from":null,"to":7800,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"lesHoursLastWeek":{"from":null,"to":1980,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"lesHoursLastLastWeek":{"from":null,"to":1980,"include_lower":true,"include_upper":true,"boost":1.0}}},{"range":{"curWeekLesHours":{"from":null,"to":1980,"include_lower":true,"include_upper":true,"boost":1.0}}}],"disable_coord":false,"adjust_pure_negative":true,"boost":1.0}}],"disable_coord":false,"adjust_pure_negative":true,"boost":1.0}},{"script":{"script":{"id":"time-filter-part","lang":"painless","params":{"duration":2700,"start":1587271500,"end":1587274200}},"boost":1.0}},{"range":{"waitingPushCount":{"from":null,"to":2,"include_lower":true,"include_upper":false,"boost":1.0}}},{"range":{"autoLesStatus":{"from":null,"to":1,"include_lower":true,"include_upper":false,"boost":1.0}}}],"must_not":[{"terms":{"teaRegion.keyword":["七台河","杭州"],"boost":1.0}},{"terms":{"teacherId":[1426608859],"boost":1.0}}],"disable_coord":false,"adjust_pure_negative":true,"boost":1.0}},"functions":[{"filter":{"match_all":{"boost":1.0}},"script_score":{"script":{"id":"auto-course-score-part","lang":"painless","params":{"regularLesCountPer":0.75,"curMonthPer":0.6,"duration":2700,"stuCity":"上海","areaPer":0.45,"freeDay":"lesHours2","sellerId":12,"stuCountPer":0.4,"maxCurMonthRegMins":12030,"maxStuCount":125,"lesQRatio":0.35000000000000003,"end":1587274200,"freeDayMinsPer":0.0,"area":"上海","baoheduRatio":0.35000000000000003,"maxFreeDayMins":240,"start":1587271500,"remarkPer":0.5,"oneWeekAgoMinsPer":0.4,"maxRegularLesCount":2588,"maxCCTransfer":100,"maxRemarkPoint":0.0,"maxTimes":77,"pushTimesRatio":0.25,"maxOneWeekAgoMins":4830,"ccTransferPer":0.25,"areaRatio":0.4}}}}],"score_mode":"multiply","max_boost":3.4028235E38,"boost":1.0}
   
}
} 

Kibana案例

GET arranged-lesson/all-lesson-match/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "lesMatchId": "9997163"
        }
      }
    }
  }
}

1.查询所有记录

  • 查询 audience_index:客户档案索引下所有文档数
GET audience_index/_search
{
  "query":{
    "match_all" : {}
  }
}

在这里插入图片描述

2.匹配id字段

查询客户id “audid”: 299433415672006

match

GET audience_index/_search
{
  "query":{
    "match" : {
     "audid": 299433415672006
    }
  }
}

在这里插入图片描述

term


GET audience_index/_search
{
  "query": {
    "term": {
      "audid": {
        "value": "299433415672006"  
      }
    }
  }
}

在这里插入图片描述

GET audience_index/_search
{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "audid": ["299433415672006"]
        }
      }
    }
  }
}

在这里插入图片描述

3.bool[复合查询]

  • must
GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
          "audid": "299433415671936"
        }},
        {
          "match": {
          "base.deleted": "0"
        }}
     ] 
    }
  }
}
  • term
GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
          "audid": "299433415671936"
        }},
        {
          "term": {
          "base.deleted": "0"
        }}
     ] 
    }
  }
}

在这里插入图片描述

  • must +filter
GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [ {
          "term": {
              "audid": 299433415671936
           }
      }] ,
      "filter": {
          "term": {
             "base.deleted": "0"
          }
      }
    }
  }
}

在这里插入图片描述

4.业务查询

一、客户列表-通过客户自选标签筛选客户(ES)

GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "appTag": "10"
          }
        },
        {
          "term": {
            "base.status": 1
          }
        }
      ]
    }
  },
  "size": 100
}

二、客户列表-通过公众号关注日期筛选客户

GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "base.custom.subscribeTime": {
              "gte": "2023-06-12 00:00:00",
              "lte": "2023-06-12 23:59:59"
            }
          }
        },
        {
          "term": {
            "base.status": 1
          }
        }
      ]
    }
  },
  "size": 200
}

三、客户列表-通过客户创建时间筛选客户

GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "createTime": {
              "gte": 1682870400000,
              "lte": 1686499199000
            }
          }
        },
        {
          "term": {
            "base.status": 1
          }
        }
      ]
    }
  },
  "size": 200

四、通过客户分组查看分组内的客户

GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
        "term": {
          "group": {
            "value": "1447-4"
          }
        }
        },
        {
          "term": {
            "base.status": 1
          }
        }
      ]
    }
  },
  "size": 200
}

五、通过APP客户自选标签查看满足条件的客户

GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "appTag": "21"
          }
        },
        {
          "term": {
            "base.status": 1
          }
        },
        {
          "term": {
            "uid.source": {
              "value": "phone"
            }
          }
        }
      ]
    }
  },
  "size": 200
}

六、通过APP客户自选标签,筛选满足发送条件的客户(内容推送和客户投放)

GET audience_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "appTag": "21"
          }
        },
        {
          "term": {
            "base.status": 1
          }
        },
        {
          "term": {
            "uid.source": {
              "value": "weiyinhang"
            }
          }
        }
      ]
    }
  },
  "size": 200
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值