转摘自:https://elasticstack.blog.csdn.net/article/details/114261636

Elasticsearch 是一个应用非常广泛的搜索引擎。它可以对文字进行分词,从而实现全文搜索。在实际的使用中,我们会发现有一些文字中包含一些表情符号,比如笑脸,动物等等,那么我们该如何对这些表情符号来进行搜索呢?

     => , light skin tone, skin tone, type 1–2
=> , medium-light skin tone, skin tone, type 3
=> , medium skin tone, skin tone, type 4
=> , medium-dark skin tone, skin tone, type 5
=> , dark skin tone, skin tone, type 6
♪ => ♪, eighth, music, note
♭ => ♭, bemolle, flat, music, note
♯ => ♯, dièse, diesis, music, note, sharp
=> , face, grin, grinning face
=> , face, grinning face with big eyes, mouth, open, smile
=> , eye, face, grinning face with smiling eyes, mouth, open, smile
=> , beaming face with smiling eyes, eye, face, grin, smile
=> , face, grinning squinting face, laugh, mouth, satisfied, smile
=> , cold, face, grinning face with sweat, open, smile, sweat
=> , face, floor, laugh, rofl, rolling, rolling on the floor laughing, rotfl
=> , face, face with tears of joy, joy, laugh, tear
=> , face, slightly smiling face, smile
=> , face, upside-down
=> , face, wink, winking face => , tiger
=> , leopard
=> , face, horse
=> , equestrian, horse, racehorse, racing
=> , face, unicorn
=> , stripe, zebra
=> , deer 在上面,我们可以看到各种各样的 emoji 符号。比如我们想搜索 grin,那么它就把含有 emoji 符号的文档也找出来。在今天的文章中,我们来展示如何实现对 emoji 符号的进行搜索。 安装 如果你还没有对 Elasticsearch 及 Kibana 进行安装的话,请参阅之前的文章 “Elastic:菜鸟上手指南” 进行安装。 另外,我们必须安装 ICU analyzer。关于 ICU analyzer 的安装,请参阅之前的文章 “Elasticsearch:ICU 分词器介绍”。我们在 Elasticsearch 的安装根目录中,打入如下的命令: ./bin/elasticsearch-plugin install analysis-icu 等安装好后,我们需要重新启动 Elasticsearch 让它起作用。运行: ./bin/elasticsearch-plugin list 上面的命令显示: $ ./bin/elasticsearch-plugin install analysis-icu
-> Installing analysis-icu
-> Downloading analysis-icu from elastic
[=================================================] 100%
-> Installed analysis-icu
$ ./bin/elasticsearch-plugin list
analysis-icu 安装完 ICU analyzer 后,我们必须重新启动 Elasticsearch。 搜索 emoji 符号 我们先做一个简单的实验: GET /_analyze
{
"tokenizer": "icu_tokenizer",
"text": "I live in and I'm ‍"
} 上面使用 icu_tokenizer 来对 “I live in and I'm ‍” 进行分词。 ‍ 表情符号非常独特,因为它是更经典的 和 表情符号的组合。 中国的国旗也很特别,它是 和 的组合。 因此,我们不仅在谈论正确地分割 Unicode 代码点,而且在这里真正地了解了表情符号。 上面的请求的返回结果为: {
"tokens" : [
{
"token" : "I",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "live",
"start_offset" : 2,
"end_offset" : 6,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "in",
"start_offset" : 7,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : """""",
"start_offset" : 10,
"end_offset" : 14,
"type" : "<EMOJI>",
"position" : 3
},
{
"token" : "and",
"start_offset" : 16,
"end_offset" : 19,
"type" : "<ALPHANUM>",
"position" : 4
},
{
"token" : "I'm",
"start_offset" : 20,
"end_offset" : 23,
"type" : "<ALPHANUM>",
"position" : 5
},
{
"token" : """‍""",
"start_offset" : 24,
"end_offset" : 29,
"type" : "<EMOJI>",
"position" : 6
}
]
} 显然 emoji 的符号被正确地分词,并能被搜索。 在实际的使用中,我们可能并不限限于对这些 emoji 的符号的搜索。比如我们想对如下的文档进行搜索: PUT emoji-capable/_doc/1
{
"content": "I like "
} 上面的文档中含有一个 ,也就是老虎。针对上面的文档,我们想搜索 tiger 的时候,也能正确地搜索到文档,那么我们该如何去做呢? 在 github 上面,有一个项目叫做 https://github.com/jolicode/emoji-search/。在它的项目中,有一个目录 https://github.com/jolicode/emoji-search/tree/master/synonyms。这里其实就是同义词的目录。我们现在下载其中的一个文件 https://github.com/jolicode/emoji-search/blob/master/synonyms/cldr-emoji-annotation-synonyms-en.txt 到 Elasticsearch 的本地安装目录: config
├── analysis
│ ├── cldr-emoji-annotation-synonyms-en.txt
│ └── emoticons.txt
├── elasticsearch.yml
... 在我的电脑上: $ pwd
/Users/liuxg/elastic1/elasticsearch-7.11.0/config
$ tree -L 3
.
├── analysis
│ └── cldr-emoji-annotation-synonyms-en.txt
├── elasticsearch.keystore
├── elasticsearch.yml
├── jvm.options
├── jvm.options.d
├── log4j2.properties
├── role_mapping.yml
├── roles.yml
├── users
└── users_roles 在上面的 cldr-emoji-annotation-synonyms-en.txt 的文件中,它包含了常见 emoji 的符号的同义词。比如: => , face, grin, grinning face
=> , face, grinning face with big eyes, mouth, open, smile
=> , eye, face, grinning face with smiling eyes, mouth, open, smile
=> , beaming face with smiling eyes, eye, face, grin, smile
=> , face, grinning squinting face, laugh, mouth, satisfied, smile
=> , cold, face, grinning face with sweat, open, smile, sweat
.... 为此,我们来进行如下的实验: PUT /emoji-capable
{
"settings": {
"analysis": {
"filter": {
"english_emoji": {
"type": "synonym",
"synonyms_path": "analysis/cldr-emoji-annotation-synonyms-en.txt"
}
},
"analyzer": {
"english_with_emoji": {
"tokenizer": "icu_tokenizer",
"filter": [
"english_emoji"
]
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "english_with_emoji"
}
}
}
} 在上面,我们定义了 english_with_emoji 分词器,同时我们在定义 content 字段时也使用相同的分词器 english_with_emoji。我们使用 _analyze API 来进行如下的使用: GET emoji-capable/_analyze
{
"analyzer": "english_with_emoji",
"text": "I like "
} 上面的命令返回: {
"tokens" : [
{
"token" : "I",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "like",
"start_offset" : 2,
"end_offset" : 6,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : """""",
"start_offset" : 7,
"end_offset" : 9,
"type" : "SYNONYM",
"position" : 2
},
{
"token" : "tiger",
"start_offset" : 7,
"end_offset" : 9,
"type" : "SYNONYM",
"position" : 2
}
]
} 显然它除了返回 , 也同时返回了 tiger 这样的 token。也就是说我们可以同时搜索这两种,都可以搜索到这个文档。同样地: GET emoji-capable/_analyze
{
"analyzer": "english_with_emoji",
"text": " means happy"
} 它返回: {
"tokens" : [
{
"token" : """""",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
},
{
"token" : "face",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
},
{
"token" : "grin",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
},
{
"token" : "grinning",
"start_offset" : 0,
"end_offset" : 2,
"type" : "SYNONYM",
"position" : 0
},
{
"token" : "means",
"start_offset" : 3,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "face",
"start_offset" : 3,
"end_offset" : 8,
"type" : "SYNONYM",
"position" : 1
},
{
"token" : "happy",
"start_offset" : 9,
"end_offset" : 14,
"type" : "<ALPHANUM>",
"position" : 2
}
]
} 它表明,如果我们搜索 face, grinning,grin,该文档也会被正确地返回。 现在,我们输入如下的两个文档: PUT emoji-capable/_doc/1
{
"content": "I like "
} PUT emoji-capable/_doc/2
{
"content": " means happy"
} 我们对文档进行如下的搜索: GET emoji-capable/_search
{
"query": {
"match": {
"content": ""
}
}
} 或: GET emoji-capable/_search
{
"query": {
"match": {
"content": "tiger"
}
}
} 他们都将返回第一个文档: {
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.8514803,
"hits" : [
{
"_index" : "emoji-capable",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.8514803,
"_source" : {
"content" : """I like """
}
}
]
}
} 通用地,我们进行如下的搜索: GET emoji-capable/_search
{
"query": {
"match": {
"content": ""
}
}
} 或者: GET emoji-capable/_search
{
"query": {
"match": {
"content": "grin"
}
}
} 它们都将返回第二个文档: {
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.8514803,
"hits" : [
{
"_index" : "emoji-capable",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.8514803,
"_source" : {
"content" : """ means happy"""
}
}
]
}
}

Elasticsearch:如何实现对 emoji 表情符号进行搜索的更多相关文章

  1. struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)

    课时22 基于XML配置方式实现对action的所有方法进行校验   使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...

  2. 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验

    在struts2中,我们能够实现对action的全部方法进行校验或者对action的指定方法进行校验. 对于输入校验struts2提供了两种实现方法: 1. 採用手工编写代码实现. 2. 基于XML配 ...

  3. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

  4. jeecms系统使用介绍——通过二次开发实现对word、pdf、txt等上传附件的全文检索

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76912307 本文出自[我是干勾鱼的博客] 之前在文章<基于Java的门户 ...

  5. 基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理

    http://www.cnblogs.com/wuhuacong/p/4175266.html 在一般的管理系统模块里面,越来越多的设计到一些常用文档的上传保存操作,其中如PDF.Word.Excel ...

  6. C#代码实现对HTTP POST参数进行排序

    private static string GetSortedParas(Dictionary<string, string> dic) { dic = dic.OrderBy(key = ...

  7. 在VS2015中用C++创建DLL并用C#调用且同时实现对DLL的调试

    from:http://m.blog.csdn.net/article/details?id=51075023 在VS2015中先创建C#项目,然后再创建要编写的动态库DLL项目,这样做的好处是整个解 ...

  8. Emoji表情符号录入MySQL数据库报错的解决方案(MySQL utf8与utf8mb4区别)

    本文转自:http://blog.itpub.net/26230597/viewspace-1243233/前言:手机app应用评论的时候,恢复表情符号,提示失败. 1,查看tomcat后台日志,核心 ...

  9. 【JAVA使用XPath、DOM4J解析XML文件,实现对XML文件的CRUD操作】

    一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作. 2.去网上下载一个“XPath帮助文档”,以便于查看语法等详细信息,最好是那种有很多实例的那种. 3.学习X ...

随机推荐

  1. 7.5 The Morning after Halloween

    本题主要是存储的问题,在存储上笔者原先的代码占用了大量的内存空间 这边笔者采用暴力的思想直接硬开所有情况的16^6的数组来存储该问题,当然这在时间上是十分浪费的,因为初始化实在太慢了,剩下的就是状态转 ...

  2. ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置

    目标是做一个机械臂视觉抓取的demo,在基地里翻箱倒柜,没有找到学长所说的 d435,倒是找到了一个老古董 kinect 360. 前几天就已经在旧电脑上配置好了,现在记录在新电脑上的配置过程. 1. ...

  3. Netty源码解读(一)-前置准备

    前置条件 源码版本netty4.1 了解Java NIO.Reactor模型和Netty的基本使用. 解释一下: Java NIO:了解BIO和NIO的区别以及Java NIO基础API的使用 Rea ...

  4. vue 数据更新了但视图没改变?试试 $set

    场景 编辑表格中某行数据时,需要把它赋值给对话框表单 this.form,如果直接用 = 赋值,会导致:表单的输入框内容无法二次编辑. 使用 Vue-dev-tool 的 Components 功能测 ...

  5. 从零开始Blazor Server(1)--项目搭建

    项目介绍 本次项目准备搭建一个使用Furion框架,Blazor的UI使用BootstrapBlazor.数据库ORM使用Freesql的后台管理系统. 目前的规划是实现简单的注册,登录.增加管理员跟 ...

  6. 20220724-Java的封装相关

    目录 含义 常见使用方法 个人理解 含义 封装 (encapsulation) 指隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读取和修改的访问级别. 常见使用方法 class Pers ...

  7. Vue路由器的hash和history两种工作模式 && Vue项目编译部署

    1 # 一.Vue路由器的两种工作模式 2 # 1.对于一个uri来说,什么是hash值? 井号及其后面的内容就是hash值. 3 # 2.hash值不会包括含在HTTP请求中,即:hash值不会带给 ...

  8. EB和Varuxn的单字聊天

    持续更新! 本文已经征得\(Varuxn\)同意,仅当做记录网课的趣事和"深厚"的友情 原标题<ErB和Varuxn的单字聊天> 原标题来源: 这个想法来源是 \(Va ...

  9. 强大博客搭建全过程(1)-hexo博客搭建保姆级教程

    1. 前言 本人本来使用国内的开源项目solo搭建了博客,但感觉1核CPU2G内存的服务器,还是稍微有点重,包括服务器内还搭建了数据库.如果自己开发然后搭建,耗费时间又比较多,于是乎开始寻找轻量型的博 ...

  10. Windows 查看端口占用并关闭

    在启动服务的时候,可能会遇到端口被占用的情况. 这时候就需要知道哪个服务占用了这个端口,并将其关闭. 然后再启动服务就不会存在端口占用了. 这里以 Tomcat 的默认端口 8080 为例. 打开命令 ...