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"
        }
      }
    ]
    }
  }
}

GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "Fillmore"
}
},
{
"match": {
"age": "36"
}
}
],
"must_not": [
{
"multi_match": {
"query": "Fillmore",
"fields": ["city"]
}
}
]
}
}
}

6、filter 结果过滤
只用于对数据进行过滤,不会计算相关性得分

GET bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  }
}

7、term 查询
和match一样。匹配某个属性的值。全文检索用match,其他非text字段匹配用term

GET /bank/_search
{
  "query": {
    "term": {
      "balance": {
        "value": "32838"
      }
    }
  }
}

精确匹配:查询address等于132 Gunnison的文档

GET bank/_search
{
  "query": {
    "match": {
      "address.keyword": "132 Gunnison"
    }
  }
}