我在进行部分搜索(半字节)时无法获得mac地址搜索来返回正确的结果。我的意思是,如果我查找确切的mac地址,则会得到结果,但是如果尝试搜索“00:19:9”之类的部分搜索,则在完成八位位组之前我什么也不会得到。
谁能指出我应该使用哪种映射来为其编制索引,或者应该使用哪种搜索查询?
curl -XDELETE http://localhost:9200/ap-test
curl -XPUT http://localhost:9200/ap-test
curl -XPUT http://localhost:9200/ap-test/devices/1 -d '
{
"user" : "James Earl",
"macaddr" : "00:19:92:00:71:80"
}'
curl -XPUT http://localhost:9200/ap-test/devices/2 -d '
{
"user" : "Earl",
"macaddr" : "00:19:92:00:71:82"
}'
curl -XPUT http://localhost:9200/ap-test/devices/3 -d '
{
"user" : "James Edward",
"macaddr" : "11:19:92:00:71:80"
}'
curl -XPOST 'http://localhost:9200/ap-test/_refresh'
curl -XGET http://localhost:9200/ap-test/devices/_mapping?pretty
当我找到完全匹配项时,我会正确找到它们。
curl -XPOST http://localhost:9200/ap-test/devices/_search -d '
{
"query" : {
"query_string" : {
"query":"\"00\\:19\\:92\\:00\\:71\\:80\""
}
}
}'
# RETURNS:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.57534903,
"hits": [
{
"_index": "ap-test",
"_type": "devices",
"_id": "1",
"_score": 0.57534903,
"_source": {
"user": "James Earl",
"macaddr": "00:19:92:00:71:80"
}
}
]
}
}
但是,我需要能够匹配部分Mac地址搜索,如下所示:
curl -XPOST http://localhost:9200/ap-test/devices/_search -d '
{
"query" : {
"query_string" : {
"query":"\"00\\:19\\:9\""
}
}
}'
# RETURNS 0 instead of returning 2 of them
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
所以,我应该使用什么映射?是否有更好的查询字符串来完成此操作?顺便说一句,使用'query_string'和'text'有什么区别?
最佳答案
似乎您根本没有定义映射,这意味着Elasticsearch将猜测您的数据类型并使用标准映射。
对于字段macaddr,这将被识别为字符串,并且将使用标准字符串分析器。该分析器将在空格和标点符号上分解字符串,为您提供由数字对组成的标记。例如"00:19:92:00:71:80"
将标记为00
19
92
00
71
80
。当您搜索时,将发生相同的标记化。
您想要定义一个将"00:19:92:00:71:80"
转换为 token 00
00:
00:1
00:19
等的分析器...
试试这个:
curl -XPUT http://localhost:9200/ap-test -d '
{
"settings" : {
"analysis" : {
"analyzer" : {
"my_edge_ngram_analyzer" : {
"tokenizer" : "my_edge_ngram_tokenizer"
}
},
"tokenizer" : {
"my_edge_ngram_tokenizer" : {
"type" : "edgeNGram",
"min_gram" : "2",
"max_gram" : "17"
}
}
}
}
}'
curl -XPUT http://localhost:9200/ap-test/devices/_mapping -d '
{
"devices": {
"properties" {
"user": {
"type": "string"
},
"macaddr": {
"type": "string",
"index_analyzer" : "my_edge_ngram_analyzer",
"search_analyzer": "keyword"
}
}
}
}'
像以前一样放置文档,然后使用针对字段的查询搜索:
curl -XPOST http://localhost:9200/ap-test/devices/_search -d '
{
"query" : {
"query_string" : {
"query":"\"00\\:19\\:92\\:00\\:71\\:80\"",
"fields": ["macaddr", "user"]
}
}
}'
至于最后一个问题,不建议使用
text
查询。祝好运!
关于search - Elasticsearch Mac地址搜索/映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17839149/