5.elasticsearch中查询条件
一、URI查询
(这里在kibana中可以自己用用,实际上,下文中的DSL查询语句作用会更大些)
通过在URI后面添加参数,实现一些简单条件的查询
- q指定查询语句,使用Query String Syntax
- df指定默认字段,不指定查询所有字段
- sort排序/from和size用于分页
- profile可以用来查看是如何被执行的
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s
{
"profile":true
}
指定字段、泛查询
GET /movies/_search?q=2012&df=title
{
"profile":"true"
}
#泛查询,正对_all,所有字段
GET /movies/_search?q=2012
{
"profile":"true"
}
分组和phrase
分组查询需要使用(),将查询条件括起来,里面字段默认是or的关系,查出来的文档有其一个就会被查出来
phrase查询需要使用"",里面的关系是and,表示查出来的文档都拥有里面的词,并且按照词组的顺序排列
#分组查询
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile":"true"
}
#使用引号,Phrase查询
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile":"true"
}
Bool条件
在term查询中添加指定bool条件
使用大写AND/OR/NOT
&&/||/!
#分组,Bool查询默认是or的关系
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile":"true"
}
#布尔操作符
GET /movies/_search?q=title:(Beautiful AND Mind)
{
"profile":"true"
}
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile":"true"
}
must条件
在term查询中可添加must和must_not条件
- +表示must
- -表述must_not
GET /movies/_search?q=title:(+Beautiful -Mind)
范围查询
- []闭区间
- {}开区间
#范围查询 ,区间写法
GET /movies/_search?q=title:beautiful AND year:[2002 TO 2018]
通配符查询
- ?代表一个字符,*代表0或多个
GET /movies/_search?q=title:(beautifu? AND y*)
正则表达式
GET /movies/_search?q=title:[bt]oy
模糊匹配与近似查询
GET /movies/_search?q=title:befutifl~1
GET /movies/_search?q=title:"lord rings"~2
二、RequestBody&DSL
DSL:Query Domain Search Language
ignore_unavailable
ignore_unavailable=true,可以忽略尝试访问不存在的索引“404_idx”导致的报错
GET /movies,404_idx/_search?ignore_unavailable=true
{
"profile": true,
"query": {
"match_all": {}
}
}
profile
返回结果中添加此次查询分词等细节
GET /movies,404_idx/_search?ignore_unavailable=true
{
"profile": true,
"query": {
"match_all": {}
}
}
explain
返回每个文档结果中添加此次查询算分的细节
GET /movies,404_idx/_search?ignore_unavailable=true
{
"explain": true,
"query": {
"match_all": {}
}
}
query
查询条件
GET /movies,404_idx/_search?ignore_unavailable=true
{
"profile": true,
"query": {
"match_all": {}
}
}
from、size
分页,from:偏移量,size:每页大小
GET /kibana_sample_data_ecommerce/_search
{
"from":10,
"size":20,
"query":{
"match_all": {}
}
}
sort
#对日期排序
GET kibana_sample_data_ecommerce/_search
{
"sort":[{"order_date":"desc"}],
"query":{
"match_all": {}
}
}
_source
用来过滤返回结果中需要显示的字段
GET kibana_sample_data_ecommerce/_search
{
"_source":["order_date"],
"query":{
"match_all": {}
}
}
script_fields
脚本字段,用来生产一个新的返回字段,生成规则写在script中
#脚本字段
GET kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "doc['order_date'].value+'hello'"
}
}
},
"query": {
"match_all": {}
}
}
match
匹配查询,如果此字段设置了分词,会分词查询
GET movies/_search
{
"query": {
"match": {
"title": "last christmas"
}
}
}
#operator可以修饰查询的条件
GET movies/_search
{
"query": {
"match": {
"title": {
"query": "last christmas",
"operator": "and"
}
}
}
}
match_phrase
短语查询,不会分词查询
GET movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love"
}
}
}
}
#slop参数可以设置查询短语的顺序,1代表单词移动一位还能和所查的短语匹配就命中
#one two love会被查出来
GET movies/_search
{
"query": {
"match_phrase": {
"title":{
"query": "one love",
"slop": 1
}
}
}
}
term
查询的字段不论是text还是keyword,不会将输入的文本进行分词处理
但是目标字段可能会被分词,所以会导致查不到想要的结果,此时建议用keyword类型的字段查询
#title的内容是"last christmas"
#如果title是text,那么会查不到
#如果title是keyword,会查到
GET movies/_search
{
"query": {
"trem": {
"title": "last christmas"
}
}
}
terms
查询字段内包含多个关键词的文档
GET movies/_search
{
"query": {
"trems": {
"title": ["last", "christmas"]
}
}
}
multi_match
一个字符串,在多个字段中查询
GET blogs/_search
{
"query": {
"multi_match": {
"type": "best_fields",
"query": "Quick pets",
"fields": ["title","body"],
"tie_breaker": 0.2,
"minimum_should_match": "20%"
}
}
}
GET books/_search
{
"multi_match": {
"query": "Quick brown fox",
"fields": "*_title"
}
}
GET books/_search
{
"multi_match": {
"query": "Quick brown fox",
"fields": [ "*_title", "chapter_title^2" ]
}
}
query String
实现多字符串,多字段查询
GET users/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "Ruan AND Yiming"
}
}
}
GET users/_search
{
"query": {
"query_string": {
"fields":["name","about"],
"query": "(Ruan AND Yiming) OR (Java AND Elasticsearch)"
}
}
}
Simple query String
- 类似Query String,但是会忽略错误的语法,同时只支持部分查询语法
- 不支持 AND OR NOT 会当作字符串处理
- Term之间默认的关系是OR,可以指定default_operate
- 支持部分逻辑
- +替代AND
- |替代OR
- -替代NOT
GET /movies/_search
{
"profile":true,
"query":{
"simple_query_string":{
"query":"Beautiful +mind",
"fields":["title"],
"default_operator": "AND"
}
}
}
GET /movies/_search
{
"profile":true,
"query":{
"simple_query_string":{
"query":"Beautiful +mind",
"fields":["title"]
}
}
}
bool 查询
它是一种嵌套结构,里面可以嵌套上述各种查询逻辑,它本身包含三种逻辑结构。must、should、must_not
GET index /_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "Search"
}
},
{
"match": {
"content": "Elasticsearch"
}
}
],
"should": [
{
"term": {
"status": "published"
}
},
{
"range": {
"publish_date": {
"gte": "2015-01-01"
}
}
}
],
"must_not": [
{
"exists": {
"field":"value"
}
}
]
}
}
}
filter
给查询添加过滤条件,过滤结构化数据
- 好处是可以把查询结果添加到缓存中
- 还有一个作用是过滤的结果不会算分
GET index /_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Search" }},
{ "match": { "content": "Elasticsearch" }}
],
"filter": [
{ "term": { "status": "published" }},
{ "range": { "publish_date": { "gte": "2015-01-01" }}}
]
}
}
}
5.elasticsearch中查询条件的更多相关文章
- eas之EntityViewInfo对象mainQuery中查询条件
EntityViewInfo对象mainQuery中查询条件: 添加查询字段:(Sql语句中的selectz子句内容) SelecttorItemCollection sic=new Sele ...
- MongoDB官方C#驱动中查询条件Query用法
Query.All("name", "a", "b");//通过多个元素来匹配数组 Query.And(Query.EQ("nam ...
- jsp中查询条件的回显
后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...
- 在Elasticsearch中查询Term Vectors词条向量信息
这篇文章有点深度,可能需要一些Lucene或者全文检索的背景.由于我也很久没有看过Lucene了,有些地方理解的不对还请多多指正. 更多内容还请参考整理的ELK教程 关于Term Vectors 额, ...
- sql通配符+sql中查询条件包含下划线等通配符的写法
一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...
- sql中 查询条件出现单引号和特殊字符处理
1.两个单引号转为一个单引号 example: select * from tb where name=' '' ' 2.如果出现 "_","%" 需要用 ...
- cookie应用——UI中查询条件的保存
var cookieOperate = { cookieNames: { companyCNName:"_companyCNName", companyENName:"_ ...
- oracle中查询条件包含null时
不能使用=null或者!=null 应该使用is null和is not null
- Logstash:把MySQL数据导入到Elasticsearch中
Logstash:把MySQL数据导入到Elasticsearch中 前提条件 需要安装好Elasticsearch及Kibana. MySQL安装 根据不同的操作系统我们分别对MySQL进行安装.我 ...
- ElasticSearch中的简单查询
前言 最近修改项目,又看了下ElasticSearch中的搜索,所以简单整理一下其中的查询语句等.都是比较基础的.PS,好久没写博客了..大概就是因为懒吧.闲言少叙书归正传. 查询示例 http:// ...
随机推荐
- Python基于Flask的高校舆情分析,舆情监控可视化系统
一.前言在当今社会,舆情监控越来越被重视.随着互联网技术的发展,我们从传统媒体渠道.官方报告.调查问卷等方式搜集到的舆情信息,逐渐被网络上的内容所替代.因为网络上的内容传播速度快.及时性强.覆盖范围广 ...
- 解决Dependency 'fastdfs-client-java’not found
如何能把 fastdfs的jar包安装到本地的仓库中(因为中央仓库没有FASTDFS的jar包地址) 1.首先去github上下载下来fastdfs的压缩包 下载链接 然后直接解压出来 2.使用cmd ...
- sql分组后排序计算
用法:RANK() OVER(PARTITION BY 分组字段 ORDER BY 排序字段 ) 例子:要得到n4列 ---创建测试数据create table tb(n1 varchar2(40) ...
- linux- 挂载本地iso,配置本地yum
------------------------------------ 关于centos8安装vm-tools: workstation部署centos8之后,不需要单独安装vm-tools,系统已 ...
- 基于落点打分的井字棋智能下棋算法(C语言实现)
本文设计了一种基于落地打分的井字棋下棋算法,能够实现电脑不败,所以如果玩家会玩的话,一般是平局. 算法核心 电脑根据对落子位置的打分,选择分数最高的位置,若不同落点分数相同则随机选择位置(随机选择就不 ...
- unity UGUI 正交相机实现图片的透视旋转效果
UI透视效果常见的就是绕x轴或y轴旋转,来达到近大远小的效果.正经要做透视UI的话肯定直接用透视相机,如果透视效果用的极少(就一张图)不改动相机类型才按这种思路进行. 最简单直接的想法就是把矩形的图片 ...
- JavaScript:对象的三个属性
每一个对象都有与之相关的原型(prototype).类(class)和可扩展性(extension attribute). 原型 prototype 对象的原型属性是用来继承属性的.通过对象直接量创建 ...
- Util应用框架基础(五) - 异常处理
本节介绍Util应用框架如何处理系统错误. 概述 系统在运行过程中可能发生错误. 系统错误可以简单分为两类: 系统异常 系统本身出现的错误. 业务异常 不满足业务规则出现的错误. 如何处理系统异常 如 ...
- “技能兴鲁”职业技能大赛-网络安全赛项-学生组初赛 Crypto WP
babyRSA 查看代码 from gmpy2 import * from Crypto.Util.number import * flag = 'flag{I\'m not gonna tell y ...
- L2-031 深入虎穴
并没有说根是谁 #include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const ...