3.ElasticSearch~进阶

一点零一的n次方 / 2023-07-18 / 原文

ES支持两种基本方式检索:

一个是通过使用 REST request URI来发送搜索参数(uri + 检索参数)

GET bank/_search?q=*&sort=account_number:asc

另一个是通过 REST request body 来发送他们 (uri + 请求体)

GET bank/_search
{
"query":{
"match_all":{}
},
"sort":[
{
"balance":{
"order":"desc"
}
}
],
"from":0,
"size":5,
"_source":["balance","firstname"]
}

1、match_all :查询所有

GET bank/_search
{
    "query":{
        "match_all":{}
     },
     "sort":[
        {
          "balance":"asc"
        } 
     ]
}

2、match查询
全文检索会按照评分进行排序,
首选需要对要查询的字符串进行分词,然后根据分词的结果进行匹配
最后根据得分进行排序

GET bank/_search
{
  "query": {
    "match": {
      "address": "kings"
    }
  }
}

3、match_phrase 【短语匹配】
将需要匹配的值,当成一个整体单词(不分词)进行检索

GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}

4、multi_match 【多字段匹配】
也是分词查询

// 查询 adress或者city中包含 mill的数据
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": ["address","city"]
    }
  }
}

5 bool查询

GET bank/_search
{
  "query": {
    "bool": {
      "must":[
      {
        "match": {
          "gender": "F"
        }
      },
      {
        "match": {
          "address": "Mill"
        }
      }
    ]
    }
  }
}