多字段特性及Mapping中配置自定义Analyzer
PUT logs/_doc/1
{“level”:“DEBUG”}
我们在创建索引的时候,可以创建多字段类型,通过多字段类型可以进行一些精确匹配,也可以使用不同的analyzer分词
GET /logs/_mapping
exact values(keyword) 精确值 full text(text) 全文检索 exact values 就是不需要进行分词
character filters 特性过滤(当分词后设置特定 的过滤方式)
tokenizer 分词器
position 位置 offset 偏移量
token filters 过滤器(设置一下停用词,大小写转换,近义词等)
可以通过_analyze对于文本的分词结果进行测试
指定tokenizer为keyword char_filter 为html_strip 则表示,按照keyword进行分词,也就是不做什么处理,将测试的值当成一个词整体 原样输出 并去除标签
POST _analyze
{
"tokenizer": "keyword",
"char_filter": ["html_strip"],
"text": "<b>hello world</b>"
}
将词作为文件路径进行分词
POST _analyze
{
“tokenizer”:“path_hierarchy”,
“text”:"/user/raven/a/b/c/d/e"
}
也可以使用char_filter 进行一些特定的替换过滤
使用 standard进行分词 设置了特定的character filters 将- 替换为了_
POST _analyze
{
"tokenizer" : "standard",
"char_filter" : [
{
"type" : "mapping",
"mappings" : ["- => _"]
}
],
"text": "123-456, I-test! test-990 650-555-1234"
}
white space and snowball 使用空格进行分词,并且将一些没有意义的词进行剔除停用 通过snowball将词还原为最初的样子
GET _analyze
{
"tokenizer": "whitespace",
"filter": ["stop","snowball"],
"text": ["The gilrs in China are playing this game!"]
}
GET _analyze
{
"tokenizer": "whitespace",
"filter": ["stop","snowball"],
"text": ["The rain in Spain falls mainly on the plain."]
}
lowercase 将词转为小写,又因为存在stop 所以the会被剔除
GET _analyze
{
"tokenizer": "whitespace",
"filter": ["lowercase","stop","snowball"],
"text": ["The gilrs in China are playing this game!"]
}
正则表达式
GET _analyze
{
"tokenizer": "standard",
"char_filter": [
{
"type": "pattern_replace",
"pattern": "http://(.*)",
"replacement": "$1"
}
],
"text": "http://www.elastic.co"
}
创建一个自定义的分词器
创建一个自定义的分词器,通过settings进行设置,取名为my_custom_analyzer 指定他拥有一个特定的分词过滤方式,指定他有一个特点的分词器,指定他有特定的过滤器 自定义的分词器中指定他使用ik分词器,自定义的特性过滤器中取出了标签,自定义的过滤器中,过滤了英语中的无效词
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "custom",
"char_filter":"my_charfilter",
"tokenizer": "my_tokenizer",
"filter": [
"lowercase",
"my_stop"
]
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ik_smart"
}
},
"char_filter": {
"my_charfilter":{
"type": "html_strip"
}
},
"filter": {
"my_stop": {
"type": "stop",
"stopwords": "_english"
}
}
}
}
}
测试自定义的分词器 他去除了标签和 英语中的辅助词。并且按照ik分词器的细粒度分词,进行了分词。
POST my_index/_analyze
{
"analyzer": "my_custom_analyzer",
"text": "我在北京天安门看<a>天空</a> is very happy ,and you?"
}