概述
注:
- 本篇博客的内容参考自 es 7.x版本的官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/term-level-queries.html
- 博客中的所有代码示例,都已经在7.1.1版本的es集群中验证通过
term-level查询,通常使用在结构化数据的查询中,是用于精确值查找的。
所谓的结构化数据,指的是比如数值(short、integer、long、float、double等8种类型)、日期(date)、keyword、boolean、ip等类型。
跟全文检索不一样的是,term-level查询不会对输入进行分词,相反的,它会直接将输入作为精确值,去做精确匹配查找
。
term-level查询有如下几种类型:
-
term query
term query 查找 指定字段中
包含
了某个精确值的文档,类似于java中xxSet.contains(“xxx”)。 -
terms query
查找指定字段中
包含
查询词根集合中 的任意一个精确值的文档。类似于java中的xxSet.contains(“xxx_1”) or xxSet.contains(“xxx_2”)。 -
terms_set query
查找与一个或多个指定词根相匹配的文档,而匹配的项的数量则可以由参数来指定。
-
range query
范围查询
类似于如下sqlselect * from tb where price >= 10 and price < 20
-
exists query
返回在原始字段中至少有一个非空值的文档
类似于如下sqlselect * from tb where dept_id is null 或者 select * from tb where dept_id is not null
-
prefix query
前缀查询
类似于如下sqlselect * from tb where name like '苏%'
-
wildcard query
通配符查询
-
fuzzy query
模糊查询
-
regexp query
正则表达式查询
-
type query
指定类型查询
-
ids query
ids数组查询。
通俗易懂的说,上面的11
中查询类型,也就是接下来要详细讲解的东西,对比到sql上,其实就是教大家在查询的时候如何去写where条件的,而且是:在面对各种数据类型的字段、或者是特定某类查询的时候,该如何正确的书写where条件。并且,本篇博客所讲的,还是where条件的最小单元
——也就说,举个例子,当我们讲解到term query的时候,我们只会讲到如何去查询dept = “IT”,而不会讲到复合查询如dept = “IT” or dept = “BIGDATA”。
样例数据
在开始之前,为了后面的测试方便,这里我统一设置了一个名为idx-susu-test-term-query的索引,使用手动设置mappings的方式,尽可能多的插入了不同类型的字段,并且也插入了尽可能多情形的数据。
# 1.为了防止index已存在,先删除一下
DELETE idx-susu-test-term-query
# 2.手动设置index的mapping
PUT idx-susu-test-term-query/
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"birth": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"salary": {
"type": "float"
},
"detp": {
"type": "keyword"
},
"skill": {
"type": "keyword"
},
"skill_count": {
"type": "short"
},
"skill_required_matches": {
"type": "integer"
},
"addr": {
"type": "text",
"analyzer": "ik_max_word"
},
"isBoss": {
"type": "boolean"
}
}
}
}
# 3.批量插入数据,同时刷新index
POST idx-susu-test-term-query/_bulk?refresh=true
{
"index": {
"_id": "1" }}
{
"id": 1, "birth": "1980-01-01 00:00:00", "salary": 40000, "dept": "BIGDATA", "skill": ["HADOOP", "JAVA", "PYTHON"], "skill_count": 3, "skill_required_matches": 2, "addr": "北京昌平区", "isBoss": false}
{
"index": {
"_id": "2" }}
{
"id": 2, "birth": "1985-02-01 10:20:00", "salary": 35000, "dept": "BIGDATA", "skill": ["JAVA", "PYTHON"], "skill_count": 2, "skill_required_matches": 2, "addr": "北京海淀区", "isBoss": false}
{
"index": {
"_id": "3" }}
{
"id": 3, "birth": "1989-11-01 05:00:10", "salary": 50000, "dept": "SALE", "skill": ["PPT", "EXCEL"], "skill_count": 2, "skill_required_matches": 1, "addr": "北京朝阳区", "isBoss": false}
{
"index": {
"_id": "4" }}
{
"id": 4, "birth": "1990-01-01 11:11:11", "salary": 40000, "dept": "IT", "skill": ["HADOOP", "PHP", "C++"], "skill_count": 3, "skill_required_matches": 3, "addr": "北京昌平区", "isBoss": false}
{
"index": {
"_id": "5" }}
{
"id": 5, "birth": "1991-06-01 12:12:12", "salary": 30000, "dept": "IT", "skill": ["HADOOP", "JAVA", "C++"], "skill_count": 3, "skill_required_matches": 4, "addr": "北京昌平区", "isBoss": false}
{
"index": {
"_id": "6" }}
{
"id": 6, "birth": "2001-03-01 13:13:13", "salary": 40000, "dept": "IT", "skill": null, "skill_count": 0, "skill_required_matches": 0, "addr": "北京昌平区", "isBoss": false}
{
"index": {
"_id": "7" }}
{
"id": 7, "birth": "1995-09-01 16:16:16", "salary": 20000, "dept": "UI", "skill": [null, "EXCEL"],