Elasticsearch 计数分词中的token使用实例
正文
在我们针对 text 类型的字段进行分词时,分词器会把该字段分解为一个个的 token。如果你对分词器还不是很理解的话,请参考我之前的文章 “Elasticsearch: analyzer”。在分词时,有一个叫做 token_count 的类型。该类型是 token 的计数器,也就是说,我们可以使用它来了解在索引字段时在字符串中生成的 token 数量。
我们下面用一个比较简单的例子来进行展示。在我们的示例中,我们将索引一些书名,并且我们将过滤标题中只有 2 个 token 的书。
` PUT book_token_count_test { "mappings": { "properties": { "book_name": { "type": "text", "fields": { "size": { "type": "token_count", "analyzer": "standard" } } } } } } `![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
使用命令写入文档
我们使用如下的命令来写入一下文档:
POST book_token_count_test/_bulk {"index":{}} { "book_name": "Ulysses" } {"index":{}} { "book_name": "Don Quixote" } {"index":{}} { "book_name": "One Hundred Years of Solitude" }
搜索 token 文档
我们使用如下的命令来搜索 token 数为 2 的文档:
GET book_token_count_test/_search { "query": { "term": { "book_name.size": { "value": "2" } } } }
上面搜索的结果为:
` { "took": 273, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "book_token_count_test", "_id": "cxczBoYB6OPboMnB7TQu", "_score": 1, "_source": { "book_name": "Don Quixote" } } ] } } `![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
我们可以使用 range 查询来检索 book_name 中包含 3 个以上 token 的文档,我们只会得到标题为 “One Hundred Years of Solitude” 的文档。
GET book_token_count_test/_search { "query": { "range": { "book_name.size": { "gte": 3 } } } }
上面搜索的结果为:
` { "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "book_token_count_test", "_id": "dBczBoYB6OPboMnB7TQu", "_score": 1, "_source": { "book_name": "One Hundred Years of Solitude" } } ] } } `![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
以上就是Elasticsearch 计数分词中的token使用实例的详细内容,更多关于Elasticsearch计数分词token的资料请关注脚本之家其它相关文章!
相关文章
SpringBoot中实现分布式的Session共享的详细教程
这篇文章主要介绍了SpringBoot中实现分布式的Session共享,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-06-06java利用Apache commons codec进行MD5加密,BASE64加密解密,执行系统命令
这篇文章主要介绍了java利用apache Commons包进行MD5加密,BASE64加密解密与执行系统命令希望对大家有用2017-12-12java中对象的比较equal、Comparble、Comparator的区别
本文主要介绍了java中对象的比较equal、Comparble、Comparator的区别,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-10-10SpringMVC中的HandlerMapping和HandlerAdapter详解
这篇文章主要介绍了SpringMVC中的HandlerMapping和HandlerAdapter详解,在Spring MVC中,HandlerMapping(处理器映射器)用于确定请求处理器对象,请求处理器可以是任何对象,只要它们使用了@Controller注解或注解@RequestMapping,需要的朋友可以参考下2023-08-08
最新评论