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中的字符串类型基本知识学习教程
Feb 04 Python
详解用Python处理HTML转义字符的5种方式
Dec 27 Python
Python基于property实现类的特性操作示例
Jun 15 Python
Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项
Nov 30 Python
python实现抠图给证件照换背景源码
Aug 20 Python
多版本python的pip 升级后, pip2 pip3 与python版本失配解决方法
Sep 11 Python
PyQt5高级界面控件之QTableWidget的具体使用方法
Feb 23 Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
Feb 25 Python
Python实现初始化不同的变量类型为空值
Jun 02 Python
Windows环境下Python3.6.8 importError: DLLload failed:找不到指定的模块
Nov 01 Python
编译 pycaffe时报错:fatal error: numpy/arrayobject.h没有那个文件或目录
Nov 29 Python
关于python爬虫应用urllib库作用分析
Sep 04 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
thinkphp区间查询、统计查询与SQL直接查询实例分析
2014/11/24 PHP
php使用fputcsv()函数csv文件读写数据的方法
2015/01/06 PHP
JavaScript OOP面向对象介绍
2010/12/02 Javascript
js获取html参数及向swf传递参数应用介绍
2013/02/18 Javascript
JS字符串处理实例代码
2013/08/05 Javascript
Jquery:ajax实现翻页无刷新功能代码
2013/08/05 Javascript
JQuery使用index方法获取Jquery对象数组下标的方法
2015/05/18 Javascript
js 右侧浮动层效果实现代码(跟随滚动)
2015/11/22 Javascript
JavaScript学习笔记之取数组中最大值和最小值
2016/03/23 Javascript
使用postMesssage()实现iframe跨域页面间的信息传递
2016/03/29 Javascript
jquery的ajax提交form表单的两种方法小结(推荐)
2016/05/25 Javascript
Bootstrap学习笔记之css样式设计(1)
2016/06/07 Javascript
js获取元素的标签名实现方法
2016/10/08 Javascript
走进AngularJs之过滤器(filter)详解
2017/02/17 Javascript
js仿淘宝评价评分功能
2017/02/28 Javascript
nodejs 最新版安装npm 的使用详解
2018/01/18 NodeJs
Puppet的一些技巧
2018/09/17 Javascript
实现一个 Vue 吸顶锚点组件方法
2019/07/10 Javascript
详解利用eventemitter2实现Vue组件通信
2019/11/04 Javascript
vue表单数据交互提交演示教程
2019/11/13 Javascript
nodejs脚本centos开机启动实操方法
2020/03/04 NodeJs
vue基于better-scroll仿京东分类列表
2020/06/30 Javascript
在MAC上搭建python数据分析开发环境
2016/01/26 Python
python回调函数中使用多线程的方法
2017/12/25 Python
pyQt4实现俄罗斯方块游戏
2018/06/26 Python
实例详解Matlab 与 Python 的区别
2019/04/26 Python
Python中的单下划线和双下划线使用场景详解
2019/09/09 Python
Python re正则表达式元字符分组()用法分享
2020/02/10 Python
python Paramiko使用示例
2020/09/21 Python
EVE LOM英国官网:全世界最好的洁面膏
2017/10/30 全球购物
万宝龙英国官网:Montblanc手表、书写工具、皮革和珠宝
2018/10/16 全球购物
小学校园活动策划
2014/01/30 职场文书
幼儿园英语教学反思
2014/01/30 职场文书
《乞巧》教学反思
2014/02/27 职场文书
电教室标语
2014/06/20 职场文书
2014年消防工作总结
2014/11/21 职场文书