分析器(Analyzer)
Elasticsearch 无论是内置分析器还是自定义分析器,都由三部分组成:字符过滤器(Character Filters)、分词器(Tokenizer)、词元过滤器(Token Filters)。 分析器Analyzer工作流程: Input Text => Character Filters(如果有多个,按顺序应用) => Tokenizer => Token Filters(如果有多个,按顺序应用) => Output Token 字符过滤器(Character Filters)
字符过滤器:对原始文本预处理,如去除HTML标签,”&”转成”and”等。 注意:一个分析器同时有多个字符过滤器时,按顺序应用。 分词器(Tokenizer)
分词器:将字符串分解成一系列的词元Token。如根据空格将英文单词分开。 词元过滤器(Token Filters)
词元过滤器:对分词器分出来的词元Token做进一步处理,如转换大小写、移除停用词、单复数转换、同义词转换等。 注意:一个分析器同时有多个词元过滤器时,按顺序应用。 分析器analyze API的使用
分析器analyze API可验证分析器的分析效果并解释分析过程。 # text: 待分析文本
# explain:解释分析过程
# char_filter:字符过滤器
# tokenizer:分词器
# filter:词元过滤器 GET _analyze
{
"char_filter" : ["html_strip"],
"tokenizer": "standard",
"filter": [ "lowercase"],
"text": "<p><em>No <b>dreams</b>, why bother <b>Beijing</b> !</em></p>",
"explain" : true
} 自定义多个分析器
创建索引并自定义多个分析器
这里对一个索引同时定义了多个分析器。 PUT my_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"analysis": {
"char_filter": { //自定义多个字符过滤器
"my_charfilter1": {
"type": "mapping",
"mappings": ["& => and"]
},
"my_charfilter2": {
"type": "pattern_replace",
"pattern": "(\\d+)-(?=\\d)",
"replacement": "$1_"
}
},
"tokenizer":{ //自定义多个分词器
"my_tokenizer1": {
"pattern":"\\s+",
"type":"pattern"
},
"my_tokenizer2":{
"pattern":"_",
"type":"pattern"
}
},
"filter": { //自定义多个词元过滤器
"my_tokenfilter1": {
"type": "stop",
"stopwords": ["the", "a","an"]
},
"my_tokenfilter2": {
"type": "stop",
"stopwords": ["info", "debug"]
}
},
"analyzer": { //自定义多个分析器
"my_analyzer1":{ //分析器my_analyzer1
"char_filter": ["html_strip", "my_charfilter1","my_charfilter2"],
"tokenizer":"my_tokenizer1",
"filter": ["lowercase", "my_tokenfilter1"]
},
"my_analyzer2":{ //分析器my_analyzer2
"char_filter": ["html_strip"],
"tokenizer":"my_tokenizer2",
"filter": ["my_tokenfilter2"]
}
}
}
}
} 验证索引my_index的多个分析器
验证分析器my_analyzer1分析效果
GET /my_index/_analyze
{
"text": "<b>Tom </b> & <b>jerry</b> in the room number 1-1-1",
"analyzer": "my_analyzer1"//,
//"explain": true
} #返回结果
{
"tokens": [
{
"token": "tom",
"start_offset": 3,
"end_offset": 6,
"type": "word",
"position": 0
},
{
"token": "and",
"start_offset": 12,
"end_offset": 13,
"type": "word",
"position": 1
},
{
"token": "jerry",
"start_offset": 17,
"end_offset": 26,
"type": "word",
"position": 2
},
{
"token": "in",
"start_offset": 27,
"end_offset": 29,
"type": "word",
"position": 3
},
{
"token": "room",
"start_offset": 34,
"end_offset": 38,
"type": "word",
"position": 5
},
{
"token": "number",
"start_offset": 39,
"end_offset": 45,
"type": "word",
"position": 6
},
{
"token": "1_1_1",
"start_offset": 46,
"end_offset": 51,
"type": "word",
"position": 7
}
]
} 验证分析器my_analyzer2分析效果
GET /my_index/_analyze
{
"text": "<b>debug_192.168.113.1_971213863506812928</b>",
"analyzer": "my_analyzer2"//,
//"explain": true
} #返回结果
{
"tokens": [
{
"token": "192.168.113.1",
"start_offset": 9,
"end_offset": 22,
"type": "word",
"position": 1
},
{
"token": "971213863506812928",
"start_offset": 23,
"end_offset": 45,
"type": "word",
"position": 2
}
]
} 添加Mapping并为不同字段设置不同分析器
PUT my_index/_mapping/my_type
{
"properties": {
"my_field1": {
"type": "text",
"analyzer": "my_analyzer1",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"my_field2": {
"type": "text",
"analyzer": "my_analyzer2",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
} 创建文档
PUT my_index/my_type/1
{
"my_field1":"<b>Tom </b> & <b>jerry</b> in the room number 1-1-1",
"my_field2":"<b>debug_192.168.113.1_971213863506812928</b>"
} Query-Mathch全文检索
查询时,ES会根据字段使用的分析器进行分析,然后检索。 #查询my_field2字段包含IP:192.168.113.1的文档
GET my_index/_search
{
"query": {
"match": {
"my_field2": "192.168.113.1"
}
}
} #返回结果
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_score": 0.2876821,
"_source": {
"my_field1": "<b>Tom </b> & <b>jerry</b> in the room number 1-1-1",
"my_field2": "<b>debug_192.168.113.1_971213863506812928</b>"
}
}
]
}
}

Elasticsearch 自定义多个分析器的更多相关文章

  1. elasticsearch 自定义_id

    elasticsearch 自定义ID: curl -s -XPUT localhost:9200/web -d ' { "mappings": { "blog" ...

  2. ElasticSearch自定义分析器-集成结巴分词插件

    关于结巴分词 ElasticSearch 插件: https://github.com/huaban/elasticsearch-analysis-jieba 该插件由huaban开发.支持Elast ...

  3. Elasticsearch自定义分析器

    关于分析器 ES中默认使用的是标准分析器(standard analyzer).如果需要对某个字段使用其他分析器,可以在映射中该字段下说明.例如: PUT /my_index { "mapp ...

  4. Elasticsearch 自定义映射

    尽管在很多情况下基本域数据类型 已经够用,但你经常需要为单独域自定义映射 ,特别是字符串域.自定义映射允许你执行下面的操作: 全文字符串域和精确值字符串域的区别 使用特定语言分析器 优化域以适应部分匹 ...

  5. 建立标准编码规则(一)-自定义C#代码分析器

    1.下载Roslyn的Visual Studio分析器模板插件(VS2015 或VS2017) https://marketplace.visualstudio.com/items?itemName= ...

  6. elasticsearch 自定义similarity 插件开发

    转自:http://www.chepoo.com/elasticsearch-similarity-custom-plug-in-development.html 在搜索开发中,我们要修改打分机制,就 ...

  7. elasticsearch自定义动态映射

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/custom-dynamic-mapping.html如果你想在运行时增加新的字 ...

  8. ElasticSearch——自定义模板

    output中配置 elasticsearch{ action => "index" hosts => ["xxx"] index => &q ...

  9. ElasticSearch 自定义排序处理

    使用function_score进行分组处理,利用分组函数script_score进行自定义分值处理, 注意:使用script功能需要在配置中打开脚本功能: script.inline: on   s ...

随机推荐

  1. 深度学习VS机器学习——到底什么区别

    转自:https://baijiahao.baidu.com/s?id=1595509949786067084&wfr=spider&for=pc 最近在听深度学习的课,老师提了一个基 ...

  2. hello2

    String username = request.getParameter("username");//获取参数值 if (username != null && ...

  3. ztree带有选项框的树形菜单使用

    1.ztree简介 zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点.专门适合项目开发,尤其是 树状菜单.树状数据的Web ...

  4. 大数据入门到精通6---spark rdd reduce by key 的使用方法

    1.前期数据准备(同之前的章节) val collegesRdd= sc.textFile("/user/hdfs/CollegeNavigator.csv")val header ...

  5. 20175314 《Java程序设计》第三周学习总结

    20175314 <Java程序设计>第三周学习总结 教材学习内容总结 编程语言的发展事是从面向机器(汇编.机器)到面向过程(C)再到面向对象(Java) 成员变量: 1.成员变量定义在类 ...

  6. Linux Ipv6地址配置

    Step1:启用IPV6网络配置 [root@node-1 ~]# vi /etc/sysconfig/network NETWORKING_IPV6=yes   //全局启用ipv6初始化IPV6_ ...

  7. django mysql 数据库配置

    在settings.py中保存了数据库的连接配置信息,Django默认初始配置使用sqlite数据库. DATABASES = { 'default': { 'ENGINE': 'django.db. ...

  8. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  9. [leetcode]44. Wildcard Matching万能符匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  10. AndFix注意事项

    1.生成补丁,修改前后的apk包都必须签名. 2.AndFix 不支持修改布局文件. 3.文件的路径必须正确. 4.AndFix 不支持添加匿名内部类(就是点击事件). 5.AndFix 不支持添加新 ...