诚然,我不太了解ES的分析部分。这是索引布局:
{ "mappings": { "event": { "properties": { "ipaddress": { "type": "string" }, "hostname": { "type": "string", "analyzer": "my_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } } } } }, "settings": { "analysis": { "filter": { "my_filter": { "type": "word_delimiter", "preserve_original": true } }, "analyzer": { "my_analyzer": { "type": "custom", "tokenizer": "whitespace", "filter": ["lowercase", "my_filter"] } } } } }
您可以看到我尝试对主机名字段使用自定义分析器。当我使用此查询查找名为“ WIN_1”的主机时,这种工作方式如下:
{ "query": { "match": { "hostname": "WIN_1" } } }
问题是它还会返回其中包含1的任何主机名。使用_analyze端点,我可以看到数字也被标记。
_analyze
{ "tokens": [ { "token": "win_1", "start_offset": 0, "end_offset": 5, "type": "word", "position": 1 }, { "token": "win", "start_offset": 0, "end_offset": 3, "type": "word", "position": 1 }, { "token": "1", "start_offset": 4, "end_offset": 5, "type": "word", "position": 2 } ] }
我想做的就是搜索WIN,并找回名称中带有WIN的所有主机。但是我还需要能够搜索WIN_1并找回确切的主机或名称中带有WIN_1的任何主机。以下是一些测试数据。
{ "ipaddress": "192.168.1.253", "hostname": "WIN_8_ENT_1" } { "ipaddress": "10.0.0.1", "hostname": "server1" } { "ipaddress": "172.20.10.36", "hostname": "ServA-1" }
希望有人可以指出正确的方向。可能我的简单查询也不是正确的方法。我已经介绍了ES文档,但是这些示例的确不好用。
这是分析器和我最终遇到的查询:
{ "mappings": { "event": { "properties": { "ipaddress": { "type": "string" }, "hostname": { "type": "string", "analyzer": "hostname_analyzer", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } } } } }, "settings": { "analysis": { "filter": { "hostname_filter": { "type": "pattern_capture", "preserve_original": 0, "patterns": [ "(\\p{Ll}{3,})" ] } }, "analyzer": { "hostname_analyzer": { "type": "custom", "tokenizer": "whitespace", "filter": [ "lowercase", "hostname_filter" ] } } } } }
查询:查找以以下内容开头的主机名:
{ "query": { "prefix": { "hostname.raw": "WIN_8" } } }
查找包含以下内容的主机名:
{ "query": { "multi_match": { "fields": [ "hostname", "hostname.raw" ], "query": "WIN" } } }
感谢Dan帮助我朝正确的方向发展。