概要

本篇主要介绍一个Term Vector的概念和基本使用方法。

term vector是什么?

每次有document数据插入时,elasticsearch除了对document进行正排、倒排索引的存储之外,如果此索引的field设置了term_vector参数,elasticsearch还会对这个的分词信息进行计算、统计,比如这个document有多少个field,每个field的值分词处理后得到的term的df值,ttf值是多少,每个term存储的位置偏移量等信息,这些统计信息统称为term vector。

term vector的值有5个

  • no:不存储term vector信息,默认值
  • yes:只存储field terms信息,不包含position和offset信息
  • with_positions:存储term信息和position信息
  • with_offsets:存储term信息和offset信息
  • with_positions_offsets:存储完整的term vector信息,包括field terms、position、offset信息。

term vector的信息生成有两种方式:index-time和query-time。index-time即建立索引时生成term vector信息,query-time是在查询过程中实时生成term vector信息,前者以空间换时间,后者以时间换空间。

term vector有什么作用?

term vector本质上是一个数据探查的工具(可以看成是一个debugger工具),上面记录着一个document内的field分词后的term的详细情况,如拆分成几个term,每个term在正排索引的哪个位置,各自的df值、ttf值分别是多少等等。一般用于数据疑似问题的排查,比如说排序和搜索与预期的结果不一致,需要了解根本原因,可以拿这个工具手动进行数据分析,帮助判断问题的根源。

读懂term vector信息

我们来看看一个完整的term vector报文,都有哪些信息,带#号的一行代码是添加的注释,如下示例:

{
"_index": "music",
"_type": "children",
"_id": "1",
"_version": 1,
"found": true,
"took": 0,
"term_vectors": {
"text": {
"field_statistics": {
"sum_doc_freq": 3,
"doc_count": 1,
"sum_ttf": 3
},
"terms": {
"elasticsearch": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 2,
"start_offset": 11,
"end_offset": 24
}
]
},
"hello": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 0,
"start_offset": 0,
"end_offset": 5
}
]
},
"java": {
"doc_freq": 1,
"ttf": 1,
"term_freq": 1,
"tokens": [
{
"position": 1,
"start_offset": 6,
"end_offset": 10
}
]
}
}
}
}
}

一段完整的term vector信息,term vector是按field为维度来统计的,主要包含三个部分:

  • field statistics
  • term statistics
  • term information

field statistics

指该索引和type下所有的document,对这个field所有term的统计信息,注意document的范围,不是某一条,是指定index/type下的所有document。

  • sum_doc_freq(sum of document frequency):这个field中所有的term的df之和。
  • doc_count(document count):有多少document包含这个field,有些document可能没有这个field。
  • sum_ttf(sum of total term frequency):这个field中所有的term的tf之和。

term statistics

hello为当前document中,text field字段分词后的term,查询时设置term_statistics=true时生效。

  • doc_freq(document frequency):有多少document包含这个term。
  • ttf(total term frequency):这个term在所有document中出现的频率。
  • term_freq(term frequency in the field):这个term在当前document中出现的频率。

term information

示例中tokens里面的内容,tokens里面是个数组

  • position:这个term在field里的正排索引位置,如果有多个相同的term,tokens下面会有多条记录。
  • start_offset:这个term在field里的偏移,表示起始位置偏移量。
  • end_offset:这个term在field里的偏移量,表示结束位置偏移量。

term vector使用案例

  1. 建立索引music,type命名为children,指定text字段为index-time,fullname字段为query-time
PUT /music
{
"mappings": {
"children": {
"properties": {
"content": {
"type": "text",
"term_vector": "with_positions_offsets",
"store" : true,
"analyzer" : "standard"
},
"fullname": {
"type": "text",
"analyzer" : "standard"
}
}
}
}
}
  1. 添加3条示例数据
PUT /music/children/1
{
"fullname" : "Jean Ritchie",
"content" : "Love Somebody"
} PUT /music/children/2
{
"fullname" : "John Smith",
"content" : "wake me, shark me ..."
}
PUT /music/children/3
{
"fullname" : "Peter Raffi",
"content" : "brush your teeth"
}
  1. 对document id为1这条数据进行term vector探查
GET /music/children/1/_termvectors
{
"fields" : ["content"],
"offsets" : true,
"positions" : true,
"term_statistics" : true,
"field_statistics" : true
}

得到的结果即为上文的term vector示例。

另外可以提一下,用这3个document的id进行查询,field_statistics部分是一样的。

term vector常见用法

除了上一节的标准查询用法,还有一些参数可以丰富term vector的查询。

  • doc参数
GET /music/children/_termvectors
{
"doc" : {
"fullname" : "Peter Raffi",
"content" : "brush your teeth"
},
"fields" : ["content"],
"offsets" : true,
"positions" : true,
"term_statistics" : true,
"field_statistics" : true
}

这个语法的含义是针对指定的doc进行term vector分析,doc里的内容可以随意指定,特别实用。

  • per_field_analyzer参数

    可以指定字段的分词器进行探查
GET /music/children/_termvectors
{
"doc" : {
"fullname" : "Jimmie Davis",
"content" : "you are my sunshine"
},
"fields" : ["content"],
"offsets" : true,
"positions" : true,
"term_statistics" : true,
"field_statistics" : true,
"per_field_analyzer" : {
"text": "standard"
}
}
  • filter参数

    对term vector统计结果进行过滤
GET /music/children/_termvectors
{
"doc" : {
"fullname" : "Jimmie Davis",
"content" : "you are my sunshine"
},
"fields" : ["content"],
"offsets" : true,
"positions" : true,
"term_statistics" : true,
"field_statistics" : true,
"filter" : {
"max_num_terms" : 3,
"min_term_freq" : 1,
"min_doc_freq" : 1
}
}

根据term统计信息,过滤出你想要看到的term vector统计结果。也挺有用的,比如你探查数据可以过滤掉一些出现频率过低的term。

  • docs参数

    允许你同时对多个doc进行探查,这个使用频率看个人习惯。
GET _mtermvectors
{
"docs": [
{
"_index": "music",
"_type": "children",
"_id": "2",
"term_statistics": true
},
{
"_index": "music",
"_type": "children",
"_id": "1",
"fields": [
"content"
]
}
]
}

term vector使用建议

有两种方式可以得到term vector信息,一种是像上面案例,建立时指定,另一种是直接查询时生成

  • index-time,在mapping里配置,建立索引的时候,就直接给你生成这些term和field的统计信息,如果term_vector设置为with_positions_offsets,索引所占的空间是不设置term vector时的2倍。
  • query-time,你之前没有生成过任何的Term vector信息,然后在查看term vector的时候,直接就可以看到了,会on the fly,现场计算出各种统计信息,然后返回给你。

这两种方式采用哪种取决于对term vector的使用期望,query-time更常用一些,毕竟这个工具的用处是协助定位问题,实时计算就行。

小结

term vector是一个比较实用的工具,尤其是针对线上数据进行分析、协助问题定位的时候,可以派上很大的用场。

专注Java高并发、分布式架构,更多技术干货分享与心得,请关注公众号:Java架构社区

可以扫左边二维码添加好友,邀请你加入Java架构社区微信群共同探讨技术

Elasticsearch系列---Term Vector工具探查数据的更多相关文章

  1. 基于term vector深入探查数据

    1.term vector介绍 获取document中的某个field内的各个term的统计信息 term information: term frequency in the field, term ...

  2. C#进阶系列——WebApi 接口测试工具:WebApiTestClient

    前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员也不懂C#语法,API里面的接口也不能直接给他们看,没办法,只有整个详细一点的文档呗.由于接口个数有点多,每 ...

  3. Map工具系列-01-Map代码生成工具说明

    所有cs端工具集成了一个工具面板 -打开(IE) Map工具系列-01-Map代码生成工具说明 Map工具系列-02-数据迁移工具使用说明 Map工具系列-03-代码生成BySQl工具使用说明 Map ...

  4. Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入

    Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入 本文为系列文章"Web 软件测试 Checklist 应用系列"中的第一篇.该系列文章旨在阐述 Check ...

  5. Java 集合系列 05 Vector详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  6. [搜索]ElasticSearch Java Api(一) -添加数据创建索引

    转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...

  7. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  8. elasticsearch系列五:搜索详解(查询建议介绍、Suggester 介绍)

    一.查询建议介绍 1. 查询建议是什么? 查询建议,为用户提供良好的使用体验.主要包括: 拼写检查: 自动建议查询词(自动补全) 拼写检查如图: 自动建议查询词(自动补全): 2. ES中查询建议的A ...

  9. Red Gate系列 - SQL各种工具

    Red Gate系列 - SQL各种工具 Red Gate系列文章: Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red ...

随机推荐

  1. 2020年iOS进阶面试题总结(一)

    准备找工作的你,可以看看,复习复习!! 1.说一下OC的反射机制 在动态运行下我们可以构建任何一个类,然后我们通过这个类知道这个类的所有的属性和方法,并且如果我们创建一个对象,我们也可以通过对象找到这 ...

  2. Salesforce 学习 | 官方总结最实用的Spring '20新功能

    在Spring '20正式发布之前,Trailblazers 社区举行了一个名为Treasure Hunt的在线活动,通过预览沙盒,分享他们认为Spring ‘20中最重要的功能.这篇文章就来盘点一下 ...

  3. TortoiseSVN的使用,以及冲突解决办法

    接下来,试试用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态, ...

  4. 终于明白if __name__ == '__main__':了

    其实很简单 if __name__ == '__main__': 就是一个判断 __name__是系统变量 __name__有一个特性,在当前文件运行是__main__,调用文件就是调用文件的路径了 ...

  5. [复现]GXY2019

    前言 当时GXY的时候在复习中,临时抱拂脚,没时间打比赛.就写了一题./(ㄒoㄒ)/~~ babysqli 当时做了写了笔记. 过滤了or,()其中or可以用大小写绕过,可以用order by盲注 第 ...

  6. EF-相关查询(逐渐完善)

    linq查询方式 多条件查询 内连接 左连接 可以执行sql含事务

  7. Say goodbye

    Since September 28th 2015 Scriptogram officially closed. We considered every option before making th ...

  8. Flutter 开发填坑指南

    引言 第一次在使用Flutter是在Ubuntu机器上,但是因为Android Studio还有Sdk配置问题,flutter doctor总是在这一步报错...最近又在win10上配了一下环境(真香 ...

  9. 一、搭建SpringBoot2.0.0M4基础Web项目

    本次开发环境为: 系统:Linux Mint 18 JDK:1.8 开发工具:IntelliJ IDEA 2017.2.4 1.启动IDEA工具,开始创建一个基础项目.点击Create New Pro ...

  10. QFileDialog::getOpenFileName() hangs

    https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-d ...