Elasticsearch:wildcard - 通配符搜索

Elasticsearch 是一个分布式、免费和开放的搜索和分析引擎,适用于所有类型的数据,例如文本、数字、地理空间、结构化和非结构化数据。 它基于 Apache Lucene 构建,Apache Lucene 是一个全文搜索引擎,可用于各种编程语言。 由于其速度、可扩展性以及对不同类型内容进行索引的能力,Elasticsearch 已在多种用例中得到应用,例如:

  • 企业搜索
  • 日志记录和日志分析
  • 应用搜索
  • 商业分析
  • 地理空间数据分析和可视化

它是如何工作的?

Elasticsearch 不是将信息存储为列式数据行,而是存储已序列化为 JSON 文档的复杂数据结构。 每个文档由一组键(文档中的字段或属性的名称)及其相应的值(字符串、数字、布尔值、日期、值数组、地理位置或其他类型的数据)组成。 它使用一种称为倒排索引的数据结构,列出任何文档中出现的每个唯一单词,并标识每个单词出现的所有文档。

字段类型 - 分析或未分析

Elasticsearch 中的字符串文字要么被分析,要么未被分析。 那么分析到底是什么意思呢? 已分析字段是指在索引之前经过分析过程的字段。 然后,该分析的结果存储在倒排索引中。 分析过程基本上涉及对文本块进行分词和规范化。 这些字段被分词为术语,并且术语被转换为小写字母。 这是标准分析器的行为,也是默认行为。 但是,如果需要,我们可以指定我们自己的分析器,例如,如果你还想索引特殊字符,而标准分析器则不会这样做。如果你想对 analyzer 有更多的了解,请阅读文章 "Elasticsearch: analyzer"。

我们尝试使用如下的命令来进行分词:

sql 复制代码
1.  GET _analyze
2.  {
3.    "analyzer": "standard",
4.    "text" : "Beijing is a beautiful city"
5.  }

Elasticsearch 的标准分析器会将此文本转换为以下内容:

json 复制代码
1.  {
2.    "tokens": [
3.      {
4.        "token": "beijing",
5.        "start_offset": 0,
6.        "end_offset": 7,
7.        "type": "<ALPHANUM>",
8.        "position": 0
9.      },
10.      {
11.        "token": "is",
12.        "start_offset": 8,
13.        "end_offset": 10,
14.        "type": "<ALPHANUM>",
15.        "position": 1
16.      },
17.      {
18.        "token": "a",
19.        "start_offset": 11,
20.        "end_offset": 12,
21.        "type": "<ALPHANUM>",
22.        "position": 2
23.      },
24.      {
25.        "token": "beautiful",
26.        "start_offset": 13,
27.        "end_offset": 22,
28.        "type": "<ALPHANUM>",
29.        "position": 3
30.      },
31.      {
32.        "token": "city",
33.        "start_offset": 23,
34.        "end_offset": 27,
35.        "type": "<ALPHANUM>",
36.        "position": 4
37.      }
38.    ]
39.  }

通配符(wildcard)搜索快速介绍

通配符是特殊字符,充当文本值中未知字符的占位符,并且可以方便地查找具有相似但不相同数据的多个项目。 通配符搜索基于查询中提到的字符与包含这些字符模式的文档中的单词之间的字符模式匹配。

查找名字/姓氏为 John 的每个人

现在我们已经基本了解了 Elasticsearch 的工作原理、分析字段和通配符搜索是什么,让我们更深入地了解本文的主题 --- 字符串字段并对其运行通配符搜索。

字符串字段和通配符搜索

Elasticsearch 中的每个字段都有一个字段数据类型。 此类型指示字段包含的数据类型(例如字符串或布尔值)及其预期用途。 Elasticsearch 中可用于字符串的两种字段类型是 --- text(默认)和 keyword。 它们之间的主要区别在于,文本字段在索引时进行分析,而关键字字段则不然。 这意味着,文本字段在索引之前会被标准化并分解为单独的分词,而关键字字段则按原样存储。 此外,由于文本字段已标准化,因此它们支持不区分大小写的搜索。 为了对关键字字段实现相同的效果,我们必须在创建索引时定义一个 normalizer,然后在定义字段映射时指定相同的 normalizer。有关 nomalizer 的详细介绍,请阅读文章 "Elasticsearch:词分析中的 Normalizer 的使用"。

markdown 复制代码
1.  PUT wildcard
2.  {
3.    "settings": {
4.      "analysis": {
5.        "normalizer": {
6.          "lowercase_normalizer": {
7.            "type": "custom",
8.            "char_filter": [],
9.            "filter": [
10.              "lowercase",
11.              "asciifolding"
12.            ]
13.          }
14.        }
15.      }
16.    },
17.    "mappings": {
18.      "properties": {
19.        "text-field": {
20.          "type": "text"
21.        },
22.        "keyword-field": {
23.          "type": "keyword",
24.          "normalizer": "lowercase_normalizer"
25.        }
26.      }
27.    }
28.  }

现在进行通配符查询,假设我们有以下文档,并且我们想要对其运行一些通配符搜索:

bash 复制代码
1.  PUT wildcard/_doc/1
2.  {
3.    "text-field": "Mockingbirds don't do one thing but make music for us to enjoy.",
4.    "keyword-field": "Mockingbirds don't do one thing but make music for us to enjoy."
5.  }

如下所示的查询可以很好地处理文本字段:

bash 复制代码
1.  GET wildcard/_search?filter_path=**.hits
2.  {
3.    "_source": false, 
4.    "fields": [
5.      "text-field"
6.    ], 
7.    "query": {
8.      "wildcard": {
9.        "text-field": {
10.          "value": "*birds*"
11.        }
12.      }
13.    }
14.  }

上面的搜索返回结果:

json 复制代码
1.  {
2.    "hits": {
3.      "hits": [
4.        {
5.          "_index": "wildcard",
6.          "_id": "1",
7.          "_score": 1,
8.          "fields": {
9.            "text-field": [
10.              "Mockingbirds don't do one thing but make music for us to enjoy."
11.            ]
12.          }
13.        }
14.      ]
15.    }
16.  }

然而,下面的搜索则不会:

bash 复制代码
1.  GET wildcard/_search?filter_path=**.hits
2.  {
3.    "_source": false, 
4.    "fields": [
5.      "text-field"
6.    ], 
7.    "query": {
8.      "wildcard": {
9.        "text-field": {
10.          "value": "*birds*music*"
11.        }
12.      }
13.    }
14.  }

它返回的结果是:

markdown 复制代码
1.  {
2.    "hits": {
3.      "hits": []
4.    }
5.  }

原因是,该字段的单词已被分析并存储为分词。 因此,elasticsearch 无法找到与给定表达式(birdsmusic*)对应的分词。

但是,这适用于关键字字段,因为它们按原样存储。我们来尝试如下的搜索:

bash 复制代码
1.  GET wildcard/_search?filter_path=**.hits
2.  {
3.    "_source": false,
4.    "fields": [
5.      "keyword-field"
6.    ],
7.    "query": {
8.      "wildcard": {
9.        "keyword-field": {
10.          "value": "*birds*music*"
11.        }
12.      }
13.    }
14.  }

上面的命令返回的结果是:

json 复制代码
1.  {
2.    "hits": {
3.      "hits": [
4.        {
5.          "_index": "wildcard",
6.          "_id": "1",
7.          "_score": 1,
8.          "fields": {
9.            "keyword-field": [
10.              "mockingbirds don't do one thing but make music for us to enjoy."
11.            ]
12.          }
13.        }
14.      ]
15.    }
16.  }

现在,让我们讨论从 ElasticSearch v7.9 引入的另一个字符串字段------通配符。 这是一种专门的字段类型,主要用于非结构化机器生成的内容。更多阅读,请参阅文章 "Elasticsearch:使用新的 wildcard 字段更快地在字符串中查找字符串 - 7.9 新功能"。

以下是对这 3 种字段类型运行几个通配符查询的性能统计数据:

Query : Elasticsearch --- Full word search

Query : Wal --- Substring search

Query : Elasticstash* --- Search across multiple words

我们可以清楚地看到,关键字字段的性能在所有搜索查询和索引大小中是最一致的。 文本字段也做得不错,但它们不能用于搜索像 Elasticstash* 这样的值,这使得关键字类型成为明显的赢家。

那么为什么要引入通配符字段呢? 那么,引入通配符字段是为了解决文本和关键字字段存在的以下限制:

  • 文本字段 - 将任何通配符表达式的匹配限制为单个分词,而不是字段中保存的原始整个值。
  • 关键字字段 - 当搜索子字符串和有许多唯一值时,关键字字段的速度很慢。 关键字字段还存在数据大小限制的缺点。 默认字符串映射会忽略长度超过 256 个字符的字符串。 这可以扩展到单个令牌 32k 的 Lucene 硬限制。 当您尝试搜索系统日志和类似文档时,这可能会产生问题。

通配符字段解决了上述限制。 它不会将字符串视为由标点符号分隔的标记集合,而是通过首先对所有文档进行近似匹配,然后对通过匹配接收到的文档子集应用详细比较来执行模式匹配。

文本、关键字和通配符字段之间的详细比较可以在此处阅读。

上述统计信息是通过在 v8.9 上运行的 elasticsearch 索引上运行搜索获得的,映射如下:

markdown 复制代码
1.  {
2.      "wildcard-search-demo-index": {
3.          "mappings": {
4.              "properties": {
5.                  "field1": {
6.                      "type": "text"
7.                  },
8.                  "field2": {
9.                      "type": "keyword"
10.                  },
11.                  "field3": {
12.                      "type": "wildcard"
13.                  }
14.              }
15.          }
16.      }
17.  }

索引的文档在所有字段中具有统一的数据,即文档中的所有 3 个字段都具有相同的值。 例如,

json 复制代码
1.  "hits": [
2.              {
3.                  "_index": "wildcard-search-demo-index",
4.                  "_type": "_doc",
5.                  "_id": "vlPiHYYB6ikeelRg4I8n",
6.                  "_score": 1.0,
7.                  "_source": {
8.                      "field1": "It started as a scalable version of the Lucene open-source search framework then added the ability to horizontally scale Lucene indices.",
9.                      "field2": "It started as a scalable version of the Lucene open-source search framework then added the ability to horizontally scale Lucene indices.",
10.                      "field3": "It started as a scalable version of the Lucene open-source search framework then added the ability to horizontally scale Lucene indices."
11.                  }
12.              },
13.              {
14.                  "_index": "wildcard-search-demo-index",
15.                  "_type": "_doc",
16.                  "_id": "v1PiHYYB6ikeelRg4I87",
17.                  "_score": 1.0,
18.                  "_source": {
19.                      "field1": "Elasticsearch allows you to store, search, and analyze huge volumes of data quickly and in near real-time and give back answers in milliseconds.",
20.                      "field2": "Elasticsearch allows you to store, search, and analyze huge volumes of data quickly and in near real-time and give back answers in milliseconds.",
21.                      "field3": "Elasticsearch allows you to store, search, and analyze huge volumes of data quickly and in near real-time and give back answers in milliseconds."
22.                  }
23.              }
24.          ]

综上所述,字段类型的选择并没有固定的规则。 它取决于多种因素,例如数据类型、必须涵盖的不同用例集等。

在设置数据存储时,决定字段类型是一个非常关键的因素,因为它极大地影响性能,并且应该通过考虑所有可能的场景和因素来决定。

Elasticsearch 还有一种称为通配符的查询类型,可用于运行通配符查询。

另外值得指出的是:由于通配符搜索带来很多的性能问题,有时甚至会吃掉很多的系统资源。在生成环境中,有的建议关掉这个功能以避免影响系统的运行。建议阅读文章:

相关推荐
Elastic 中国社区官方博客3 小时前
不到 5 分钟完成本地部署:Jina embedding 模型现已支持本地部署
大数据·人工智能·elasticsearch·搜索引擎·embedding·jina
二进制流水搬运工3 小时前
入职第一天拉了23个仓库,我写了个脚本解放双手
大数据·elasticsearch·搜索引擎
Elasticsearch4 小时前
在不到一小时内将 Datadog Kubernetes 仪表板迁移到 Elastic Observability
elasticsearch
.柒宇.5 小时前
Elasticsearch 核心概念与系统架构详解
elasticsearch·系统架构
风行無痕7 小时前
单机Elasticsearch 8.19.18安全升级到9.4.3的过程
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客9 小时前
使用重新设计的 AutoOps 更快地进行 Elasticsearch 问题排查
大数据·运维·前端·人工智能·elasticsearch·搜索引擎·全文检索
阿拉雷️10 小时前
【搜索实战】Spring Boot 3.3 + AI Agent × Elasticsearch:让AI自动优化搜索策略,查询速度从2秒压到50毫秒
人工智能·spring boot·elasticsearch
阿里云大数据AI技术1 天前
阿里云 ES AI 引擎版:面向 Agent 场景,为亿级租户、千亿规模向量设计的搜索引擎
人工智能·elasticsearch·agent
阿里云大数据AI技术1 天前
FalconSeek 技术解析:阿里云 Elasticsearch 云原生内核如何让查询性能飙升600%
人工智能·elasticsearch
Elastic 中国社区官方博客1 天前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
大数据·人工智能·elasticsearch·搜索引擎·容器·kubernetes·grafana