## 模糊匹配
<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": "李*"
}
}
}
```