如果您没有指定分析器,则默认使用standard。它将进行基于语法的标记化。因此,您对“嗨,我的名字 isxyz abc”这句话的用语。将类似于 [hi, my, name, isxyz, abc] 和 match_phrase 正在寻找彼此相邻的术语 [xyz, abc](除非您指定 slop)。
您可以使用不同的分析器或修改您的查询。如果您使用match 查询,它将匹配术语“abc”。如果您希望短语匹配,则需要使用不同的分析器。 NGrams 应该适合你。
这是一个例子:
PUT test_index
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 3,
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"content": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
PUT test_index/_doc/1
{
"content": "hi my name isxyz abc."
}
PUT test_index/_doc/2
{
"content": "hey wassupxyz abc. how is life"
}
POST test_index/_doc/_search
{
"query": {
"match_phrase": {
"content": "xyz abc"
}
}
}
这会导致找到两个文档。
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5753642,
"hits": [
{
"_index": "test_index",
"_type": "_doc",
"_id": "2",
"_score": 0.5753642,
"_source": {
"content": "hey wassupxyz abc. how is life"
}
},
{
"_index": "test_index",
"_type": "_doc",
"_id": "1",
"_score": 0.5753642,
"_source": {
"content": "hi my name isxyz abc."
}
}
]
}
}
编辑:
如果您要进行wildcard 查询,可以使用standard 分析器。您在 cmets 中指定的用例将像这样添加:
PUT test_index/_doc/3
{
"content": "RegionLasit Pant0Q00B000001KBQ1SAO00"
}
你可以用wildcard查询:
POST test_index/_doc/_search
{
"query": {
"wildcard": {
"content.keyword": {
"value": "*Lasit Pant*"
}
}
}
}
基本上你是在没有nGram 分析器的情况下进行子字符串搜索。然后,您的查询短语将只是 "*<my search terms>*"。我仍然建议您查看nGrams。