Elasticsearch学习---Query DSL基本操作

版本说明

本文基于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": "普通人"
        }
      }
    ]
  }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值