4

es笔记五之term-level的查询操作 - XHunter

 1 year ago
source link: https://www.cnblogs.com/hunterxiong/p/17426759.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文首发于公众号:Hunter后端
原文链接:es笔记五之term-level的查询操作

官方文档上写的是 term-level queries,表义为基于准确值的对文档的查询,可以理解为对 keyword 类型或者 text 类型分词为 keyword 的字段进行 term 形式的精确查找。

以下是本篇笔记目录:

  1. 是否存在值
  2. 大小于操作
  3. term 查询
  4. terms 查询
  5. wildcard 查询

1、是否存在值

exists 查询某个字段是否存在值。

还是使用上篇笔记讲的 exam 这个 index,我们创建一条数据,只给定 name 的值,那么 address 的值就 null,或者说查询返回的数据就没有这个字段了。

PUT /exam/_doc/12
{
    "name" : "test"
}

然后我们查询 address 字段有值的数据:

GET /exam/_search
{
  "query": {
    "exists": {
      "field": "address"
    }
  }
}

就可以发现返回的数据中没有我们创建的这条数据,或者我们取反操作,查询 address 字段没有值的数据:

GET /exam/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"exists": {"field": "address"}}
      ]
    }
  }
}

2、前缀搜索

对于我们在前面创建的这条数据:

PUT /exam/_doc/16
{
    "name" : "张三丰",
    "address": "一个苹果"
}

如果是 name 字段,因为它是一个 keyword 类型,所以它是一个整体不会被分词处理,我们可以搜索 name 的值为 '张', '张三' 和 '张三丰' 都可以搜索到。

GET /exam/_search
{
  "query": {
    "prefix": {
      "name": {
        "value": "张"
      }
    }
  }
}

但是对于 address 字段,发现是可以搜索到 '一','一个' 和 '苹果',但是搜索 '一个苹',或者 '一个苹果' 是搜不到结果的。

GET /exam/_search
{
  "query": {
    "prefix": {
      "address": {
        "value": "一个苹"
      }
    }
  }
}

我们可以看一下 '一个苹果' 的分词结果:

GET /exam/_doc/16/_termvectors?fields=address

可以发现可以搜索到的词都在以分词结果的开头或者全部,但是 '一个苹' 是没有分词结果以此为开头的。

所以这里我们的搜索操作是基于 address 字段的分词结果列表来查询的。

如果想要搜索到从 '一' 开始到结尾之间任意地点截断的数据,我们就需要将 address 字段作为一个整体来搜索,那就是加上 .keyword 来操作。

GET /exam/_search
{
  "query": {
    "prefix": {
      "address.keyword": {
        "value": "一个苹"
      }
    }
  }
}

3、大小于操作

前面介绍了 gt, gte, lt, lte 的操作是在 bool 下的 filter 里操作,这里我们可以直接放到 query 下:

GET /bank/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

4、term 查询

前面介绍过 term 查询是一种精确查询,但是官方文档提醒我们应该尽量避免对 text 字段使用 term 查询,因为 text 类型的数据在写入的时候会被分词,通过 term 查询我们可能搜索不到想要的查询的数据。同时建议我们查询 text 字段应当使用 match 操作。

我们使用官方文档提供的一个示例来说明为什么应该尽量避免使用 term 查询来查询 text 字段,其实前面我们介绍过相关的示例,这里单独拿出来做一下说明。

还是使用我们前面用过的索引 exam,我们来写入一条数据:

PUT /exam/_doc/18
{
  "address": "quick brown foxes"
}

然后我们想要搜索 'quick brown foxes' 这个字符串,使用下面的操作:

GET /exam/_search
{
  "query": {
    "term": {
      "address": {
        "value": "quick brown foxes"
      }
    }
  }
}

这个肯定是搜索不到的,因为这个字符串在写入的时候已经被分词处理了,而 term 是一个精确查找,相当于搜索一整个字符串,这就肯定搜索不到了。

但是我们可以使用 match,match 操作会在搜索前先对搜索的字符串进行分词处理,然后进行匹配操作,所以使用下面的操作是可以搜索到数据的:

GET /exam/_search
{
  "query": {
    "match": {
      "address": "quick brown foxes"
    }
  }
}

前面还介绍过,如果想要搜索一整个 address 的值为我们搜索的字符串内容,可以使用 address.keyword:

GET /exam/_search
{
  "query": {
    "term": {
      "address.keyword": "quick brown foxes"
    }
  }
}

5、terms 查询

如果想要同时搜索多个精确字段值,比如搜索 "quick" 和 "苹果",就可以使用 terms:

GET /exam/_search
{
  "query": {
    "terms": {
      "address": ["quick", "苹果"]
    }
  }
}

6、wildcard 查询

wildcard 是通配符的意思,这里的用法有点类似于前缀的操作,都是通过符号来实现更为随意的匹配。

这里有两个通配符,一个是 *,一个是 ?

* 的作用是 0 到 n 个字符长度

比如我搜索 qui* 就可以查到 quick 的数据:

GET /exam/_search
{
  "query": {
    "wildcard": {
      "address": {
        "value": "qui*"
      }
    }
  }
}

? 的作用是匹配任意单个字符,比如我们搜索 qui?k,也可以查询到这条数据:

GET /exam/_search
{
  "query": {
    "wildcard": {
      "address": {
        "value": "qui?k"
      }
    }
  }
}

如果想获取更多后端相关文章,可扫码关注阅读:

image

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK