ES基础语法学习

说明:当前所有示例均使用Kibana执行,如果未安装,可以选择使用Postman执行测试

#数据写入
PUT /kevin/doc/4
{
  "name":"kevin",
  "age":30
}
PUT /kevin/doc/8
{
  "name":"kevin",
  "age":26,
  "id":1
}
PUT /kevin/doc/10
{
  "name":"kevin",
  "age":25,
  "id":2
}
PUT /kevin/doc/2
{
  "name":"kevin2",
  "age":22,
  "id":3
}
PUT /kevin/doc/3
{
  "name":"kevin3",
  "age":23,
  "id":4
}

PUT /ding/doc/1
{
  "name":"ding",
  "age":18,
  "id":5
}


#查询所有索引
GET /_search
GET /_all/_search?size=0
GET kevin/_search
{
  "query": {
    "match_all": {}
  }
}

#查询指定索引
GET /kevin/_search
#查询多个指定索引
GET /ding,kevin/_search?q=name:kevin

#简单查询-size为0
GET /kevin/_search
{
    "from":1,
    "size":0
}

#简单条件查询(允许单个值)
GET /kevin/_search
{
    "query" : {
        "term" : {
          "name":"kevin"
        }
    },
    "from":1,
    "size":10
}

#简单条件查询(允许多个值)
GET /kevin/_search
{
    "query" : {
        "terms" : {
          "age":[23,22]
        }
    }
}

#range简单使用
#可使用条件:from, lt, lte, gt, gte,boost,format,time_zone
GET /kevin/_search
{
    "query" : {
        "range" : {
          "age":{"gte": 22, "lte": 23}
        }
    },
    "from": 0, "size": 20
}


#bool简单使用:用于符合条件的联合使用
GET /kevin/_search
{
  "query" : {
    "bool" : {
        "must": [
          {
            "term" : {
              "name":"kevin"
            }
          }
        ],
        "must_not": [
          {
            "term" : {
              "_id":"4"
            }
          }
        ],
        "filter": {
          "range": {
              "age": {"from":26}
            }
        }
    }
  },
  "from":0, "size": 2
}

#sort简单使用
GET /kevin/_search
{
  "sort": [
    {
      "age": {
        "order": "asc"
      },
      "id": {
        "order": "desc"
      }
    }
  ]
}


#排序sort_mode_option支持:
#min,max,sum,avg,median,
#1.准备数据
PUT /price/doc/1
{
  "name":"shop1",
  "price":[3,3,9]
}
PUT /price/doc/2
{
  "name":"shop1",
  "price":[5,5,7]
}
PUT /price/doc/3
{
  "name":"shop1",
  "price":[6,6,6]
}

#2.查询示例:
GET /price/_search
{
  "sort": [
    {
      "price": {
        "order": "desc",
        "mode": "min"
      }
    }
  ]
}

GET /price/_search
{
  "sort": [
    {
      "price": {
        "order": "desc",
        "mode": "max"
      }
    }
  ]
}

#Elasticsearch还支持按一个或多个嵌套对象中的字段排序。
#嵌套字段排序支持在现有排序选项之上有以下参数
#1.nested_path (嵌套对象中的直接字段:必须),
#2.nested_filter (嵌套路径内的内部对象应与其匹配的过滤器)
#1.准备数据
PUT /shop/doc/1
{
  "name":"shop1",
  "price": {
    "total_price":10,
    "actual_price":6
  },
  "product":{
    "price":1,
    "quantity":[5,6]
  }
}
PUT /shop/doc/2
{
  "name":"shop2",
  "price": {
    "total_price":6,
    "actual_price":5
  },
  "product":{
    "price":1,
    "quantity":[4,6]
  }
}
PUT /shop/doc/3
{
  "name":"shop3",
  "price": {
    "total_price":3,
    "actual_price":7
  },
  "product":{
    "price":1,
    "quantity":[1,2]
  }
}
DELETE /shop
#2.查询示例(存在问题-需要nested类型):
GET /shop/_search
{
  "sort": [
    {
      "product.quantity": {
        "order": "desc",
        "nested_path": "product"
      }
    }
  ]
}

#查询指定字段(只包含 name 字段)
GET /kevin/_search
{
  "_source": {
    "includes": ["name"]
  }
}

原文地址:https://www.cnblogs.com/sunshinekevin/p/13264331.html