Term Vectors

Term Vectors(词条向量)

返回有关特定文档字段中的词条的信息和统计信息。文档可以存储在索引中或由用户人工提供。词条向量默认为实时,不是近实时。这可以通过将realtime参数设置为false来更改。

  1. GET /twitter/tweet/1/_termvectors

可选的,您可以使用url中的参数指定检索信息的字段:

  1. GET /twitter/tweet/1/_termvectors?fields=message

或通过在请求主体中添加请求的字段(参见下面的示例)。也可以使用通配符指定字段,类似于多匹配查询

警告

请注意/_termvector的使用方式在2.0中已废弃,请使用_termvectors替代。

返回值

请求可以得到三种类型的值:词条信息,词条统计和字段统计。默认情况下,所有词条信息与字段统计信息都会被返回,但不包含词条统计信息。

词条信息

  • 在字段中的词频(总是返回)
  • 词条位置(positions: true
  • 开始与结束的偏移量(offsets: true
  • 词条有效载荷(payloads: true),base64编码的字节

如果请求的信息没有存储在索引中,如果可能它将被即时计算。另外,对于甚至不存在于索引中但由用户提供的文档,也可以计算词条向量。

警告

开始与结束的偏移量假设UTF-16编码被使用。如果要使用这些偏移量来从原始文本中获取词条,则应确保使用UTF-16对正在使用的子字符串进行编码。

词条统计

设置term_statisticstrue(默认为false)将返回:

  • 总词频(所有文件中的词条频率)
  • 文档频率(包含词条的文档数)

默认情况下这些值不返回,因为词条统计数据会严重影响性能。

字段统计

field_statistics设置为false(默认值为true)将省略:

  • 文档数(包含此字段的文档数)
  • 文档频率的总和(本字段中所有词条的文档频率的总和)
  • 词频的总和(该字段中每个词条的词频的总和)

词条过滤

使用参数filter,返回的词条也可以根据其tf-idf分数进行过滤。这可能是有用的良好特征向量,以便找到文档。此功能的工作方式与More Like This Query第二章节相似。参见示例5的使用。 支持以下子参数:

参数名 描述
max_num_terms 每个字段必须返回的最大词条数。默认为25
min_term_freq 在源文档中忽略少于此频率的单词。默认为1
max_term_freq 在源文档中忽略超过此频率的单词。默认为无界。
min_doc_freq 忽略文档频率少于此参数的词条。默认为1
max_doc_freq 忽略文档频率大于此参数的词条。默认为无界。
min_word_length 字词长度低于此参数的将被忽略。默认为0
max_word_length 字词长度大于此参数的将被忽略。默认为无界(0)。

行为

词条和字段统计数据不准确。删除的文件不被考虑。这些信息只能用于所请求文档所在的分片。因此,术语和字段统计信息仅用作相对度量,而绝对数字在此上下文中无意义。默认情况下,当请求人造文档的词条向量时,随机选择获取统计信息的分片。使用routing将命中特定的分片。

示例:返回存储词条向量

首先,我们创建一个存储词条向量、有效载荷等的索引:

  1. PUT /twitter/
  2. { "mappings": {
  3. "tweet": {
  4. "properties": {
  5. "text": {
  6. "type": "text",
  7. "term_vector": "with_positions_offsets_payloads",
  8. "store" : true,
  9. "analyzer" : "fulltext_analyzer"
  10. },
  11. "fullname": {
  12. "type": "text",
  13. "term_vector": "with_positions_offsets_payloads",
  14. "analyzer" : "fulltext_analyzer"
  15. }
  16. }
  17. }
  18. },
  19. "settings" : {
  20. "index" : {
  21. "number_of_shards" : 1,
  22. "number_of_replicas" : 0
  23. },
  24. "analysis": {
  25. "analyzer": {
  26. "fulltext_analyzer": {
  27. "type": "custom",
  28. "tokenizer": "whitespace",
  29. "filter": [
  30. "lowercase",
  31. "type_as_payload"
  32. ]
  33. }
  34. }
  35. }
  36. }
  37. }

然后,我们添加一些文档:

  1. PUT /twitter/tweet/1
  2. {
  3. "fullname" : "John Doe",
  4. "text" : "twitter test test test "
  5. }
  6. PUT /twitter/tweet/2
  7. {
  8. "fullname" : "Jane Doe",
  9. "text" : "Another twitter test ..."
  10. }

以下请求返回文档1(John Doe)中字段text的所有信息和统计信息:

  1. GET /twitter/tweet/1/_termvectors
  2. {
  3. "fields" : ["text"],
  4. "offsets" : true,
  5. "payloads" : true,
  6. "positions" : true,
  7. "term_statistics" : true,
  8. "field_statistics" : true
  9. }

响应:

  1. {
  2. "_id": "1",
  3. "_index": "twitter",
  4. "_type": "tweet",
  5. "_version": 1,
  6. "found": true,
  7. "took": 6,
  8. "term_vectors": {
  9. "text": {
  10. "field_statistics": {
  11. "doc_count": 2,
  12. "sum_doc_freq": 6,
  13. "sum_ttf": 8
  14. },
  15. "terms": {
  16. "test": {
  17. "doc_freq": 2,
  18. "term_freq": 3,
  19. "tokens": [
  20. {
  21. "end_offset": 12,
  22. "payload": "d29yZA==",
  23. "position": 1,
  24. "start_offset": 8
  25. },
  26. {
  27. "end_offset": 17,
  28. "payload": "d29yZA==",
  29. "position": 2,
  30. "start_offset": 13
  31. },
  32. {
  33. "end_offset": 22,
  34. "payload": "d29yZA==",
  35. "position": 3,
  36. "start_offset": 18
  37. }
  38. ],
  39. "ttf": 4
  40. },
  41. "twitter": {
  42. "doc_freq": 2,
  43. "term_freq": 1,
  44. "tokens": [
  45. {
  46. "end_offset": 7,
  47. "payload": "d29yZA==",
  48. "position": 0,
  49. "start_offset": 0
  50. }
  51. ],
  52. "ttf": 2
  53. }
  54. }
  55. }
  56. }
  57. }

示例:自动生成词条向量

未明确存储在索引中的词条向量将自动计算。以下请求返回文档1中字段的所有信息和统计信息,即使词条尚未明确存储在索引中。请注意,对于字段text,术语不会重新生成。

  1. GET /twitter/tweet/1/_termvectors
  2. {
  3. "fields" : ["text", "some_field_without_term_vectors"],
  4. "offsets" : true,
  5. "positions" : true,
  6. "term_statistics" : true,
  7. "field_statistics" : true
  8. }

示例:人造文档

还可以为人造文档生成词条向量,也就是生成索引中不存在的文档。例如,以下请求将返回与示例1中相同的结果。所使用的映射由索引和类型确定。

如果动态映射打开(默认),则不在原始映射中的文档字段将被动态创建。

  1. GET /twitter/tweet/_termvectors
  2. {
  3. "doc" : {
  4. "fullname" : "John Doe",
  5. "text" : "twitter test test test"
  6. }
  7. }

Per-field 分析器

另外,可以通过使用per_field_analyzer参数来提供不同于当前的分析器。这对于以任何方式生成词条向量是有用的,特别是在使用人造文档时。当为已经存储的词条向量提供分析器时,将重新生成项向量。

  1. GET /twitter/tweet/_termvectors
  2. {
  3. "doc" : {
  4. "fullname" : "John Doe",
  5. "text" : "twitter test test test"
  6. },
  7. "fields": ["fullname"],
  8. "per_field_analyzer" : {
  9. "fullname": "keyword"
  10. }
  11. }

响应:

  1. {
  2. "_index": "twitter",
  3. "_type": "tweet",
  4. "_version": 0,
  5. "found": true,
  6. "took": 6,
  7. "term_vectors": {
  8. "fullname": {
  9. "field_statistics": {
  10. "sum_doc_freq": 2,
  11. "doc_count": 4,
  12. "sum_ttf": 4
  13. },
  14. "terms": {
  15. "John Doe": {
  16. "term_freq": 1,
  17. "tokens": [
  18. {
  19. "position": 0,
  20. "start_offset": 0,
  21. "end_offset": 8
  22. }
  23. ]
  24. }
  25. }
  26. }
  27. }
  28. }

示例:词条过滤

最后,返回的词条可以根据他们的tf-idf分数进行过滤。在下面的例子中,我们从具有给定“plot”字段值的人造文档中获取三个“interesting”的关键字。请注意,关键字“Tony”或任何停止词不是响应的一部分,因为它们的tf-idf必须太低。

  1. GET /imdb/movies/_termvectors
  2. {
  3. "doc": {
  4. "plot": "When wealthy industrialist Tony Stark is forced to build an armored suit after a life-threatening incident, he ultimately decides to use its technology to fight against evil."
  5. },
  6. "term_statistics" : true,
  7. "field_statistics" : true,
  8. "positions": false,
  9. "offsets": false,
  10. "filter" : {
  11. "max_num_terms" : 3,
  12. "min_term_freq" : 1,
  13. "min_doc_freq" : 1
  14. }
  15. }

响应:

  1. {
  2. "_index": "imdb",
  3. "_type": "movies",
  4. "_version": 0,
  5. "found": true,
  6. "term_vectors": {
  7. "plot": {
  8. "field_statistics": {
  9. "sum_doc_freq": 3384269,
  10. "doc_count": 176214,
  11. "sum_ttf": 3753460
  12. },
  13. "terms": {
  14. "armored": {
  15. "doc_freq": 27,
  16. "ttf": 27,
  17. "term_freq": 1,
  18. "score": 9.74725
  19. },
  20. "industrialist": {
  21. "doc_freq": 88,
  22. "ttf": 88,
  23. "term_freq": 1,
  24. "score": 8.590818
  25. },
  26. "stark": {
  27. "doc_freq": 44,
  28. "ttf": 47,
  29. "term_freq": 1,
  30. "score": 9.272792
  31. }
  32. }
  33. }
  34. }
  35. }