• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Elasticsearch:Text vs. keyword - 它们之间的差异以及它们的行为方式

武飞扬头像
Elasticsearch
帮助66

前言

很多刚开始学习 Elasticsearch 的人经常会混淆 textkeyword 字段数据类型。 它们之间的区别很简单,但非常关键。 在本文中,我将讨论两者之间的区别、如何使用它们、它们的行为方式以及使用哪一种。

区别

它们之间的关键区别在于,Elasticsearch 会在将 text 存储到倒排索引之前对其进行分析,而不会分析 keyword 类型。 分析或不分析将影响它在被查询时的行为方式。

>如果你刚开始学习 Elasticsearch,还不知道什么是 Inverted Index 和 Analyzer,我建议你先阅读文章 “Elasticsearch:inverted index,doc_values 及 source”。

如何使用它们

如果你将包含字符串的文档索引到 Elasticsearch 之前没有定义到字段的映射,Elasticsearch 将创建一个包含 text 和 keyword 数据类型的动态映射。 但即使它适用于动态映射,我建议你在索引任何文档之前根据用例定义映射设置,以节省空间并提高写入速度。

这些是 text 和 keyword 类型的映射设置示例,请注意,我将使用我之前为该示例创建的名为 text-vs-keyword 的索引。

keyword mapping



1.  # Create index
2.  PUT text-vs-keyword

4.  # Create keyword mapping
5.  PUT text-vs-keyword/_mapping
6.  {
7.    "properties": {
8.      "keyword_field": {
9.        "type": "keyword"
10.      }
11.    }
12.  }


text mapping



1.  # Create text mapping
2.  PUT text-vs-keyword/_mapping
3.  {
4.    "properties": {
5.      "text_field": {
6.        "type": "text"
7.      }
8.    }
9.  }


Multi Fields



1.  PUT text-vs-keyword/_mapping
2.  {
3.    "properties": {
4.      "text_and_keyword_mapping": {
5.        "type": "text",
6.        "fields": {
7.          "keyword_type": {
8.            "type":"keyword"
9.          }
10.        }
11.      }
12.    }
13.  }


 运行上面的三个命令之后,我们可以看到 text-vs-keyword 的 mapping 为:

GET text-vs-keyword/_mapping

上述命令的返回值为:



1.  {
2.    "text-vs-keyword": {
3.      "mappings": {
4.        "properties": {
5.          "keyword_field": {
6.            "type": "keyword"
7.          },
8.          "text_and_keyword_mapping": {
9.            "type": "text",
10.            "fields": {
11.              "keyword_type": {
12.                "type": "keyword"
13.              }
14.            }
15.          },
16.          "text_field": {
17.            "type": "text"
18.          }
19.        }
20.      }
21.    }
22.  }


上面显示了三个字段,它们有不同的字段类型定义。特别值得指出的是:上面的 keyword_field 及 keyword_type 这些名称都是开发者可以自己定义的名称,但是在很大的情况下,我们把他们的名字都取为 keyword,如下:



1.  {
2.    "text-vs-keyword": {
3.      "mappings": {
4.        "properties": {
5.          "keyword": {
6.            "type": "keyword"
7.          },
8.          "text_and_keyword_mapping": {
9.            "type": "text",
10.            "fields": {
11.              "keyword": {
12.                "type": "keyword"
13.              }
14.            }
15.          },
16.          "text_field": {
17.            "type": "text"
18.          }
19.        }
20.      }
21.    }
22.  }


他们是如何工作的

这两种字段类型在倒排索引中的索引方式不同。 索引过程的差异会影响n你何时对 Elasticsearch 进行查询。

让我们索引一个文档,例如:



1.  POST text-vs-keyword/_doc
2.  {
3.    "keyword_field": "The quick brown fox jumps over the lazy dog",
4.    "text_field": "The quick brown fox jumps over the lazy dog"
5.  }


上述命令将将生成如一个文档。你可以通过如下的方式来进行搜索:

GET text-vs-keyword/_search


1.  {
2.    "took": 4,
3.    "timed_out": false,
4.    "_shards": {
5.      "total": 1,
6.      "successful": 1,
7.      "skipped": 0,
8.      "failed": 0
9.    },
10.    "hits": {
11.      "total": {
12.        "value": 1,
13.        "relation": "eq"
14.      },
15.      "max_score": 1,
16.      "hits": [
17.        {
18.          "_index": "text-vs-keyword",
19.          "_id": "fS95JoYBS2OSAePn1Qxh",
20.          "_score": 1,
21.          "_source": {
22.            "keyword_field": "The quick brown fox jumps over the lazy dog",
23.            "text_field": "The quick brown fox jumps over the lazy dog"
24.          }
25.        }
26.      ]
27.    }
28.  }


keyword

让我们从更简单的 keyword 开始。 Elasticsearch 不会分析 Keyword 数据类型,这意味着你索引的 String 将保持原样。

那么,对于上面的例子,倒排索引中的字符串会是什么样子呢?

Term count
The quick brown fox jumps over the lazy dog
1

是的,你没看错,就是你写的那样。

Text

与 keyword 字段数据类型不同,索引到 Elasticsearch 的字符串在存储到倒排索引之前会经过分词器过程。 默认情况下,Elasticsearch 的标准分词器将拆分并小写化我们索引的字符串。 你可以在 Elasticsearch 的文档中了解有关标准分析器的更多信息。

>Elasticsearch 有一个 API 可以检查文本在分析过程后的样子,我们可以尝试一下:



1.  GET _analyze
2.  {
3.    "text": "The quick brown fox jumps over the lazy dog",
4.    "analyzer": "standard"
5.  }


我们可以看到如下的返回结果:



1.  {
2.    "tokens": [
3.      {
4.        "token": "the",
5.        "start_offset": 0,
6.        "end_offset": 3,
7.        "type": "<ALPHANUM>",
8.        "position": 0
9.      },
10.      {
11.        "token": "quick",
12.        "start_offset": 4,
13.        "end_offset": 9,
14.        "type": "<ALPHANUM>",
15.        "position": 1
16.      },
17.      {
18.        "token": "brown",
19.        "start_offset": 10,
20.        "end_offset": 15,
21.        "type": "<ALPHANUM>",
22.        "position": 2
23.      },
24.      {
25.        "token": "fox",
26.        "start_offset": 16,
27.        "end_offset": 19,
28.        "type": "<ALPHANUM>",
29.        "position": 3
30.      },
31.      {
32.        "token": "jumps",
33.        "start_offset": 20,
34.        "end_offset": 25,
35.        "type": "<ALPHANUM>",
36.        "position": 4
37.      },
38.      {
39.        "token": "over",
40.        "start_offset": 26,
41.        "end_offset": 30,
42.        "type": "<ALPHANUM>",
43.        "position": 5
44.      },
45.      {
46.        "token": "the",
47.        "start_offset": 31,
48.        "end_offset": 34,
49.        "type": "<ALPHANUM>",
50.        "position": 6
51.      },
52.      {
53.        "token": "lazy",
54.        "start_offset": 35,
55.        "end_offset": 39,
56.        "type": "<ALPHANUM>",
57.        "position": 7
58.      },
59.      {
60.        "token": "dog",
61.        "start_offset": 40,
62.        "end_offset": 43,
63.        "type": "<ALPHANUM>",
64.        "position": 8
65.      }
66.    ]
67.  }


所以根据上面的回复,这就是 text_field 字段的倒排索引的样子:

Term Count
the 1
quick 1
brown 1
fox 1
jumps 1
over 1
the 1
lazy 1
dog 1

与 keyword 略有不同,对吧? 但是你需要注意它在倒排索引中存储的内容,因为它会主要影响查询过程。

文本和关键字查询

现在我们了解了 text 和 keyword 在索引时的行为方式,让我们了解它们在被查询时的行为方式。

首先,我们必须知道字符串的查询有两种类型:

Match Query 和 Term Query 和 text 和 keyword 一样,区别在于 Match Query 中的 query 会先解析为 terms,而 Term Query 中的 query 不会。Term query 不分析搜索词。 Term query 仅搜索你提供的确切术语。 这意味着在搜索 text 字段时,术语查询可能返回较差的结果或没有返回结果。

查询 Elasticsearch 的工作原理是将查询的词与倒排索引中的词进行匹配,查询的词和倒排索引中的词必须完全相同,否则匹配不上。 这意味着在索引和查询结果中分析过的字符串和未分析过的字符串会产生截然不同的结果。

使用 Term Query 查询 keyword 字段

因为字段数据类型和查询都没有被分析,所以它们都需要完全相同才能产生结果。

如果我们尝试使用完全相同的查询:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "term": {
5.        "keyword_field": {
6.          "value": "The quick brown fox jumps over the lazy dog"
7.        }
8.      }
9.    }
10.  }


上述命令返回的结果为:



1.  {
2.    "took": 2,
3.    "timed_out": false,
4.    "_shards": {
5.      "total": 1,
6.      "successful": 1,
7.      "skipped": 0,
8.      "failed": 0
9.    },
10.    "hits": {
11.      "total": {
12.        "value": 1,
13.        "relation": "eq"
14.      },
15.      "max_score": 0.2876821,
16.      "hits": [
17.        {
18.          "_index": "text-vs-keyword",
19.          "_id": "fS95JoYBS2OSAePn1Qxh",
20.          "_score": 0.2876821,
21.          "_source": {
22.            "keyword_field": "The quick brown fox jumps over the lazy dog",
23.            "text_field": "The quick brown fox jumps over the lazy dog"
24.          }
25.        }
26.      ]
27.    }
28.  }


显然之前写入的文档被搜索到了。如果我们尝试一些不准确的东西,即使倒排索引中有这个词:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "term": {
5.        "keyword_field": {
6.          "value": "The"
7.        }
8.      }
9.    }
10.  }


它没有返回任何结果,因为查询中的术语与倒排索引中的任何术语都不匹配。

使用 Match Query 查询 keyword 字段

让我们首先尝试使用 Match Query 对 keyword_field 查询相同的字符串 “The quick brown fox jumps over the lazy dog”,看看会发生什么:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "match": {
5.        "keyword_field": "The quick brown fox jumps over the lazy dog"
6.      }
7.    }
8.  }


结果应该是:



1.  {
2.    "took": 0,
3.    "timed_out": false,
4.    "_shards": {
5.      "total": 1,
6.      "successful": 1,
7.      "skipped": 0,
8.      "failed": 0
9.    },
10.    "hits": {
11.      "total": {
12.        "value": 1,
13.        "relation": "eq"
14.      },
15.      "max_score": 0.2876821,
16.      "hits": [
17.        {
18.          "_index": "text-vs-keyword",
19.          "_id": "fS95JoYBS2OSAePn1Qxh",
20.          "_score": 0.2876821,
21.          "_source": {
22.            "keyword_field": "The quick brown fox jumps over the lazy dog",
23.            "text_field": "The quick brown fox jumps over the lazy dog"
24.          }
25.        }
26.      ]
27.    }
28.  }


等等,它不应该产生任何结果,因为查询时,针对查询的文字 “The quick brown fox jumps over the lazy dog” 需要进行分词。如果按照使用 standard 分词器对它进行分析,它产生的术语与倒排索引中的 “The quick brown fox jumps over the lazy dog” (这是一整个术语)不完全匹配,但为什么它会产生结果呢?

没错,查询被分析是因为我们使用的是 Match Query,但 Elasticsearch 使用的不是 standard 分析器,而是 index-time 分析器,它被映射到 keyword 字段数据类型。 由于与 keyword 字段数据类型映射的分词器是 keyword Analyzer,因此 Elasticsearch 在查询中没有任何改变。我们尝试使用 term analyzer 来试试:



1.  GET _analyze
2.  {
3.    "analyzer": "keyword",
4.    "text": "The quick brown fox jumps over the lazy dog"
5.  }


上述命令将生成:



1.  {
2.    "tokens": [
3.      {
4.        "token": "The quick brown fox jumps over the lazy dog",
5.        "start_offset": 0,
6.        "end_offset": 43,
7.        "type": "word",
8.        "position": 0
9.      }
10.    ]
11.  }


可以见得,它就是只有一个 term。

现在,让我们尝试使用 standard 分析器:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "match": {
5.        "keyword_field": {
6.          "query": "The quick brown fox jumps over the lazy dog",
7.          "analyzer": "standard"
8.        }
9.      }
10.    }
11.  }


在上面,我们定义了 analyzer。这个实际上是 search_analyer。请详细阅读 “Elasticsearch: analyzer”。上述命令将不会返回任何的结果。其原因显而易见,查询字符串的倒排术语和 keyword_field 字段里的术语完全不同。

使用 Term Query 查询 text 类型

正如我们在上一节中看到的那样,text 类型的索引文档将包含许多术语。 为了显示查询如何与倒排索引中的术语匹配,让我们尝试两个查询,第一个查询将整个句子发送到 Elasticsearch:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "term": {
5.        "text_field": {
6.          "value": "The quick brown fox jumps over the lazy dog"
7.        }
8.      }
9.    }
10.  }


由于使用 Term query 时,它不会对搜索的字符串 “The quick brown fox jumps over the lazy dog” 进行任何的分词,而 text_field 的倒排索引中没有这么长的完整术语。所以上述的查询不会有任何的结果。

 我们再进行如下的查询:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "term": {
5.        "text_field": "The"
6.      }
7.    }
8.  }


这两个查询都没有结果。

第一个查询没有产生结果,因为在倒排索引中,我们从未存储过整个句子,索引过程只存储已经从文本中分块的术语。

第二个查询也没有产生任何结果。 索引文档中有一个“The”,但记住分析器将单词小写,所以在 Inverted Index 中,它存储为 the

让我们用 the 再次尝试 Term 查询:



1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "term": {
5.        "text_field": "the"
6.      }
7.    }
8.  }


是的! 它产生了一个结果,因为查询的 the 与倒排索引中的 the 完全匹配。

使用 Match query 查询 text 类型

现在是使用 Match Query 进行 text 类型处理的时候了,因为它会分析这两种类型,所以很容易让它们产生结果。 让我们先尝试两个查询:

  • 第一个查询会将 The 发送到 Elasticsearch,我们知道使用 term query 不会产生任何结果,但是 match query 呢?
  • 第二个查询将发送 the LAZ dog tripped over the QUICK brown dog,有些词在倒排索引中,有些不在,Elasticsearch 会从中产生任何结果吗?


1.  GET text-vs-keyword/_search
2.  {
3.    "query": {
4.      "match": {
5.        "text_field": "The"
6.      }
7.    }
8.  }

10.  GET text-vs-keyword/_search
11.  {
12.    "query": {
13.      "match": {
14.        "text_field": "the LAZ dog tripped over th QUICK brown dog"
15.      }
16.    }
17.  }


是的! 两者都产生了结果:



1.  {
2.    "took": 2,
3.    "timed_out": false,
4.    "_shards": {
5.      "total": 1,
6.      "successful": 1,
7.      "skipped": 0,
8.      "failed": 0
9.    },
10.    "hits": {
11.      "total": {
12.        "value": 1,
13.        "relation": "eq"
14.      },
15.      "max_score": 1.8339733,
16.      "hits": [
17.        {
18.          "_index": "text-vs-keyword",
19.          "_id": "fS95JoYBS2OSAePn1Qxh",
20.          "_score": 1.8339733,
21.          "_source": {
22.            "keyword_field": "The quick brown fox jumps over the lazy dog",
23.            "text_field": "The quick brown fox jumps over the lazy dog"
24.          }
25.        }
26.      ]
27.    }
28.  }


第一个查询产生结果是因为查询中的 The 被分析并成为与倒排索引中的完全匹配的 the。

第二个查询,虽然并非所有术语都在倒排索引中,但仍会产生一个结果。 Elasticsearch 将返回一个结果,即使只有一个查询的术语与倒排索引中的术语完全匹配。

如果你注意结果,有一个 _score 字段。 有多少查询词与倒排索引中的词完全匹配是影响分数的因素之一。请阅读我的另外文章 “Elasticsearch:分布式计分”。

该选 text 还是 keyword 呢?

在以下情况下使用 keyword 字段数据类型:

  • 你想要一个完全匹配查询
  • 你想让 Elasticsearch 像其他数据库一样运行
  • 你想用它来进行通配符查询

在以下情况下使用 text 字段数据类型:

  • 你想创建一个自动完成
  • 你想创建一个搜索系统

结论

了解 text 和 keyword 字段数据类型的工作原理是你想要在 Elasticsearch 中学习的内容之一,区别看似简单但很重要。

你需要了解并选择适合您的用例的字段数据类型,如果你需要两种字段数据类型,则可以在创建映射时使用 multi fields 功能。比如在我们上面已经创建的 text_and_keyword_mapping 字段。

最后,希望本文能帮助大家学习 Elasticsearch,了解 Elasticsearch 中 text 和 keyword 字段数据类型的区别。 谢谢阅读!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfbfei
系列文章
更多 icon
同类精品
更多 icon
继续加载