Elastic-Language analyzer

时间:2021-09-03 21:12:58   收藏:0   阅读:21

Language analyzer

支持如下语种:

重新实现语言分词器

这个内置语言分词器可以作为自定义分词器去实现,为了定制它们的行为。

NOTE

如果你不打算排除被提取词干的单词(相当于上面的stem_exclusion参数),那么你应该从自定义分析器配置中删除keyword_marker标记过滤器。

词干提取排除

例如一个句子World Health Organization,这个跟分词替换为organ health 的结果。这是因为organ和organization有相同的词根:organ。通常这个不是什么大问题,但是在一些特殊的文档中就会导致有歧义的结果,所以我们希望防止单词organization和organizations被缩减为词干。

english analyzer


PUT /my_index

{

  "mappings": {

?    "blog": {

?      "properties": {

?        "title": {

?          "type":     "string",

?          "analyzer": "english"   // [1]

?        }

?      }

?    }

  }

}




GET /my_index/_analyze?field=title 

I‘m not happy about the foxes

这个分词为:


i‘m,happi,about,fox

我们无法分辨源文档中是包含单数 fox 还是复数 foxes ;单词 not 因为是停用词所以被移除了, 所以我们无法分辨源文档中是happy about foxes还是not happy about foxes,虽然通过使用 english (英语)分析器,使得匹配规则更加宽松,我们也因此提高了召回率,但却降低了精准匹配文档的能力。

为了获得两方面的优势,我们可以使用multifields(多字段)对 title 字段建立两次索引: 一次使用 english(英语)分析器,另一次使用 standard(标准)分析器:


PUT /my_index

{

  "mappings": {

?    "blog": {

?      "properties": {

?        "title": { 	// [1]

?          "type": "string",

?          "fields": {

?            "english": { 	// [2]

?              "type":     "string",

?              "analyzer": "english"

?            }

?          }

?        }

?      }

?    }

  }

}


替换为该字段映射后,我们可以索引一些测试文档来展示怎么在搜索时使用两个字段。


PUT /my_index/blog/1

{ "title": "I‘m happy for this fox" }



PUT /my_index/blog/2

{ "title": "I‘m not happy about my fox problem" }



GET /_search

{

  "query": {

?    "multi_match": {

?      "type":     "most_fields", 	// [1]

?      "query":    "not happy foxes",

?      "fields": [ "title", "title.english" ]

?    }

  }

}

czech analyzer

这个捷克语分词器可以被实现,作为一个自定义的分词器。


PUT /czech_example

{

  "settings": {

?    "analysis": {

?      "filter": {

?        "czech_stop": {

?          "type":       "stop",

?          "stopwords":  "_czech_" 	// [1]

?        },

?        "czech_keywords": {

?          "type":       "keyword_marker",

?          "keywords":   ["p?íklad"] 	// [2]

?        },

?        "czech_stemmer": {

?          "type":       "stemmer",

?          "language":   "czech"

?        }

?      },

?      "analyzer": {

?        "rebuilt_czech": {

?          "tokenizer":  "standard",

?          "filter": [

?            "lowercase",

?            "czech_stop",

?            "czech_keywords",

?            "czech_stemmer"

?          ]

?        }

?      }

?    }

  }

}

注释

例子


GET /_analyze

{

  "tokenizer": "whitespace",

  "filter": [ "stemmer" ],

  "text": "fox running and jumping"

}


这个请求会产生如下的一些词组,比如running会被提取词干为run 和 jumping被提取为jump。


[ fox, run, and, jump ]


若不想被提取词干,则需要对单词进行标记,即keyword_marker参数过滤


GET /_analyze

{

  "tokenizer": "whitespace",

  "filter": [

?    {

?      "type": "keyword_marker",

?      "keywords": [ "jumping" ]

?    },

?    "stemmer"

  ],

  "text": "fox running and jumping"

}


如下则为提取结果:


[ fox, run, and, jumping ]


jumping则没有被提取词干。

原文:https://www.cnblogs.com/Choleen/p/15221436.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!