ElasticSearch索引分析器使用

配置分析器

分析器时三个顺序执行的组件的组合。包括字符过滤器,分词器,标记过滤器。
示例:

PUT /spanish_docs
{
	   "settings": {
			"analysis": {
			"analyzer": {
			  "es_std": {
			  "type": "standard",
			  "stopwords": "_spanish_"
			}
		}
		}
	}
}

自定义分析器

利用过滤器,分词器,过滤器配置一个自定义的分析器。
包括如下步骤:

  1. 用 html_strip 字符过滤器去除所有的 HTML 标签
  2. 将 & 替换成 and ,使用一个自定义的 mapping 字符过滤器
 "char_filter": {
	"&_to_and": {
	"type": "mapping",
	"mappings": [ "&=> and "]
	}
 }
  1. 使用 standard 分词器分割单词
  2. 使用 lowercase 标记过滤器将词转为小写
  3. 用 stop 标记过滤器去除一些自定义停用词。
"filter": {
	"my_stopwords": {
		"type": "stop",
		"stopwords": [ "the", "a" ]
	}
}

完整的例子:

{
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "char_filter": [
                        "html_strip",
                        "&_to_and"
                    ],
                    "filter": [
                        "lowercase",
                        "my_stopwords"
                    ],
                    "tokenizer": "standard",
                    "type": "custom"
                }
            },
            "char_filter": {
                "&_to_and": {
                    "mappings": [
                        "&=> and "
                    ],
                    "type": "mapping"
                }
            },
            "filter": {
                "my_stopwords": {
                    "stopwords": [
                        "the",
                        "a"
                    ],
                    "type": "stop"
                }
            }
        }
    }
}

创建之后给索引加上,然后在检索时就可使用。

PUT /my_index/_mapping/my_type
{
    "properties": {
        "title": {
            "analyzer": "my_analyzer",
            "type": "string"
        }
    }
}

检索:

GET /my_index/_analyze?analyzer=my_analyzer
发布了79 篇原创文章 · 获赞 3 · 访问量 5253

猜你喜欢

转载自blog.csdn.net/SW_LCC/article/details/103059121