内容简介:使用 Elasticsearch 搭建搜索引擎的过程中,免不了会反复测试 Analyzer 的效果,如果每次都建立一个索引,配置索引的 Analyzer,插入 Document,无疑效率会很低。ES 提供了Response由于 Analyzer 必须指定一个 Tokenizer,因此可以使用
使用 Elasticsearch 搭建搜索引擎的过程中,免不了会反复测试 Analyzer 的效果,如果每次都建立一个索引,配置索引的 Analyzer,插入 Document,无疑效率会很低。ES 提供了 _analyze
接口,可以 无需创建索引快速测试 Analyzer
,推荐搭配 Kibana 中的 Dev Tools 一同使用。
POST _analyze
{
"tokenizer": "icu_tokenizer",
"text": "你好世界"
}
Response
{
"tokens" : [
{
"token" : "你好",
"start_offset" : 0,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "世界",
"start_offset" : 2,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 1
}
]
}
测试 Character Filter
由于 Analyzer 必须指定一个 Tokenizer,因此可以使用 Keyword 这个特殊的 Tokenizer, 即不做任何分词,从而可以看到 Character Filter 的效果。
POST _analyze
{
"char_filter": [ "html_strip" ],
"tokenizer": "keyword",
"text": "<p>Hello <b>World</b>!</p>"
}
Response
{
"tokens" : [
{
"token" : """
Hello World!
""",
"start_offset" : 0,
"end_offset" : 26,
"type" : "word",
"position" : 0
}
]
}
测试 Token filter
POST _analyze
{
"tokenizer": "icu_tokenizer",
"filter": [{
"type": "stop", "stopwords": ["am"]
}],
"text": "I am ironman"
}
Response
{
"tokens" : [
{
"token" : "I",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "ironman",
"start_offset" : 5,
"end_offset" : 12,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
测试已经创建的索引
而对于已经创建的索引,可以通过 ${index}/_analyze
接口来调用某个已经创建好的 Analyzer,或者预览某个 Field 对于文本的分析结果。 如创建如下索引
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"char_filter": [
"html_strip"
],
"tokenizer": "icu_tokenizer",
"filter": [
"my_stop_filter"
]
}
},
"filter": {
"my_stop_filter": {
"type": "stop",
"stopwords": [
"am"
]
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"title": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
调用这个索引中已经创建的 Analyzer my_analyzer
POST my_index/_analyze
{
"analyzer": "my_analyzer",
"text": "<p>I am <b>Ironman</b>!</p>"
}
或者预览 title
字段的分析结果
POST my_index/_analyze
{
"field": "title",
"text": "<p>I am <b>Ironman</b>!</p>"
}
而对于已经索引的数据,可以通过
GET /${index}/${type}/${id}/_termvectors?fields=${fields_name}
来查看实际存储的数据, 如
POST _bulk
{ "index": { "_index": "my_index", "_type": "my_type", "_id": 1} }
{ "title": "<p>I am <b>Ironman</b>!</p>" }
GET my_index/my_type/1/_termvectors?fields=title
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_version": 1,
"found": true,
"took": 2,
"term_vectors": {
"title": {
"field_statistics": {
"sum_doc_freq": 2,
"doc_count": 1,
"sum_ttf": 2
},
"terms": {
"I": {
"term_freq": 1,
"tokens": [
{
"position": 0,
"start_offset": 3,
"end_offset": 4
}
]
},
"Ironman": {
"term_freq": 1,
"tokens": [
{
"position": 2,
"start_offset": 11,
"end_offset": 22
}
]
}
}
}
}
}
以上所述就是小编给大家介绍的《如何快速测试 Elasticsearch 的 Analyzer》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Hit Refresh
Satya Nadella、Greg Shaw / HarperBusiness / 2017-9-26 / USD 20.37
Hit Refresh is about individual change, about the transformation happening inside of Microsoft and the technology that will soon impact all of our lives—the arrival of the most exciting and disruptive......一起来看看 《Hit Refresh》 这本书的介绍吧!