Elasticsearch之基本查询及组合查询操作示例
作者:Jeff的技术栈 发布时间:2023-01-09 16:26:48
标签:Elasticsearch,基本查询,组合查询,查询
Elasticsearch查询
查询分类:
基本查询:使用es内置查询条件进行查询
组合查询:把多个查询组合在一起进行复合查询
过滤:查询的同时,通过filter条件在不影响打分的情况下筛选数据
一 基本查询
#添加映射
PUT lago
{
"mappings": {
"properties":{
"title":{
"stort":true,
"type":"text",
"analyzer":"ik_max_word"
},
"company_name":{
"stort":true,
"type":"keyword",
},
"desc":{
"type":"text"
},
"comments":{
"type":"integer"
},
"add_time":{
"type":"date",
"format":"yyy-MM-dd"
}
}
}
}
#测试数据
POST lago/job
{
"title":"python django 开发工程师",
"company_name":"美团科技有限公司",
"desc":"对django熟悉,掌握mysql和非关系型数据库,网站开发",
"comments:200,
"add_time":"2018-4-1"
}
POST lago/job
{
"title":"python数据分析",
"company_name":"百度科技有限公司",
"desc":"熟悉python基础语法,熟悉数据分析",
"comments:5,
"add_time":"2018-10-1"
}
POST lago/job
{
"title":"python自动化运维",
"company_name":"上海华为",
"desc":"熟悉python基础语法,精通Linux",
"comments:90,
"add_time":"2019-9-18"
}
1.1 match查询
GET lagou/job/_search
{
"query":{
"match":{
"title":"python"
}
}
}
#因为title字段做了分词,python都能搜索出来
#搜索python网站也能搜索出来,把python和网站分成两个词
#搜索爬取也能搜索到,把爬和取分词,去搜索
#只搜取 搜不到
1.2 term查询
GET lagou/_search
{
"query":{
"term":{
"title":"python"
}
}
}
#会拿着要查询的词不做任何处理,直接查询
#用python爬虫,查不到,用match就能查到
{
"query":{
"term":{
"company_name":"美团"
}
}
}
#通过美团,就查询不到
1.3 terms查询
GET lagou/_search
{
"query":{
"terms":{
"title":["工程师","django","运维"]
}
}
}
#三个词,只要有一个,就会查询出来
1.4 控制查询的返回数量(分页)
GET lagou/_search
{
"query":{
"match":{
"title":"python"
}
},
"form":1,
"size":2
}
#从第一条开始,大小为2
1.5 match_all 查询
GET lagou/_search
{
"query":{
"match_all":{}
}
}
#所有数据都返回
1.6 match_phrase查询
GET lagou/_search
{
"query":{
"match_phrase":{
"title":{
"query":"python系统",
"slop":6
}
}
}
}
#短语查询,
#会把查询条件python和系统分词,放到列表中,再去搜索的时候,必须满足python和系统同时存在的才能搜出来
#"slop":6 :python和系统这两个词之间最小的距离
1.7 multi_match
GET lagou/_search
{
"query":{
"multy_match":{
"query":"python",
"fields":["title","desc"]
}
}
}
#可以指定多个字段
#比如查询title和desc这个两个字段中包含python关键词的文档
#"fields":["title^3","desc"]:权重,title中的python是desc中的三倍
1.8 指定返回的字段
GET lagou/_search
{
"query":{
"stored_fields":["title","company_name"]
"match":{
"title":"python"
}
}
}
#只返回title和company_name字段
#"stored_fields":["title","company_name",'dsc'],不会返回dsc,因为我们要求stroed_fields,之前desc字段设为false(默认),不会显示
1.9 sort 结果排序
GET lagou/_search
{
"query":{
"match_all":{}
},
"sort":[
{
"comments":{
"order":"desc"
}
}
]
}
#查询所有文档,按comments按desc降序排序
1.10 range范围查询
GET lagou/_search
{
"query":{
"range":{
"comments":{
"gte":10,
"lte":20,
"boost":2.0
}
}
}
}
#指定comments字段大于等于10,小于等于20
#boost:权重
GET lagou/_search
{
"query":{
"range":{
"add_time":{
"gte":"2019-10-11",
"lte":"now",
}
}
}
}
#对时间进行查询
1.11 wildcard查询
GET lagou/_search
{
"query":{
"wildcard":{
"title":{
"value":"pyth*n",
"boost":2.0
}
}
}
}
#模糊查询,title中,有pyth任意值n得都能查出来
1.12 exists存在
exists:字段包含,存在的
# 包含followers_count字段
GET user_toutiao/_search
{
"query": {
"bool": {
"must": [
{"exists": {
"field": "followers_count"
}}
]
}
}
}
# 不包含followers_count字段
GET user_toutiao/_count
{
"query": {
"bool": {
"must_not": [
{"exists": {
"field": "followers_count"
}}
]
}
}
}
# 不包含followers_count且updata_timestamp>1614221216
GET user_toutiao/_count
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "followers_count"
}
}
],
"must": [
{"range": {
"updata_timestamp": {
"gt": 1614221216
}
}}
]
}
}
}
二 组合查询
2.1 bool查询
#bool查询包括must should must_not filter
'''
bool:{
"filter":[], 字段过滤
"must":[], 所有查询条件都满足
"should":[], 满足一个或多个
"must_not":{} 都不满足于must相反
}
'''
# 建立测试数据
POST lago/testjob/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"Python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}
2.2 简单过滤查询
#select * from testjob where salary=20
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"term":{
"salary":20
}
}
}
}
}
2.3 查询多个值
#查询薪资是10k或20k的
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"terms":{
"salary":[10,20]
}
}
}
}
}
#select * from testjob where title="python"
GET lagou/testjob/_search
{
"query":{
"bool":{
"must":{
"match_all":{}
},
"filter":{
"term":{
"title":"Python"
}
}
}
}
}
#title 是text字段,会做大小写转换,term不会预处理,拿着大写Python去查查不到
#可以改成小写,或者用match来查询
'''
"filter":{
"match":{
"title":"Python"
}
}
'''
#查看分析器解析结果
GET _analyze
{
"analyzer":"ik_max_word",
"text":"python网络开发工程师"
}
2.4 bool过滤查询,可以做组合过滤查询
#select * from testjob where (salary=20 or title=Python) and (salary!=30)
#查询薪资等于20k或者工作为python的工作,排除价格为30k的
{
"query":{
"bool":{
"should":[
{"term":{"salary":20}},
{"term":{"title":"python"}}
],
"must_not":{
"term":{"salary":30}
}
}
}
}
#select * from testjob where title=python or (title=django and salary=30)
{
"query":{
"bool":{
"should":[
{"term":{"title":"python"}},
{
"bool":{
"must":[
{"term":{"title":"django"}},
{"term":{"salary":30}}
]
}
}
]
}
}
}
来源:https://www.cnblogs.com/guyouyin123/p/13308669.html
0
投稿
猜你喜欢
- 本文实例讲述了Python将名称映射到序列元素中的方法。分享给大家供大家参考,具体如下:问题:希望通过名称来访问元素,减少结构中对位置的依赖
- 什么是事件呢?按下键盘某个按键,鼠标移动,包括点击关闭按钮都可以算是事件操作。Pygame事件pygame.event.EventType&
- 最近学习python并发,于是对多进程、多线程、异步和协程做了个总结。一、多线程多线程就是允许一个进程内存在多个控制权,以便让多个函数同时处
- 利用python pyheatmap包绘制热力图,供大家参考,具体内容如下import matplotlib.pyplot as pltfr
- 一、概念说明柱状图(bar chart),从相同的横坐标出发,以不同的数值大小来设定柱子的高度,进而表示无序或有序的定性数据间某个定量指标的
- 前言本专栏将非常细致的讲解相关与计算机视觉OpenCV的相关知识即操作,非常的简单易懂。本文主要讲解相关与计算机视觉的相关入门内容,关于图像
- 对于小数据量,xml文件在检索更新上于ACCESS有很多优势。我曾经测试过不用数据库,把网站的会员信息,商品数据信息,交易信息,网站定制信息
- IntelliJ IDEA 2020.1 插件中心一直打不开,鉴于有部分同学反馈设置http proxy不能解决,所以可按以下顺序检查一、设
- 本文介绍了asp编程中使用数组的各种方法,并给出了详细的asp实例代码方便大家理解。asp中数组的定义Dim MyArray My
- 一、概述推荐使用参考网站: json在python中,json模块可以实现json数据的序列化和反序列化序列化:将可存放在内存中的pytho
- 对于三目运算符(ternary operator),python可以用conditional expressions来替代如对于x<5
- 这10个asp处理网页编码转换的函数,不知何时收藏在我的电脑中,今天刚好看到了,拿出来与大家分享,这里各种常见的网页编码问题已经
- 效果图from wxpyimport *import requestsfrom datetimeimport datetimeimport
- python版本为python3.51.要求1)输入用户名密码2)认证成功后显示欢迎信息3)输错三次后锁定2.需求分析1)用户信息存储在文件
- 原理使用python win32 库 调用word底层vba,将word转成pdf安装pywin32pip install pywin32p
- 1. python函数1.1 函数的作用函数是组织好的,可重复使用的,用来实现单一或相关联功能的代码段函数能提高应用的模块性和代码的重复利用
- 前言接着上一篇:AI识别照片是谁,人脸识别face_recognition开源项目安装使用根据项目提供的demo代码,调整了一下功能,自己写
- 这里只列举了部分方法,其他方法或python库暂时还没使用到1.不用库,直接打印:代码样例:import time#demo1def pro
- Python通过yield提供了对协程的基本支持,但是不完全。而第三方的gevent为Python提供了比较完善的协程支持。gevent是第
- 用python做一个简单的随机点名程序(不重复点名)这是我来到csdn的第一篇文章,内容如果有瑕疵的地方或者代码可以进一步改善,请大家对我指