了解 ELK 分析器

Understanding ELK Analyzer

我是 ELK 5.1.1 堆栈的新手,我有几个问题只是为了我的理解。 我基本上使用标准分析器/过滤器设置了这个堆栈,一切都很好。 我的数据源是我使用 Logstash 编制索引的 MySQL 后端。 我想处理包含重音符号的查询,希望 asciifolding 标记过滤器可以帮助实现这一点。

首先我了解了如何创建自定义分析器并另存为模板。 现在,当我查询此 url http://localhost:9200/_template?pretty 时,我有 2 个模板:名为 logstash 的 logstash 默认模板和我的自定义模板,其设置为:

"custom_template" : {
    "order" : 1,
    "template" : "doo*",
    "settings" : {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "myCustomAnalyzer" : {
                        "filter" : [
                            "standard",
                            "lowercase",
                            "asciifolding"
                        ],
                        "tokenizer" : "standard"
                   }
               }
         },
        "refresh_interval" : "5s"
     }
    },
    "mappings" : { },
    "aliases" : { }
}

搜索关键字 Yaoundé returns 70 次点击,但当我搜索 Yaounde 时,我一直没有点击。 下面是我对第二种情况的查询

{
"query": {
    "query_string": {
        "query": "yaounde",
        "fields": [
            "title"
        ]
    }
},
"from": 0,
"size": 10
}

请有人帮我猜猜这里做错了什么? 还知道我的数据在索引过程中由 Logstash 分析,我真的必须指定分析器 myCustomAnalyzer 应该在研究期间根据第二个查询应用吗?

{
     "query": {
    "query_string": {
        "query": "yaounde",
        "fields": [
            "title"
        ],
        "analyzer": "myCustomAnalyzer"
    }
},
"from": 0,
"size": 10
}

这是我的 logstash 配置文件的输出部分示例

output {
stdout { codec => json_lines }
if [type] == "announces" {
    elasticsearch {  
        hosts => "localhost:9200"
        document_id => "%{job_id}"
        index => "dooone"
    document_type => "%{type}"
    }
} else {
    elasticsearch {  
        hosts => "localhost:9200"
    document_id => "%{uid}"
    index => "dootwo"
    document_type => "%{type}"
    }
}
}

谢谢

能否显示文档的映射?

(获取/my_index/my_doc/_mapping)

您在查询中作为参数提供的分析器仅适用于搜索时,不适用于索引时。因此,如果您没有在映射中设置此分析器,该字符串仍使用 "default" 分析器进行索引,因此它不会匹配您的结果。

您在搜索时提供的分析器将应用于您的查询字符串,但随后它会查看索引数据,索引为 "Yaoundé",而不是 "yaounde"。

一个好的起点是 elasticsearch 的索引模板文档: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

适用于标题字段的场景示例:

"custom_template" : {
    "order" : 1,
    "template" : "doo*",
    "settings" : {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "myCustomAnalyzer" : {
                        "filter" : [
                            "standard",
                            "lowercase",
                            "asciifolding"
                        ],
                        "tokenizer" : "standard"
                   }
               }
         },
        "refresh_interval" : "5s"
     }
    },
    "mappings" : {
      "your_type": {
        "properties": {
          "title": {
            "type": "text",
            "analyzer": "myCustomAnalyzer"
          }
        }
      }
    },
    "aliases" : { }
}

另一种方法是更改​​动态映射。你可以在这里找到一个很好的字符串示例。 https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html#match-mapping-type