## 模糊匹配
<br>
> 通过match关键词模糊匹配条件内容,如果字段是数字就会变成精确查询
```
GET /user/_search
{
"query": {
"match": {
"name": "李四"
}
}
}
```
会根据分词模糊匹配进行全文检索,全文检索会按照得分进行排序
```
{
"took" : 559,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.89712,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.89712,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 0.6931471,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
```
> 通过 multi_match 关键词在多个字段中检索
```
GET /user/_search
{
"query": {
"multi_match": {
"query": "李四",
"fields": ["name","address"]
}
}
}
```
> prefix 前缀匹配
```
GET /user/_search
{
"query": {
"prefix": {
"name": "李"
}
}
}
```
会查询以 ``李`` 开头的数据
```
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
```
> regexp 通过正则表达来匹配数据
正则表达式语法中使用了许多符号和运算符来表示通配符和字符范围
* 句号 “.” 用于代表任何字符。
* 用括号括起来的一系列字符,例如 [a-z],是一个字符类。 字符类表示字符范围; 在此示例中,它充当任何字母的替代。
* 加号 “+” 用于表示重复的字符; 例如,“Mississippi” 中的 “pp”。
```
GET /user/_search
{
"query": {
"regexp": {
"name": "李.*"
}
}
}
```
查询结果
```
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "docs",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "李四",
"sex" : 1,
"age" : 25,
"address" : "河北省"
}
},
{
"_index" : "user",
"_type" : "docs",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "李华",
"sex" : 1,
"age" : 26,
"address" : "河北省"
}
}
]
}
}
```
> wildcard 通配符匹配 (*表示任意字符串,?表示任意一个字符)
```
GET /user/_search
{
"query": {
"wildcard": {
"name": "李*"
}
}
}
```