Elasticsearch基本查询

2023-11-17

目录

基本语法

查询所有(match_all)

匹配查询(match)

多字段查询(multi_match)

精确匹配(term)

多词条精确匹配(terms)

结果过滤

直接指定字段

指定includes和excludes

布尔组合查询(bool)

must、must_not

should

filter

范围查询(range)

排序

单字段排序

多字段排序

分页

关键知识点总结

 


基本语法

POST /索引库名/_search
{
    "query":{
        "查询类型":{
            "查询条件":"查询条件值"
        }
    }
}
  • query:代表一个查询对象,里面可以有不同的查询属性;
  • 查询类型:例如:match_all, matchterm , range 等等;
  • 查询条件:查询条件会根据类型的不同,写法也有差异。

查询所有(match_all)

POST shop/_search
{
  "query": {
    "match_all": {}
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "京欣西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 10.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "苹果",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 3.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "麒麟西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 20.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 15.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "香蕉",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 2.3
        }
      }
    ]
  }
}
  • took:查询花费时间,单位是毫秒

  • time_out:是否超时

  • _shards:分片信息

  • hits:搜索结果总览对象

    • total:搜索到的总条数

    • max_score:所有结果中文档得分的最高分

    • hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息

      • _index:索引库
      • _type:文档类型
      • _id:文档id
      • _score:文档得分
      • _source:文档的源数据

匹配查询(match)

POST /shop/_search
{
    "query":{
        "match":{
            "title":"麒麟西瓜"
        }
    }
}

match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系,例如通过查询条件为麒麟西瓜,分词后与西瓜相关的记录都会查到。

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.219939,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "4",
        "_score" : 1.219939,
        "_source" : {
          "title" : "麒麟西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 20.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "京欣西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 10.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 15.6
        }
      }
    ]
  }
}

如果想使用match进行更精确的查找,可以指定operator属性为and:

POST /shop/_search
{
  "query": {
    "match": {
      "title": {
        "query": "麒麟西瓜",
        "operator": "and"
      }
    }
  }
}

结果:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.219939,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "4",
        "_score" : 1.219939,
        "_source" : {
          "title" : "麒麟西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 20.6
        }
      }
    ]
  }
}

多字段查询(multi_match)

POST /shop/_search
{
  "query": {
    "multi_match": {
      "query": "苹果",
      "fields": ["title","subtitle"]
    }
  }
}

结果:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.2330425,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.2330425,
        "_source" : {
          "title" : "苹果",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 3.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "v4e-03QB1qDLo16zF5gJ",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "红富士",
          "images" : "http://image.xxx.com/12479122.jpg",
          "price" : 6,
          "subtitle" : "苹果"
        }
      }
    ]
  }
}

精确匹配(term)

term 查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串。

POST /shop/_search
{
    "query":{
        "term":{
            "price":3.6
        }
    }
}

结果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "苹果",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 3.6
        }
      }
    ]
  }
}

多词条精确匹配(terms)

terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql的in。

POST /shop/_search
{
    "query":{
        "terms":{
            "price":[3.6,20.6]
        }
    }
}

结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "苹果",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 3.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "麒麟西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 20.6
        }
      }
    ]
  }
}

结果过滤

默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。如果我们只想获取其中的部分字段,我们可以添加_source的过滤。

直接指定字段

POST /shop/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "price": 10.60
    }
  }
}

结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "price" : 10.6,
          "title" : "京欣西瓜"
        }
      }
    ]
  }
}

指定includes和excludes

  • includes:来指定想要显示的字段;
  • excludes:来指定不想要显示的字段。
POST /shop/_search
{
  "_source": {
    "includes":["title","price"]
  },
  "query": {
    "term": {
      "price": 3.6
    }
  }
}
POST /shop/_search
{
  "_source": {
    "excludes":["images","price"]
  },
  "query": {
    "term": {
      "price": 3.6
    }
  }
}

布尔组合查询(bool)

bool查询包含四种操作符,分别是must,should,must_not,query。它们均是一种数组,数组里面是对应的判断条件。

  • must: 必须匹配,与and等价,贡献算分;
  • must_not:必须不匹配,与not等价,常过滤子句用,但不贡献算分;
  • should: 选择性匹配,至少满足一条,与 OR 等价,贡献算分;
  • filter: 过滤子句,必须匹配,但不贡献算分。

must、must_not

POST /shop/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "title": "西瓜"
        }
      },
      "must_not": {
        "match": {
          "price": "20.6"
        }
      }
    }
  }
}

结果:

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "京欣西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 10.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 15.6
        }
      }
    ]
  }
}

should

should类似于or,只要有一个条件满足即可:


POST /shop/_search
{
  "query": {
    "bool": {
      "should": [{
        "match": {
          "title": "苹果"
        }
      },{
        "match": {
          "price": "20.6"
        }
      }]
    }
  }
}

结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.2330425,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.2330425,
        "_source" : {
          "title" : "苹果",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 3.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "麒麟西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 20.6
        }
      }
    ]
  }
}

filter

会查询对结果进行缓存,不会计算相关度,避免计算分值,执行速度非常快。

POST /shop/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "price": "20.6"
        }
      }
    }
  }
}

结果:_score属性为0

{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "title" : "麒麟西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 20.6
        }
      }
    ]
  }
}

 


范围查询(range)

range 查询找出那些落在指定区间内的数字或者时间。

range查询允许以下字符:

操作符 说明
gt 大于
gte 大于等于
lt 小于
lte 小于等于
POST /shop/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  10,
                "lt":   20
            }
        }
    }
}

 结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "京欣西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 10.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 15.6
        }
      }
    ]
  }
}

排序

单字段排序

POST /shop/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 5,
        "lt": 20
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

结果:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "title" : "西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 15.6
        },
        "sort" : [
          15.6
        ]
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "title" : "京欣西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 10.6
        },
        "sort" : [
          10.6
        ]
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "v4e-03QB1qDLo16zF5gJ",
        "_score" : null,
        "_source" : {
          "title" : "红富士",
          "images" : "http://image.xxx.com/12479122.jpg",
          "price" : 6,
          "subtitle" : "苹果"
        },
        "sort" : [
          6.0
        ]
      }
    ]
  }
}

多字段排序

POST /shop/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 5,
        "lt": 20
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

分页

POST /shop/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 0
}
  • size:每页显示多少条;
  • from:当前页起始索引, int start = (pageNum - 1) * size。

结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "京欣西瓜",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 10.6
        }
      },
      {
        "_index" : "shop",
        "_type" : "goods",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "苹果",
          "images" : "http://image.xxx.com/435252.jpg",
          "price" : 3.6
        }
      }
    ]
  }
}

关键知识点总结

 

  • match 查询的时候,ES会根据你给定的字段提供合适的分析器,相当于模糊匹配,只包含其中一部分关键词就行;
  • term 查询不会有分析器分析的过程,一般被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串;
  • must 为必须匹配,与and等价,贡献算分,查询时分数高的靠前;
  • must_not 为必须不匹配,与not等价,常过滤子句用,但不贡献算分;
  • should 为选择性匹配,至少满足一条,与 OR 等价,贡献算分;
  • filter 为必须匹配,查询过程忽略算分,直接过滤匹配的数据,结果会被缓存,效率较高。

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Elasticsearch基本查询 的相关文章

随机推荐

  • 按钮提交在url后添加字段_在输入字段上定向单击“清除”按钮(X)

    按钮提交在url后添加字段 jQuery makes it easy to get your project up and running Though it s fallen out of favor in recent years it
  • :nth-of-child和:nth-of-type

    nth of child 用法 nth of child n n可以为数字或者表达式 例如2n 1 指的是奇数项 介绍 第n个孩子 例如 div nth of child 1 等同于 div first of child div的第一个孩子
  • Hadoop的伪分布式运行模式

    Hadoop运行模式包括 本地模式 伪分布式模式 以及完全分布式模式 1 本地模式 安装简单 在一台机器上运行服务 几乎不用做任何配置 但仅限于调试用途 没有分布式文件系统 直接读写本地操作系统的文件系统 2 伪分布式模式 在单节点上同时启
  • typescript—第七天,命名空间

    没有命名空间时的问题 先写一下这样代码 用类的形式在index html中实现header content和Footer部分 类似我们常说的模板 在page ts文件里 写出下面的代码 class Header constructor co
  • 简述 RSA 加密算法实现过程

    银行密码系统安全吗 质数 素数 到底有啥用 李永乐老师11分钟讲RSA加密算法 2018最新 哔哩哔哩 bilibili 质数 和 欧拉函数 公钥 范围 且
  • Python 爬虫学习笔记(十(4))scrapy链接提取器CrawlSpider

    CrawlSpider也可以这样用 对每一个提取出的链接都调用某些操作 创建项目 scrapy startproject 项目的名字 跳转到scrapy文件夹的目录下 创建爬虫文件 语句和之前不同 scrapy genspider t cr
  • Java读取resource目录下图片插入excel导出

    1 将图片放在resoure目录下 2 读取图片并插入excel private void monthContractRow1 Sheet sheet CellStyle style1 String language Workbook wo
  • ElementUI常用组件之布局组件

    安装elementui npm i element ui S 配置ElementUI 1 main js中 引入elementUI 共分三步走 a import element ui b import css文件 c use Element
  • jQuery中$ $()与$(document) this与$(this)三个的区别

    是jquery专用的特殊符号 bai可以说所有dujquery函数的调用都是从 开始的 在jquery里面表示一zhi个选择器 括号dao里面填写一定的表达式就可以选中你想要的元素 jquery的选择器除了能很好地遵循css的表达式外 还额
  • Ubuntu 无法进行SSH连接,开启22端口

    我们在VM中安装好Ubuntu 虚拟机后 经常需要使用Xshell等工具进行远程连接 但是会出现无法连接的问题 原因是Ubuntu中默认关闭了SSH 服务 1 查看Ubuntu虚拟机IP地址 命令 ifconfig 2 利用XShell等工
  • 点击按钮改变颜色 模型动画暂停继续功能

    using System Collections using System Collections Generic using UnityEngine using UnityEngine UI using DG Tweening publi
  • C语言关键字必备练习题

    1 作业标题 642 关于C语言关键字说法正确的是 作业内容 A 关键字可以自己创建 B 关键字不能自己创建 C 关键字可以做变量名 D typedef不是关键字 答案解析 C语言关键字 C语言定义的 具有特定含义 专门用于特殊用途的C语言
  • 【c++】内存四区(代码区、全局区、栈区、堆区)

    文章目录 内存分区模型 代码区 全局区 栈区 堆区 内存分区模型 c 程序在执行时 将内存大方向划分为4个区域 代码区 存放函数体的二进制代码 有操作系统进行管理 全局区 存放全局变量和静态变量以及常量 栈区 由编译器自动分配释放 存放函数
  • C++ 产生随机数

    推荐一个博客 以下转载该博客部分内容 主要代码 include
  • JSP、JSTL标签

  • 02-JavaWeb之Servlet及相关知识点

    一 Servlet 简介 Java Servlet 是运行在 Web 服务器 tomcat 或应用服务器上的程序 它可以接收来自 Web 浏览器或其他 HTTP 客户端的请求 并进行结果的响应 使用 Servlet 可以收集来自网页表单的用
  • 1064 Complete Binary Search Tree (30 分)

    题目 题目链接 题解 数据结构 二叉排序树的中序遍历是单调递增的 完全二叉树具有很好的顺序存储的性质 利用中序遍历和递归 先构造左子树 再对根节点进行赋值 再构造右子树 我是fw 代码 include
  • 【异常】 DevOps工具链SonarQube提示严重的错误,内容为Save and re-use this “Random“,需要使用SecureRandom替换Random

    一 报错内容 二 报错说明 2 1 Random和SecureRandom的区别 Random生成伪随机数 这意味着这些数字并不是真正的随机数 而是由算法生成的 如果攻击者知道生成这些数字的算法 那么Random生成的数字就可以被预测 因此
  • scrapy中使用正确的xpath出现空列表问题

    今天在使用scrapy框架爬取网页时 使用正确的xpath来爬取时 爬取的缺失空列表 代码如下 coding utf 8 import scrapy class HaodfSpider scrapy Spider name haodf st
  • Elasticsearch基本查询

    目录 基本语法 查询所有 match all 匹配查询 match 多字段查询 multi match 精确匹配 term 多词条精确匹配 terms 结果过滤 直接指定字段 指定includes和excludes 布尔组合查询 bool