分析器(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. Object.defineProperty(o,p,descriptor ) 理解应用

    1. Object.defineProperty  在一个对象上定义一个新属性,或修改一个已经存在的属性, 最终返回这个对象. var __define = this.__define || func ...

  2. A CLOSER LOOK AT CSS

    A CLOSER LOOK AT CSS css-review Congratulations! You worked hard and made it to the end of a challen ...

  3. 尚硅谷springboot学习31-jdbc数据连接

    可以使用JdbcTemplate操作数据库,可以在启动的时候自动建表,更新数据表 配置依赖 <dependency> <groupId>org.springframework. ...

  4. jmeter 实现 mysql 存储过程

    Callable Statement:存储过程语句.可以在一个脚本里实现增删改查. 实现方法: 1)首先创建一个存储过程 2)然后执行这个存储过程

  5. 框架和内嵌框架--->frameset 和 iframe 的文档对象

    框架和内嵌框架分别用 HTMLFrameElemnt 和 HTMLIFrameElement 表示,它们在 DOM2 中有一个新属性----->contentDocument,是一个指针,表示框 ...

  6. php面试题五之nginx如何调用php和php-fpm的作用和工作原理

    nginx如何调用php 采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一 ...

  7. [leetcode]27. Remove Element删除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  8. angularJs, ui-grid 设置默认group, 及排序

  9. (转载)Ubuntu 安装GNU Scientific library(GSL)

    背景: Blei的hlda的C语言实现需要使用C语言的科学计算包GSL,因此决定安装.由于在windows下安装极其繁琐,先在Linux上安装之. 系统环境: Linux version 2.6.35 ...

  10. face_recognition 相关依赖

    centos-v:7 python-v:3.7 IDE:pycharm 安装顺序: boost boost-py cmake numpy opencv-python scipy 安装方法:settin ...