Elasticsearch 基本查询和组合查询


Posted in Python onApril 19, 2022

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}}
            ]
          }
        }
      ]
    }
  }
}

以上就是Elasticsearch之基本查询及组合查询操作示例的详细内容!

Python 相关文章推荐
Python实现去除代码前行号的方法
Mar 10 Python
浅析Python中的多条件排序实现
Jun 07 Python
深度定制Python的Flask框架开发环境的一些技巧总结
Jul 12 Python
python、java等哪一门编程语言适合人工智能?
Nov 13 Python
Python爬虫通过替换http request header来欺骗浏览器实现登录功能
Jan 07 Python
python之DataFrame实现excel合并单元格
Feb 22 Python
python实现类之间的方法互相调用
Apr 29 Python
Flask使用Pyecharts在单个页面展示多个图表的方法
Aug 05 Python
python数据库编程 ODBC方式实现通讯录
Mar 27 Python
python使用nibabel和sitk读取保存nii.gz文件实例
Jul 01 Python
Python创建简单的神经网络实例讲解
Jan 04 Python
Python批量将csv文件转化成xml文件的实例
May 10 Python
Elasticsearch 批量操作
Apr 19 #Python
Elasticsearch 数据类型及管理
Apr 19 #Python
Elasticsearch 索引操作和增删改查
Apr 19 #Python
python中redis包操作数据库的教程
Apr 19 #Python
python中pymysql包操作数据库方法
Apr 19 #Python
Python中Schedule模块使用详解 周期任务神器
Apr 19 #Python
python中urllib包的网络请求教程
Apr 19 #Python
You might like
获取php页面执行时间,数据库读写次数,函数调用次数等(THINKphp)
2013/06/03 PHP
php上传文件并显示上传进度的方法
2015/03/24 PHP
thinkPHP5.0框架URL访问方法详解
2017/03/18 PHP
PHP PDOStatement::columnCount讲解
2019/01/30 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
硬盘浏览程序,保存成网页格式便可使用
2006/12/03 Javascript
use jscript List Installed Software
2007/06/11 Javascript
js window.onload 加载多个函数的方法
2009/11/02 Javascript
网站导致浏览器崩溃的原因总结(多款浏览器) 推荐
2010/04/15 Javascript
jquery统计复选框选中示例
2013/11/05 Javascript
Jquery.Form 异步提交表单的简单实例
2014/03/03 Javascript
删除Javascript Object中间的key
2014/11/18 Javascript
JavaScript中的逻辑判断符&&、||与!介绍
2014/12/31 Javascript
webpack中引用jquery的简单实现
2016/06/08 Javascript
Angular ng-class详解及实例代码
2016/09/19 Javascript
JS实现重新加载当前页面
2016/11/29 Javascript
JavaScript实现反转字符串的方法详解
2017/04/27 Javascript
js+html5实现侧滑页面效果
2017/07/15 Javascript
Auto.js自动收取自己和好友蚂蚁森林能量脚本
2018/06/28 Javascript
JS实现图片切换效果
2018/11/17 Javascript
Vuex持久化插件(vuex-persistedstate)解决刷新数据消失的问题
2019/04/16 Javascript
[01:32:22]DOTA2-DPC中国联赛 正赛 Ehome vs VG BO3 第一场 2月5日
2021/03/11 DOTA
python中的计时器timeit的使用方法
2017/10/20 Python
python实现的生成word文档功能示例
2019/08/23 Python
Python OpenCV读取中文路径图像的方法
2020/07/02 Python
CSS3中设置3D变形的transform-style属性详解
2016/05/23 HTML / CSS
html5 http的轮询和Websocket原理
2018/10/19 HTML / CSS
锐步香港官方网上商店:Reebok香港
2020/11/05 全球购物
如何定义一个可复用的服务
2014/09/30 面试题
前台文员个人求职信范文
2014/01/05 职场文书
计生工作先进事迹
2014/08/15 职场文书
2015年除四害工作总结
2015/07/23 职场文书
2016年党员读书月活动总结
2016/04/06 职场文书
python字典的元素访问实例详解
2021/07/21 Python
CI Games宣布《堕落之王2》使用虚幻引擎5制作 预计将于2023年正式发售
2022/04/11 其他游戏
Win11 Beta 22621.601 和 22622.601今日发布 KB5017384修复内容汇总
2022/09/23 数码科技