我有一个映射字段:
{
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
该文档之一在上述字段中的值为“abcdef”。
搜索“def”时应使用哪种ES查询来匹配该文档?
我尝试过匹配,前缀查询。
ES版本:5.1.1
最佳答案
您可以创建一个使用n-gram analyzer的自定义分析器,并在需要子字符串搜索的字段上使用它,通配符搜索的成本很高,我想这就是您不想使用此重复SO中提到的原因的原因题。
我的索引设置和映射根据您的要求。
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 3,
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"properties": {
"foo": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"analyzer": "my_analyzer"
}
}
}
}
我创建了一个名为
foo
的字段,并在该字段上使用了我的自定义n-gram分析器,因此对于abcdef
值,它将创建以下标记。{
"tokens": [
{
"token": "abc",
"start_offset": 0,
"end_offset": 3,
"type": "word",
"position": 0
},
{
"token": "bcd",
"start_offset": 1,
"end_offset": 4,
"type": "word",
"position": 1
},
{
"token": "cde",
"start_offset": 2,
"end_offset": 5,
"type": "word",
"position": 2
},
{
"token": "def",
"start_offset": 3,
"end_offset": 6,
"type": "word",
"position": 3
}
]
}
然后在下面的搜索查询中返回包含
abcdef
的文档。{
"query": {
"term" : {
"foo" : "def"
}
}
}
编辑:如果您要检查所有API调用,我的邮递员收藏link。 ,只需将其替换为您的端口和索引。