博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch学习---Query DSL基本操作
阅读量:2492 次
发布时间:2019-05-11

本文共 11321 字,大约阅读时间需要 37 分钟。

版本说明

本文基于Elasticsearch6.4.0版本

在这里插入图片描述

关于DSL

DSL是Elasticsearch提供的一种基于JSON格式的查询方式。

演示数据

mapping映射

{
"emp": {
"mappings": {
"base_info": {
"properties": {
"age": {
"type": "integer" }, "dept": {
"type": "long" }, "detail": {
"type": "text" }, "name": {
"type": "keyword" }, "salary": {
"type": "double" }, "sex": {
"type": "integer" } } } } }}
GET /emp/base_info/_search

已有数据

{
"took": 3, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 6, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "5", "_score": 1, "_source": {
"name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": "普通人" } }, {
"_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": {
"name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, {
"_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, {
"_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } }, {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, {
"_index": "emp", "_type": "base_info", "_id": "11", "_score": 1, "_source": {
"name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}

查询演示

1、返回所有文档信息(match_all)

GET /_search{
"query":{
"match_all": {
} } }

2、返回索引中的文档信息

GET /emp/base_info/_search{
"query": {
"match_all": {
} }}

3、条件查询(match)

GET /emp/base_info/_search{
"query": {
"match": {
"name": "赵六" } }}

查询结果

{
"took": 2, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 1, "max_score": 0.2876821, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "5", "_score": 0.2876821, "_source": {
"name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": "普通人" } } ] }}

4、返回指定条数(size)

GET /emp/base_info/_search{
"query": {
"match_all": {
} }, "size": 2}

查询结果

{
"took": 1, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 4, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "5", "_score": 1, "_source": {
"name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": "普通人" } }, {
"_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } } ] }}

5、分页查询(size、from)

GET /emp/base_info/_search{
"query": {
"match_all": {
} }, "size": 2, "from": 1}

查询结果

{
"took": 1, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 4, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, {
"_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } } ] }}

6、只查找指定字段

GET /emp/base_info/_search{
"query":{
"match": {
"name": "赵六" } }, "_source": ["name","salary"]}

查询结果

{
"took": 2, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 1, "max_score": 0.2876821, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "5", "_score": 0.2876821, "_source": {
"name": "赵六", "salary": 8888 } } ] }}

7、多字段查询(multi_match)

GET /emp/base_info/_search{
"query":{
"multi_match": {
"query": "王者", "fields": ["name","detail"] #在指定字段中按条件查询 } }}

查询结果

{
"took": 2, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 3, "max_score": 0.87546873, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 0.87546873, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, {
"_index": "emp", "_type": "base_info", "_id": "10", "_score": 0.6931472, "_source": {
"name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, {
"_index": "emp", "_type": "base_info", "_id": "11", "_score": 0.18232156, "_source": {
"name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}

8、分词查询(term)

GET /emp/base_info/_search{
"query":{
"term": {
"detail": {
"value": "王" } } }}

查询结果

{
"took": 1, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 2, "max_score": 0.18232156, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 0.18232156, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, {
"_index": "emp", "_type": "base_info", "_id": "11", "_score": 0.18232156, "_source": {
"name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}

如果查“老王”

GET /emp/base_info/_search{
"query":{
"term": {
"detail": {
"value": "老王" } } }}

查询不到数据,说明默认的分词器对中文会按照一个字一个字的进行分词

{
"took": 0, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 0, "max_score": null, "hits": [] }}

9、范围查询(range)

GET /emp/base_info/_search{
"query": {
"range": {
"salary": {
"gte": 10000, "lte": 20000 } } }}

查询结果

{
"took": 0, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 2, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } } ] }}

10、前缀查询(prefix)

GET /emp/base_info/_search{
"query": {
"prefix": {
"name": {
"value": "王" } } }}

查询结果如下,name字段的type是keyword,之前使用match是查询不到的。

{
"took": 2, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 2, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": {
"name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } } ] }}

11、通配符查询(wildcard)

GET /emp/base_info/_search{
"query": {
"wildcard": {
"name": {
"value": "王*" } } }}

查询结果

{
"took": 2, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 2, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": {
"name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } } ] }}

12、模糊查询(fuzzy)

GET /emp/base_info/_search{
"query": {
"fuzzy": {
"detail": "王" } }}

13、ids查询

GET /emp/base_info/_search{
"query": {
"ids": {
"values": ["1","2"] } }}

查询结果

{
"took": 0, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 2, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, {
"_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } } ] }}

转载地址:http://ehlrb.baihongyu.com/

你可能感兴趣的文章
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel 操作redis的各种数据类型
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>