Elasticsearch: 权威指南 » 处理人类语言 » 开始处理各种语言 分词学习!****

自定义分词器测试!

创建一个索引,并自定义分词器

自定义一个分词器,名字为my_custom_analyzer 指定他为自定义分词器(type=custom) 指定他的char_filter 为my_charfilter(自定义产生,会对查询词进行预处理),指定他的分词方式为my_tokenizer(自定义的分词方式),指定一个结果过滤器(会去除调指定不需要的词项)
PUT test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom",
          "char_filter": "my_charfilter",
          "tokenizer": "my_tokenizer",
          "filter": [
            "my_stop",
            "lowercase"
          ]
        }
      },
      "char_filter": {
        "my_charfilter": {
          "type": "html_strip"
        }
      },
      "tokenizer": {
        "my_tokenizer":{
          "type":"whitespace"
        }
      },
      "filter": {
        "my_stop":{
          "type":"stop",
          "stopwords": "_english"
        }
      }
    }
  }
}
GET test_index
PUT test_index/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "analyzer": "ik_smart"
    },
    "address": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "address2": {
      "type": "text",
      "analyzer": "standard"
    },
    "address3": {
      "type": "text",
      "analyzer": "my_custom_analyzer"
    }
  }
}

添加测试数据

PUT test_index/_doc/1
{
  "name": "<h1>我默认</h1> 的 分词方式 是使用空格 进行分词",
  "address": "<h1>我默认</h1> 的 分词方式 是使用空格 进行分词",
  "address2": "<h1>我默认</h1> 的 分词方式 是使用空格 进行分词",
  "address3": "<h1>我默认</h1> 的 分词方式 是使用空格 进行分词"
}

GET test_index/_doc/1

查看分词结果

name 字段使用ik smart 进行分词 会产生约12条分词结果

GET test_index/_analyze
{
  "field": "name", 
  "text": "<h1>我默认</h1> 的 分词方式 是使用空格 进行分词"
}

address 使用ik_max_word进行分词 会产生约16条分词结果

GET test_index/_analyze
{
  "field": "address", 
  "text": "<h1>我默认</h1> 的 分词方式 是使用空格 进行分词"
}

address2 使用standard (es原本默认的分词方式) 一个字一个分词结果

GET test_index/_analyze
{
  "field": "address2", 
  "text": "<H1>我默认</H1> 的 分词方式 是使用空格 进行分词 a the"
}

address3 使用了自定义分词器中的分词方式,成功的去掉了标签,并且以空格进行分词

GET test_index/_analyze
{
  "field": "address3", 
  "text": "<H1>我默认</H1> 的 分词方式 是使用空格 进行分词 this is a cat"
}

通过fields 方式对指定字段进行多种方式的分词

DELETE my_index
PUT /my_index
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_smart", 
        "fields": {
          "max": {
            "type": "text",
            "analyzer": "ik_max_word"
          }
        }
      }
    }
  }
}

测试title 不同情况下的分词

GET /my_index/_analyze
{
  "field": "title",
  "text": "雷锋塔看我想要看的风景"
}
GET /my_index/_analyze
{
  "field": "title.max",
  "text": "雷锋塔看我想要看的风景"
}
PUT /my_index/_doc/1
{
  "title": "雷锋塔看我想要看的风景"
}

对于ik_smart 不能查出数据

GET my_index/_search
{
  "query": {
    "multi_match": {
      "query": "塔",
      "fields": ["title"]
    }
  }
}

对于ik_max_word 可以查出想要的数据

GET my_index/_search
{
  "query": {
    "multi_match": {
      "query": "塔",
      "fields": ["title.max"]
    }
  }
}

因为前面使用了fields 多字段对title进行分词 所以使用multi_match查询塔的时候,可以实现一份数据,多种不同的分词方式进行查询

GET my_index/_search
{
  "query": {
    "multi_match": {
      "query": "塔",
      "fields": ["title","title.max"]
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值