多字段特性及Mapping中配置自定义Analyzer

本文通过示例展示了如何在Elasticsearch中配置和使用自定义Analyzer,包括char_filter的映射替换、正则表达式处理、停用词过滤等,详细解析了Analyzer在文本分析过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POST _analyze
{
  "tokenizer": "keyword",
  "char_filter": ["html_strip"],
  "text": "<b>hello world</b>"
}

//使用char filter进行替换
POST _analyze
{
  "tokenizer": "standard",
  "char_filter": [{
    "type":"mapping",
    "mappings":["- => _"]
  }],
  "text": "123-456,I-test! test-990 650-555-1234"
}

//使用char filter 替换表情符号
POST _analyze
{
  "tokenizer": "standard",
  "char_filter": [
    {
      "type":"mapping",
      "mappings":[":) => happy",":( => sad"]
    }
  ],
  "text": ["I am felling :)","Felling :( today"]
}

//正则表达式
GET _analyze
{
  "tokenizer": "standard",
  "char_filter": [
    {
      "type":"pattern_replace",
      "pattern":"http://(.*)",
      "replacement":"$1"
    }],
    "text": "http://www.elastic.co"
}

POST _analyze
{
  "tokenizer": "path_hierarchy",
  "text": "/user/ymruan/a/b/c/d/e"
}

//whitespace与stop
GET _analyze
{
  "tokenizer":"whitespace",
  "filter":["stop"],
  "text":["The rain in Spain falls mainly on the plain."]
}

//remove 加入lowercase后,The 被当成stopword删除
GET _analyze
{
  "tokenizer": "whitespace",
  "filter": ["lowercase","stop"],
  "text": ["The girls in China are playing this game!"]
}

DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer":{
          "type":"custom",
          "char_filter":[
            "emoticons"],
            "tokenizer":"punctuation",
            "filter":[
              "lowercase","english_stop"
              ]
        }
      },
      "tokenizer": {
        "punctuation":{
          "type":"pattern",
          "pattern":"[ .,!?]"
        }
      },
      "char_filter": {
        "emoticons":{
          "type":"mapping",
          "mappings":[
            ":) => _happy_",
            ":( => _sad_"
            ]
        }
      },
      "filter": {
        "english_stop":{
          "type":"stop",
          "stopwords":"_english_"
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "I'm a :) person,and you?"
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值