分析器(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. MySQL统计信息以及执行计划预估方式初探

    数据库中的统计信息在不同(精确)程度上描述了表中数据的分布情况,执行计划通过统计信息获取符合查询条件的数据大小(行数),来指导执行计划的生成.在以Oracle和SQLServer为代表的商业数据库,和 ...

  2. 【原】The Linux Command Line - Processes

    ps - report a snapshot of current processes top - display tasks job - list active jobs bg - place a ...

  3. Spring再接触 模拟Spring

    项目分层: 1.最土的方法是直接写到main中去 2.分出model层 2.如下 4.在抽象一个对数据库的访问层(跨数据库实现) 面向抽象编程 User.java package com.bjsxt. ...

  4. 对于链表中tada的绝对值相等的点,仅保留第一次出现的结点而删除其余绝对值相等的点

    算法的核心思想是用空间换时间,使用辅助数组记录链表中已出现的数值  从而只需对链表进行一趟扫描 typedef struct node { int data; struct node* next; } ...

  5. WordCount 3

    学号:201631062130.201631062304 码云地址:https://gitee.com/xnsy/WordCountPlus 一.代码互审情况:在代码的互审过程中,在命令和路径没有没有 ...

  6. jeesite 下载ckfinder上传的文件

    在需要下载的位置,将以下代码复制到页面最下方,就可以实现文件下载了 <script> $(document).ready(function() { var fileName = $(&qu ...

  7. easyui改变tab标题

    截图:   代码: //更改tab的标题 var tab = $('#microAppVersionTabs').tabs('getTab',0);// 取得第一个tab $('#microAppVe ...

  8. Page Visibility(网页可见性) API与登录同步引导页实例页面

    页面1  HTML代码: <p id="loginInfo"></p> JS代码: (function() {     if (typeof pageVis ...

  9. select2插件设置选中值并显示的问题

    在select2中,要想设置指定值为选中状态并显示: $("#select2_Id").val("XXXXX").select2() 或者 var obj= $ ...

  10. python note 03 切片及对字符串操作

    1.计算 1 - 2 + 3 ... + 99 中除了88以外的数之和 i = 1 sum = 0 while i < 100 : if i == 88 : i = i + 1 continue ...