您可以使用should 查询,按字词分割您的搜索词组,并根据您的pourcentage 设置minimum_should_match
查询示例
{
"query": {
"bool": {
"should": [
{
"term": {
"my_field": "hello"
}
},
{
"term": {
"my_field": "world"
}
},
{
"term": {
"my_field": "i'm"
}
},
{
"term": {
"my_field": "alive"
}
}
],
"minimum_should_match": 2
}
}
}
会找到hello world、hello alive 等...
要拆分文本,您应该使用 _analysis of your index
分析和拆分术语
POST myindex/_analyze
{
"field": "my_field",
"text": "hello world i'm alive"
}
如果您使用自定义analyzer,那么它会为您提供这样的结果来填充您的查询,并将术语分析器与查询匹配。
{
"tokens" : [
{
"token" : "hello",
"start_offset" : 0,
"end_offset" : 5,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "world",
"start_offset" : 6,
"end_offset" : 11,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "i'm",
"start_offset" : 12,
"end_offset" : 15,
"type" : "<ALPHANUM>",
"position" : 2
},
{
"token" : "alive",
"start_offset" : 16,
"end_offset" : 21,
"type" : "<ALPHANUM>",
"position" : 3
}
]
}