一、简介

有时候我们需要在项目中支持中文 和 拼音的搜索。采用ik分词来做中文分词是目前比好的方式。至于拼音分词可以采用lc-pinyin,虽然lc-pinyin能很好的解决首字母和全拼的搜索,但是在中文分词上却是不支持的,lc-pinyin只能把中文拆成单字来处理。要是能把IK分词和lc-pinyin分词结合那该多好,不是么?本文主要介绍如何把ik和lc-pinyin结合起来使用,使我们的搜索既支持中文搜索,又支持拼音搜索。

环境:elasticsearch1.4.5, elasticsearch-analysis-lc-pinyin1.4.5,elasticsearch-analysis-ik1.3.0

二、配置lc-pinyin和ik分词器

1. 首先需要安装lc-pinyin和ik分词,这里就不再讲如何安装,不会的童鞋参考:http://blog.csdn.net/chennanymy/article/details/52336368

2.  安装好lc-pinyin和ik分词插件后就可以配置分词器额,打开 config/elasticsearch.yml文件,在末尾加上下面的配置

index:analysis:analyzer:ik_max_word:type: ikuse_smart: falseik_smart:type: ikuse_smart: trueik_syno:tokenizer: ikfilter: [ik_synonym_filter]ik_syno_smart:tokenizer: ik_tk_smartfilter: [ik_synonym_filter]lc:alias: [lc_analyzer]type: org.elasticsearch.index.analysis.LcPinyinAnalyzerProviderlc_index:type: lcanalysisMode: indexlc_search:type: lcanalysisMode: searchtokenizer:ik_tk_smart:type: ikuse_smart: truefilter:ik_synonym_filter:type: synonymsynonyms_path: analysis/synonym.txt

上面的配置定义了一个同义词过滤器“ik_synonym_filter” 并指定了一个同义词文件,该文件目录结构如下

对同义词不熟悉的同学,可以参考官网教程:https://www.elastic.co/guide/en/elasticsearch/reference/1.5/analysis-synonym-tokenfilter.html

到这里分词器就配置完成了。

三、测试同义词

下面我们就来测试一下同义词是否有效,打开同义词配置文件在里面加入两行

第一行同义词表示当要索引的文字中包含了 蜡烛、园丁、师傅、先生 都会被转换成 老师 索引到文档中

第二行同义词表示当出现中文、汉语、汉字中任何一个词的时候都把上述3个词索引到文档中。所以这种方式是比较费索引的

上图可以看到我们配置的同义词生效了。

四、搜索测试

首先创建索引和mapping,这里我们采用multi_field来做针对同一字段设置不同的分词器

content字段采用拼音分词,content.cn采用ik分词

curl -XPUT http://localhost:9200/index
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{"fulltext": {"properties": {"content": {"type": "string","index_analyzer": "lc_index","search_analyzer": "lc_search","fields": {"cn": {"type": "string","index_analyzer": "ik_syno","search_analyzer": "ik_syno_smart"}}}}}
}'

然后索引几条数据

curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"湖北工业大学"}
'curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"华中科技大学"}
'curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"武汉大学"}
'curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"武汉理工大学"}
'
curl -XPOST http://localhost:9200/index/fulltext/5 -d'
{"content":"香港中文大学"}
'

执行查询

@Testpublic void testMultiMatch() {final String index = "index";final String type = "fulltext";SearchRequestBuilder requestBuilder = elasticIndexOperateHelper.getClient().prepareSearch(index).setTypes(type);String input = "中文大学";QueryBuilder pinyinSearch = QueryBuilders.matchQuery("content", input).type(MatchQueryBuilder.Type.PHRASE).analyzer("lc_search").boost(4).zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE);QueryBuilder chineseSearch = QueryBuilders.matchQuery("content.cn", input).type(MatchQueryBuilder.Type.BOOLEAN).analyzer("ik_syno_smart").boost(8).zeroTermsQuery(MatchQueryBuilder.ZeroTermsQuery.NONE);QueryBuilder mixQueryBuilder = QueryBuilders.boolQuery().should(pinyinSearch).should(chineseSearch).minimumNumberShouldMatch(1);requestBuilder = requestBuilder.setQuery(mixQueryBuilder).setHighlighterPreTags("<tag1>", "<tag2>").setHighlighterPostTags("</tag1>", "</tag2>").addHighlightedField("content").addHighlightedField("content.cn").setHighlighterRequireFieldMatch(true);SearchResponse response = requestBuilder.execute().actionGet();System.out.println(requestBuilder);System.out.println(response);}

返回结果

{"took" : 734,"timed_out" : false,"_shards" : {"total" : 2,"successful" : 2,"failed" : 0},"hits" : {"total" : 5,"max_score" : 1.7481089,"hits" : [ {"_index" : "index","_type" : "fulltext","_id" : "5","_score" : 1.7481089,"_source":
{"content":"武汉中文大学"}
,"highlight" : {"content.cn" : [ "武汉<tag1>中文</tag1><tag1>大学</tag1>" ],"content" : [ "武汉<tag1>中</tag1><tag1>文</tag1><tag1>大</tag1><tag1>学</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "3","_score" : 0.014395926,"_source":
{"content":"武汉大学"}
,"highlight" : {"content.cn" : [ "武汉<tag1>大学</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "1","_score" : 0.009597284,"_source":
{"content":"湖北工业大学"}
,"highlight" : {"content.cn" : [ "湖北工业<tag1>大学</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "2","_score" : 0.0077423635,"_source":
{"content":"华中科技大学"}
,"highlight" : {"content.cn" : [ "华中科技<tag1>大学</tag1>" ]}}, {"_index" : "index","_type" : "fulltext","_id" : "4","_score" : 0.0061938907,"_source":
{"content":"武汉理工大学"}
,"highlight" : {"content.cn" : [ "武汉理工<tag1>大学</tag1>" ]}} ]}
}

搜索:gongyedaxue

{"took" : 105,"timed_out" : false,"_shards" : {"total" : 2,"successful" : 2,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.9900317,"hits" : [ {"_index" : "index","_type" : "fulltext","_id" : "1","_score" : 0.9900317,"_source":
{"content":"湖北工业大学"}
,"highlight" : {"content" : [ "湖北<tag1>工</tag1><tag1>业</tag1><tag1>大</tag1><tag1>学</tag1>" ]}} ]}
}

转载于:https://www.cnblogs.com/chennanlcy/p/6591792.html

Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文、拼音、同义词搜索相关推荐

  1. [Elasticsearch](五)Docker环境下搭建Elasticsearch,Elasticsearch集群,Elasticsearch-Head以及IK分词插件和拼音分词插件

    目录: https://github.com/dolyw/ProjectStudy/tree/master/Elasticsearch DockerStudy dolyw:https://note.d ...

  2. Elasticsearch生产实战(ik分词器、拼音分词、自动补全、自动纠错)

    目录 一.IK分词器 1.IK分词器介绍 2.安装 3.使用 4.自定义词库 二.拼音分词器 1.拼音分词器介绍 2.安装 三.自动补全 1.效果演示 2.实战 四.自动纠错 1.场景描述 2.DSL ...

  3. 服务器安装配置elasticsearch,kibana,IK分词器和拼音分词器,集群搭建教程

    docker安装配置elasticsearch,kibana和IK分词器 elasticsearch文章系列 前置安装docker 创建docker网络 安装Elasticsearch 运行elast ...

  4. elasticsearch ik分词实现 中文、拼音、同义词搜索

    EasticSearch版本:1.5.2 1.配置分词器:配置IK,参照 <ElasticSearch 安装和使用IK分词器> 2.拼音分词器配置:使用已经编译好的:elasticsear ...

  5. ElasticSerach 6.0.1 测试IK分词器和拼音分词器是否生效

    post http://192.168.1.129:9200/_analyze

  6. ElasticSerach6.0.1测试拼音分词器,IK分词器,并且次测试语法

    第一步:安装ElasticSearch 6.0.1 下载ElasticSerach,下载IK分词器 由于IK和ElasticSerach已经是编译好的,不需要编译,直接在安装解压修改配置文件即可 详情 ...

  7. Elasticsearch安装IK分词器,kibana安装是基本使用,DSL语句入门

    文章目录 1. 安装IK分词器 2. Kibana安装和使用 2.1 ELK概述 2.2 Kibana下载 2.3 DSL语句 1. 安装IK分词器 ElasticSearch 默认采用的分词器, 是 ...

  8. requirednew基于xml配置日志不回滚_Elasticsearch配置IK分词器的远程词库

    在生活中很多很多地方都涉及到了全文检索,最常见的就好比日常使用到的百度搜索等搜索引擎,也都是基于全文检索来实现的:全文检索种类较多,就好比Elasticsearch.Sorl等. 为Ealsticse ...

  9. ik mysql热加载分词_Elasticsearch 之(25)重写IK分词器源码来基于mysql热更新词库...

    热更新在上一节< IK分词器配置文件讲解以及自定义词库>自定义词库,每次都是在es的扩展词典中,手动添加新词语,很坑 (1)每次添加完,都要重启es才能生效,非常麻烦 (2)es是分布式的 ...

最新文章

  1. ARM Linux 基于S3C2451的AD9833波形发生器/Linux字符驱动的理解
  2. 发现价值(1)-无限的网络资源
  3. 【怎样写代码】偷窥高手 -- 反射技术(六):深入窥视DLL内部
  4. 有多个重载参数pow_面试深刨——150分面重载
  5. 新近爆出的runC容器逃逸漏洞,用户如何面对?
  6. (转)简单代码生成器原理剖析(二)
  7. matlab算概率,用matlab计算概率,再次吐槽某些吧友国战比赛七框选将的建议
  8. 让线程等待10秒_把python程序变成多线程
  9. Android 获取触摸点坐标,判断滑动方向,滑动距离,滑动速度
  10. Vue如何循环提取对象数组中的值
  11. python入门教材论坛_Python初学者(零基础学习Python、Python入门)书籍、视频、资料、社区推荐...
  12. 虚拟机中的linux系统联网,虚拟机上Linux系统上网设置
  13. 跟开涛老师学shiro -- 身份验证
  14. ios13.7 iPhone/iPad Location-cleaned 驱动下载
  15. 拖动滑块验证 php,基于JS组件实现拖动滑块验证功能
  16. VBM后的双样本t检验
  17. 一个很好用的移动端Lightbox特效插件(一)
  18. 如何使用Robostudio加载地图?
  19. python——基础3
  20. 【视觉高级篇】27 # 如何实现简单的3D可视化图表:GitHub贡献图表的3D可视化?

热门文章

  1. java springcloud版b2b2c社交电商spring cloud分布式微服务 (四) 断路器(Hystrix)
  2. 压力测试工具gatling安装和介绍
  3. 进阶第四课 Python模块之os
  4. FSM状态机之状态模式
  5. Canvas、Paint、Path
  6. Uploadify在asp.net下使用Demo
  7. JQuery Tab菜单的实现
  8. Javascript中最常用的61段经典代码
  9. Gotchas 44-引用和临时对象
  10. 继承机制中的构造器和析构器 - C++快速入门17