Elasticsearch query example

Serch All:

GET /_search    //所有索引,所有type下的所有数据都搜索出来
{
  "query": {
    "match_all": {}
  }
}
GET /test_index/_search    //指定一个index,搜索其下所有type的数据
{
  "query": {
    "match_all": {}
  }
}
GET /test_index,test_index2/_search    //同时搜索两个index下的数据
{
  "query": {
    "match_all": {}
  }
}
GET /*1,*2/_search    //按照通配符去匹配多个索引
{
  "query": {
    "match_all": {}
  }
}
GET /test_index/test_type/_search    //搜索一个index下指定的type的数据
{
  "query": {
    "match_all": {}
  }
}
GET /test_index/test_type,test_type2/_search    //可以搜索一个index下多个type的数据
{
  "query": {
    "match_all": {}
  }
}
GET /test_index,test_index2/test_type,test_type2/_search    //搜索多个index下的多个type的数据
{
  "query": {
    "match_all": {}
  }
}
GET /_all/test_type,test_type2/_search    //可以代表搜索所有index下的指定type的数据
{
  "query": {
    "match_all": {}
  }
}

Match:

GET /_search
{
  "query": {
    "match": {
      "title": "elasticsearch"
    }
  }
}

multi match:

GET /_search
{
  "query": {
    "multi_match": {
      "query": "elasticsearch",
      "fields": ["title","content"]
    }
  }
}

 

range query:

GET /company/employee/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 30
      }
    }
  }
}

 

term query:

//term 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些 not_analyzed 的字符串
//term 查询对于输入的文本不 分析 ,所以它将给定的值进行精确查询
GET /_search
{
  "query": {
    "term": {
      "title":"test hello"
    }
  }
}

 

terms query:

//terms 查询和 term 查询一样,但它允许你指定多值进行匹配。
//如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件:
{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}

 

定制排序:

查询的默认情况是按照_score排序的,然而某些情况下,可能没有有用的_score,比如说filter或constant_score

GET _search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "author_id": 110
        }
      }
    }
  }
}
GET _search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "author_id": 110
        }
      }
    }
  }
}

  

定制排序规则

 

GET /company/employee/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range":{
          "age":{
            "gte":30
          }
        }
      }
    }
  },
  "sort": [
    {
      "join_date": {
        "order": "asc"
      }
    }
  ]
}

  

  

转自: https://www.jianshu.com/p/5ef2312bf12b  

  

  

  

  

转载于:https://www.cnblogs.com/allenhaozi/p/8733785.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值