ElasticSearch学习笔记

预备知识-Restful

风闻ElasticSearch API是纯Restful风格的,那他喵的什么是Restful?


 

起源

 

在没有前后端分离概念之前,一个网站的完成总是"all in one",在这个阶段,页面、数据、渲染全部在服务端完成,这样做的最大的弊端是后期维护,扩展极其痛苦,开发人员必须同时具备前后端知识。

于是后来慢慢的兴起了前后端分离的思想:即后端负责数据编造,而前端则负责数据渲染,前端静态页面调用指定 api 获取到有固定格式的数据,再将数据展示出来,这样呈现给用户的就是一个 "动态" 的过程。

 

 

而关于 api 这部分的设计则成了一个问题。如何设计出一个便于理解,容易使用的 api 则成了一个问题,而所谓的 RESTful 就是用来规范我们的 API 的一种约束。
所以巴拉巴拉了这么多,Restful就是规范API的一种约束。

REST

作为 REST,其实是 Representational State Transfer(表象层状态转变)三个单词的缩写,它由 Roy Fielding(Fielding 是 HTTP 协议(1.0 版和 1.1 版)的主要设计者、Apache 服务器软件的作者之一、Apache 基金会的第一任主席)于 2000 年论文中提出,他在论文中提到:"我这篇文章的写作目的,就是想在符合架构原理的前提下,理解和评估以网络为基础的应用软件的架构设计,得到一个功能强、性能好、适宜通信的架构。REST 指的是一组架构约束条件和原则。"
如果一个架构符合 REST 的约束条件和原则,我们就称它为 RESTful 架构。
 
要理解 RESTful 架构,最好的方法就是去理解 Representational State Transfer 这个词组到底是什么意思,它的每一个词代表了什么涵义。如果你把这个名称搞懂了,也就不难体会 REST 是一种什么样的设计。
 

基本操作

 索引

1.创建索引

PUT /索引名称

PUT /haoke
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "haoke" }

2.查看索引

GET /索引名称

GET /haoke
{ "haoke" : { "aliases" : { }, "mappings" : { }, "settings" : { "index" : { "routing" : { "allocation" : { "include" : { "_tier_preference" : "data_content" } } }, "number_of_shards" : "1", "provided_name" : "haoke", "creation_date" : "1625209127283", "number_of_replicas" : "1", "uuid" : "TRiu2s7fTv-RExr4O9zT5g", "version" : { "created" : "7130299" } } } } }

3.删除索引

DELETE /索引名称

DELETE /haoke

{
  "acknowledged" : true
}

 

文档

1.创建文档

PUT /索引名称/类型/ID

PUT /haoke/user/1001
{
  "name": "张翼德",
  "sex": "男",
  "age": 30,
  "address": "涿郡屠夫"
}

 

2.查看指定文档

GET /索引名称/类型/ID

GET /haoke/user/1001
{ "_index" : "haoke", "_type" : "user", "_id" : "1001", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "name" : "张翼德", "sex" : "男", "age" : 30, "address" : "涿郡屠夫" } }

 

3.查看所有文档

GET /索引名称/_search

GET /haoke/user/_search
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "haoke", "_type" : "user", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "张翼德", "sex" : "男", "age" : 30, "address" : "涿郡屠夫" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "关羽", "sex" : "男", "age" : 32, "address" : "河东解良" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "刘备", "sex" : "男", "age" : 36, "address" : "世居巷里" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "曹操", "sex" : "男", "age" : 36, "address" : "世之枭雄" } } ] } }

 

4.分页查询文档

GET /索引名称/_search?size=xx&from=xx

from即从多少开始插,默认0

size即一次查多少条

类似 mysql 中 limit 0, 10

GET /haoke/_search?size=2&from=1
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1002",
        "_score" : 1.0,
        "_source" : {
          "name" : "关羽",
          "sex" : "男",
          "age" : 32,
          "address" : "河东解良"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1003",
        "_score" : 1.0,
        "_source" : {
          "name" : "刘备",
          "sex" : "男",
          "age" : 36,
          "address" : "世居巷里"
        }
      }
    ]
  }
}

 

5.删除指定文档

DELETE /索引名称/类型/ID

DELETE /haoke/user/1003
{
  "_index" : "haoke",
  "_type" : "user",
  "_id" : "1003",
  "_version" : 4,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 6,
  "_primary_term" : 1
}

6.修改指定文档

POST /索引名称/类型/ID

POST /haoke/user/1003
{
  
  "name": "魏延",
  "sex": "男",
  "age": 38,
  "address": "北京西三旗"
}

响应结果:
{
  "_index" : "haoke",
  "_type" : "user",
  "_id" : "1003",
  "_version" : 2,
  "_seq_no" : 8,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "魏延",
    "sex" : "男",
    "age" : 38,
    "address" : "北京西三旗"
  }
}

 

查询操作

1.查询当前类型中的所有文档

GET /索引名称/索引类型/_search

GET /haoke/user/_search

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1001",
        "_score" : 1.0,
        "_source" : {
          "name" : "张翼德",
          "sex" : "男",
          "age" : 30,
          "address" : "涿郡屠夫"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1002",
        "_score" : 1.0,
        "_source" : {
          "name" : "关羽",
          "sex" : "男",
          "age" : 32,
          "address" : "河东解良"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1004",
        "_score" : 1.0,
        "_source" : {
          "name" : "曹操",
          "sex" : "男",
          "age" : 36,
          "address" : "世之枭雄"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1003",
        "_score" : 1.0,
        "_source" : {
          "name" : "魏延",
          "sex" : "男",
          "age" : 38,
          "address" : "北京西三旗"
        }
      }
    ]
  }
}

 

2.条件查询

如果查询年龄等于36岁的

GET /haoke/user/_search?q=age:36

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1004",
        "_score" : 1.0,
        "_source" : {
          "name" : "曹操",
          "sex" : "男",
          "age" : 36,
          "address" : "世之枭雄"
        }
      }
    ]
  }
}

 

3.范围查询

GET /索引名称/类型/_search?q=xxx[xx TO xx]  ,注意TO大写

GET /haoke/user/_search?q=age[30 TO 36]

{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1001",
        "_score" : 1.0,
        "_source" : {
          "name" : "张翼德",
          "sex" : "男",
          "age" : 30,
          "address" : "涿郡屠夫"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1002",
        "_score" : 1.0,
        "_source" : {
          "name" : "关羽",
          "sex" : "男",
          "age" : 32,
          "address" : "河东解良"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1004",
        "_score" : 1.0,
        "_source" : {
          "name" : "曹操",
          "sex" : "男",
          "age" : 36,
          "address" : "世之枭雄"
        }
      }
    ]
  }
}

 

4. 根据多个ID批量查询_mget

GET /索引名称/类型/_mget

相当于:select * from user where id in (1001, 1002)

GET /haoke/user/_mget
{
  "ids": ["1001", "1002"]
}

结果:
{
  "docs" : [
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1001",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "张翼德",
        "sex" : "男",
        "age" : 30,
        "address" : "涿郡屠夫"
      }
    },
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1002",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "关羽",
        "sex" : "男",
        "age" : 32,
        "address" : "河东解良"
      }
    }
  ]
}

 

5.查询年龄小于等于36岁的憨憨

GET /索引名称/类型/_search?q=age:<=36

GET /haoke/user/_search?q=age:<=36

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1001",
        "_score" : 1.0,
        "_source" : {
          "name" : "张翼德",
          "sex" : "男",
          "age" : 30,
          "address" : "涿郡屠夫"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1002",
        "_score" : 1.0,
        "_source" : {
          "name" : "关羽",
          "sex" : "男",
          "age" : 32,
          "address" : "河东解良"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1004",
        "_score" : 1.0,
        "_source" : {
          "name" : "曹操",
          "sex" : "男",
          "age" : 36,
          "address" : "世之枭雄"
        }
      }
    ]
  }
}

 

6.查询年龄大于28岁的

GET /索引名称/类型/_search?q=age:>28

GET /haoke/user/_search?q=age:>28
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "haoke", "_type" : "user", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "张翼德", "sex" : "男", "age" : 30, "address" : "涿郡屠夫" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "关羽", "sex" : "男", "age" : 32, "address" : "河东解良" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "曹操", "sex" : "男", "age" : 36, "address" : "世之枭雄" } }, { "_index" : "haoke", "_type" : "user", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "魏延", "sex" : "男", "age" : 38, "address" : "北京西三旗" } } ] } }

 

7.分页查询

GET /索引名称/类型/_search?size=xx&from=xx

GET /haoke/user/_search?size=2&from=1

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1002",
        "_score" : 1.0,
        "_source" : {
          "name" : "关羽",
          "sex" : "男",
          "age" : 32,
          "address" : "河东解良"
        }
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1004",
        "_score" : 1.0,
        "_source" : {
          "name" : "曹操",
          "sex" : "男",
          "age" : 36,
          "address" : "世之枭雄"
        }
      }
    ]
  }
}

 

8.输出指定查询结果字段_source

比如:只返回name, age字段

GET /索引名称/类型/_search?_source=name,age

GET /索引名称/类型/_search?_source=name,age
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "haoke", "_type" : "user", "_id" : "1001", "_score" : 1.0, "_source" : { "name" : "张翼德", "age" : 30 } }, { "_index" : "haoke", "_type" : "user", "_id" : "1002", "_score" : 1.0, "_source" : { "name" : "关羽", "age" : 32 } }, { "_index" : "haoke", "_type" : "user", "_id" : "1004", "_score" : 1.0, "_source" : { "name" : "曹操", "age" : 36 } }, { "_index" : "haoke", "_type" : "user", "_id" : "1003", "_score" : 1.0, "_source" : { "name" : "魏延", "age" : 38 } } ] } }

 9. 对查询结果排序

GET /索引名称/类型/_search?sort=字段:desc/asc

GET /haoke/user/_search?sort=age:desc
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1003",
        "_score" : null,
        "_source" : {
          "name" : "魏延",
          "sex" : "男",
          "age" : 38,
          "address" : "北京西三旗"
        },
        "sort" : [
          38
        ]
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1004",
        "_score" : null,
        "_source" : {
          "name" : "曹操",
          "sex" : "男",
          "age" : 36,
          "address" : "世之枭雄"
        },
        "sort" : [
          36
        ]
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1002",
        "_score" : null,
        "_source" : {
          "name" : "关羽",
          "sex" : "男",
          "age" : 32,
          "address" : "河东解良"
        },
        "sort" : [
          32
        ]
      },
      {
        "_index" : "haoke",
        "_type" : "user",
        "_id" : "1001",
        "_score" : null,
        "_source" : {
          "name" : "张翼德",
          "sex" : "男",
          "age" : 30,
          "address" : "涿郡屠夫"
        },
        "sort" : [
          30
        ]
      }
    ]
  }
}

 文档批量操作

1.批量获取文档数据

批量获取文档数据是通过_mget API实现的

请求方式:GET
请求地址:_mget
功能说明 : 可以通过ID批量获取不同index和type的数据
请求参数:
docs : 文档数组参数
_index : 指定index
_type : 指定type
_id : 指定id
_source : 指定要查询的字段,以数组的形式:如 ["name", "age"]

----------------在URL中不指定index、type

GET _mget
{
  "docs": 
  [
    {
      "_index": "haoke",
      "_type": "user",
      "_id": "1004",
      "_source": ["name", "age"]
    },
    {
      "_index": "haoke",
      "_type": "user",
      "_id": "1003",
      "_source": ["name", "age"]
    }
  ]
}

 查询结果:

{
  "docs" : [
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1004",
      "_version" : 1,
      "_seq_no" : 3,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "曹操",
        "age" : 36
      }
    },
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1003",
      "_version" : 2,
      "_seq_no" : 8,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "魏延",
        "age" : 38
      }
    }
  ]
}

------------------在URL中指定index

GET /haoke/_mget
{
  "docs": 
  [
    {
      "_type": "user",
      "_id": "1003",
      "_source": ["name", "age"]
    },
    {
      "_type": "user",
      "_id": "1004",
      "_source": ["name", "age"]
    }
    
  ]
}

 查询结果:

{
  "docs" : [
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1003",
      "_version" : 2,
      "_seq_no" : 8,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "魏延",
        "age" : 38
      }
    },
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1004",
      "_version" : 1,
      "_seq_no" : 3,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "曹操",
        "age" : 36
      }
    }
  ]
}

------------------在URL中指定index、type

GET /haoke/user/_mget
{
  "docs":
  [
    {
      "_id": "1001"
    },
    {
      "_id": "1002"
    }
  ]
}

 查询结果:

{
  "docs" : [
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1001",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "张翼德",
        "sex" : "男",
        "age" : 30,
        "address" : "涿郡屠夫"
      }
    },
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1002",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "关羽",
        "sex" : "男",
        "age" : 32,
        "address" : "河东解良"
      }
    }
  ]
}

2.批量操作文档数据

批量对文档进行写操作是通过_bulk的API来实现的
请求方式:POST
请求地址:_bulk
请求参数:通过_bulk操作文档,一般至少有两行参数(或偶数行参数)
第一行参数为指定操作的类型及操作的对象
(index,type和id)
第二行参数才是操作的数据
参数类似于:
{"actionName":{"_index":"indexName", "_type":"typeName","_id":"id"}}
{"field1":"value1", "field2":"value2"}
 
actionName:表示操作类型,主要有create,index,delete和update
 
1.批量创建文档create
POST _bulk
{"create": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"name": "马超", "sex": "男", "age": 34, "address": "西凉"}
{"create": {"_index": "haoke", "_type": "user", "_id": "1006"}}
{"name": "庞德", "sex": "男", "age": 33, "address": "西凉"}
{"create": {"_index": "haoke", "_type": "user", "_id": "1007"}}
{"name": "夏侯惇", "sex": "男", "age": 36, "address": "大魏"}

2.普通创建或者全量替换index

POST _bulk
{"index": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"name": "马岱", "sex": "男", "age": 32, "address": "西凉"}
{"index": {"_index": "haoke", "_type": "user", "_id": "1008"}}
{"name": "诸葛亮", "sex": "男", "age": 28, "address": "南阳"}
如果原文档不存在,则是创建
如果原文档存在,则是替换(全量修改原文档)
验证结果:
GET _mget
{
  "docs": 
  [
    {
      "_index": "haoke",
      "_type": "user",
      "_id": "1005"
    },
    {
      "_index": "haoke",
      "_type": "user",
      "_id": "1008"
    }
  ]
}
{
  "docs" : [
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1005",
      "_version" : 3,
      "_seq_no" : 13,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "马岱",
        "sex" : "男",
        "age" : 32,
        "address" : "西凉"
      }
    },
    {
      "_index" : "haoke",
      "_type" : "user",
      "_id" : "1008",
      "_version" : 1,
      "_seq_no" : 14,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "诸葛亮",
        "sex" : "男",
        "age" : 28,
        "address" : "南阳"
      }
    }
  ]
}

3.批量删除delete

POST _bulk
{"delete": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"delete": {"_index": "haoke", "_type": "user", "_id": "1006"}}
{"delete": {"_index": "haoke", "_type": "user", "_id": "1007"}}
4.批量修改
POST _bulk
{"update": {"_index": "haoke", "_type": "user", "_id": "1005"}}
{"doc": {"name": "马岱"}}
{"update": {"_index": "haoke", "_type": "user", "_id": "1007"}}
{"doc": {"name": "夏侯渊"}}

DSL语言高级查询

Domain Specific Language
领域专用语言
Elasticsearch provides a ful1 Query DSL based on JSON to define queries
Elasticsearch提供了基于JSON的DSL来定义查询。
DSL由叶子查询子句和复合查询子句两种子句组成
 

 

 1.无条件查询

无查询条件是查询所有,默认是查询所有的,或者使用match_all表示所
有。
GET /haoke/user/_search
{
  "query": 
  {
    "match_all": {}
  }
}

2.有条件查询

-------------- 模糊匹配:

模糊匹配主要是针对文本类型的字段,文本类型的字段会对内容进行分
词,对查询时,也会对搜索条件进行分词,然后通过倒排索引查找到匹
配的数据,模糊匹配主要通过match等参数来实现
 
match : 通过match关键词模糊匹配条件内容
prefix : 前缀匹配
regexp : 通过正则表达式来匹配数据
 
match的复杂用法
match条件还支持以下参数:
  •   query : 指定匹配的值
  •   operator : 匹配条件类型
      and : 条件分词后都要匹配
      or : 条件分词后有一个匹配即可(默认)
  •   minmum_should_match : 指定最小匹配的数量

 

------------------ 精准匹配:

term : 单个条件相等
terms : 单个字段属于某个值数组内的值
range : 字段属于某个范围内的值
exists : 某个字段的值是否存在
ids : 通过ID批量查询
 
term 主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经分析的文本数据类型)
示例:
GET /haoke/user/_search
{
  "query": 
  {
    "term": {"age": 36}
  }
}

terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值

示例:

GET /haoke/user/_search
{
  "query": 
  {
    "terms": {"age": [34, 36]}
  }
}

range 过滤允许我们按照指定范围查找一批数据

范围操作符包含:

  • gt : 大于

  • gte:: 大于等于

  • lt : 小于

  • lte: 小于等

GET /haoke/user/_search
{
  "query": 
  {
    "range": {
      "age": {
        "gte": 33,
        "lte": 36
      }
    }
  }
}

exists 查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL 条件

GET /haoke/user/_search
{
  "query": 
  {
    "exists": {"field": "hobby"}
  }
}

ids通过ID批量查询

GET /haoke/user/_search
{
  "query":
  {
    "ids": {"values": ["1001", "1002"]}
  }
}

 

 

 

 
 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2021-06-30 16:20  liuruinian  阅读(38)  评论(0)    收藏  举报