【Elasticsearch7】3-基本操作
目录
RESTful
数据格式
HTTP操作
索引操作
倒排索引
创建索引
查看所有索引
查看单个索引
删除索引
文档操作
创建文档
查看文档
编辑
全量修改
编辑局部修改
删除文档
条件删除文档
高级查询
条件查询
URL带参查询
请求体带参查询
带请求体方式的查找所有内容
查询指定字段
分页查询
查询排序
多条件查询
范围查询
全文检索
完全匹配
高亮查询
聚合查询
映射操作
创建映射
查询映射
RESTful
REST 指的是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是RESTful。Web 应用程序最重要的 REST 原则是,客户端和服务器之间的交互在请求之间是无状态的。从客户端到服务器的每个请求都必须包含理解请求所必需的信息。如果服务器在请求之间的任何时间点重启,客户端不会得到通知。此外,无状态请求可以由任何可用服务器回答,这十分适合云计算之类的环境。客户端可以缓存数据以改进性能。
在服务器端,应用程序状态和功能可以分为各种资源。资源是一个有趣的概念实体,它向客户端公开。资源的例子有:应用程序对象、数据库记录、算法等等。每个资源都使用 URI(Universal Resource Identifier) 得到一个唯一的地址。所有资源都共享统一的接口,以便在客户端和服务器之间传输状态。使用的是标准的 HTTP 方法,比如 GET、 PUT、 POST 和DELETE。
在 REST 样式的 Web 服务中,每个资源都有一个地址。资源本身都是方法调用的目标,方法列表对所有资源都是一样的。这些方法都是标准方法,包括 HTTP GET、 POST、PUT、 DELETE,还可能包括 HEAD 和 OPTIONS。简单的理解就是,如果想要访问互联网上的资源,就必须向资源所在的服务器发出请求,请求体中必须包含资源的网络路径, 以及对资源进行的操作(增删改查)。
REST 样式的 Web 服务若有返回结果,大多数以JSON字符串形式返回。
数据格式
ElasticSearch是面向文档型数据库,一条数据就是一个文档,将ElasticSearch里存储文档数据和MySQL存储数据的概念进行一个类比:
ES里的Index可以看做一个库,而Types相当于表,Documents则相当于表的行
ES6.X中,一个index下只能包含一个type,ES7.X中,Type的概念已被删除
6用JSON作为文档序列化的格式,比如一条用户信息:
HTTP操作
索引操作
倒排索引
正排索引(传统)
倒排索引
创建索引
对比关系型数据库,创建索引就等同于创建数据库。
在 Postman 中,向 ES 服务器发 PUT 请求 : http://127.0.0.1:9200/shopping
请求后,服务器返回响应:
如果重复发 PUT 请求 : http://127.0.0.1:9200/shopping 添加索引,会返回错误信息 :
查看所有索引
在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/_cat/indices?v
这里请求路径中的_cat 表示查看的意思, indices 表示索引,所以整体含义就是查看当前 ES服务器中的所有索引,就好像 MySQL 中的 show tables 的感觉,服务器响应结果如下 :
查看单个索引
在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping
查看索引向ES服务器发送的请求路径和创建索引是一致的,但是HTTP方法不一致,这里可以体会一下RESTful的意义,请求后,服务器响应结果如下:
删除索引
在 Postman 中,向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping
返回结果如下:
重新访问索引时,服务器返回响应:索引不存在
文档操作
创建文档
假设索引已经创建好了,接下来我们来创建文档,并添加数据。这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为 JSON 格式
在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc,
请求体JSON内容为:
注意,此处发送请求的方式必须为 POST,不能是 PUT,否则会发生错误。
服务器响应结果如下:
上面的数据创建后,由于没有指定数据唯一性标识(ID),默认情况下, ES 服务器会随机生成一个。
如果想要自定义唯一性标识,需要在创建时指定: http://127.0.0.1:9200/shopping/_doc/1,请求体JSON内容为:
此处需要注意:如果增加数据时明确数据主键,那么请求方式也可以为 PUT。
查看文档
查看文档时,需要指明文档的唯一性标识,类似于 MySQL 中数据的主键查询
在 Postman 中,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1 。
返回结果如下:
查找不存在的内容,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_doc/1001。
返回结果如下:
{"_index": "shopping","_type": "_doc","_id": "1001","found": false
}
查看索引下所有数据,向 ES 服务器发 GET 请求 : http://127.0.0.1:9200/shopping/_search。
返回结果如下:
{"took": 133,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "1","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}}]}
}
全量修改
和新增文档一样,输入相同的 URL 地址请求,如果请求体变化,会将原有的数据内容覆盖
在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_doc/1
请求体JSON内容为:
修改成功后,服务器响应结果:
局部修改
修改数据时,也可以只修改某一给条数据的局部信息
在 Postman 中,向 ES 服务器发 POST 请求 : http://127.0.0.1:9200/shopping/_update/1。
请求体JSON内容为:
返回结果如下:
根据唯一性标识,查询文档数据,文档数据已经更新
删除文档
删除一个文档不会立即从磁盘上移除,它只是被标记成已删除(逻辑删除)。
在 Postman 中,向 ES 服务器发 DELETE 请求 : http://127.0.0.1:9200/shopping/_doc/1
返回结果:
删除后再查询当前文档信息
如果删除一个并不存在的文档
条件删除文档
一般删除数据都是根据文档的唯一性标识进行删除,实际操作时,也可以根据条件对多条数据进行删除
首先分别添加多条数据:
向ES服务器发POST请求:http://127.0.0.1:9200/shopping/_delete_by_query
请求体内容为:
删除成功后,服务器响应结果:
高级查询
条件查询
假设有以下文档内容,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search:
{"took": 5,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
URL带参查询
查找category为小米的文档,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search?q=category:小米,返回结果如下:
{"took": 94,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": 1.3862942,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
上述为URL带参数形式查询,这很容易让不善者心怀恶意,或者参数值出现中文会出现乱码情况。为了避免这些情况,我们可用使用带JSON请求体请求进行查询。
请求体带参查询
接下带JSON请求体,还是查找category为小米的文档,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match":{"category":"小米"}}
}
返回结果如下:
{"took": 3,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": 1.3862942,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
带请求体方式的查找所有内容
查找所有文档内容,也可以这样,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match_all":{}}
}
则返回所有文档内容:
{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
查询指定字段
如果想查询指定字段,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match_all":{}},"_source":["title"]
}
返回结果如下:
{"took": 5,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机"}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1,"_source": {"title": "小米手机"}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1,"_source": {"title": "小米手机"}},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 1,"_source": {"title": "华为手机"}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 1,"_source": {"title": "华为手机"}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 1,"_source": {"title": "华为手机"}}]}
}
分页查询
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match_all":{}},"from":0,"size":2
}
返回结果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
查询排序
如果想通过排序查出价格最高的手机,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match_all":{}},"sort":{"price":{"order":"desc"}}
}
返回结果如下:
{"took": 96,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": null,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": null,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999},"sort": [3999]},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": null,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"sort": [1999]},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": null,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"sort": [1999]},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": null,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"sort": [1999]},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": null,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"sort": [1999]},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": null,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"sort": [1999]}]}
}
多条件查询
假设想找出小米牌子,价格为3999元的。(must相当于数据库的&&)
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"bool":{"must":[{"match":{"category":"小米"}},{"match":{"price":3999.00}}]}}
}
返回结果如下:
{"took": 134,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 2.3862944,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 2.3862944,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}}]}
}
假设想找出小米和华为的牌子。(should相当于数据库的||)
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"bool":{"should":[{"match":{"category":"小米"}},{"match":{"category":"华为"}}]},"filter":{"range":{"price":{"gt":2000}}}}
}
返回结果如下:
{"took": 8,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1.3862942,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 1.3862942,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 1.3862942,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 1.3862942,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
范围查询
假设想找出小米和华为的牌子,价格大于2000元的手机。
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"bool":{"should":[{"match":{"category":"小米"}},{"match":{"category":"华为"}}],"filter":{"range":{"price":{"gt":2000}}}}}
}
返回结果如下:
{"took": 72,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 1.3862942,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1.3862942,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}}]}
}
全文检索
这功能像搜索引擎那样,如品牌输入“小华”,返回结果带回品牌有“小米”和“华为”的。
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match":{"category" : "小华"}}
}
返回结果如下:
{"took": 7,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 0.6931471,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 0.6931471,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 0.6931471,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 0.6931471,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
完全匹配
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match_phrase":{"category" : "为"}}
}
返回结果如下:
{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": 0.6931471,"hits": [{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]}
}
高亮查询
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"query":{"match_phrase":{"category" : "为"}},"highlight":{"fields":{"category":{}//<----高亮这字段}}
}
返回结果如下:
{"took": 100,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 3,"relation": "eq"},"max_score": 0.6931471,"hits": [{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"highlight": {"category": ["华<em>为</em>"//<------高亮一个为字。]}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"highlight": {"category": ["华<em>为</em>"]}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 0.6931471,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999},"highlight": {"category": ["华<em>为</em>"]}}]}
}
聚合查询
聚合允许使用者对 es 文档进行统计分析,类似与关系型数据库中的 group by,当然还有很多其他的聚合,例如取最大值max、平均值avg等等。
接下来按price字段进行分组:
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"aggs":{//聚合操作"price_group":{//名称,随意起名"terms":{//分组"field":"price"//分组字段}}}
}
返回结果如下:
{"took": 63,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shopping","_type": "_doc","_id": "ANQqsHgBaKNfVnMbhZYU","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 3999}},{"_index": "shopping","_type": "_doc","_id": "A9R5sHgBaKNfVnMb25Ya","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BNR5sHgBaKNfVnMb7pal","_score": 1,"_source": {"title": "小米手机","category": "小米","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "BtR6sHgBaKNfVnMbX5Y5","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "B9R6sHgBaKNfVnMbZpZ6","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}},{"_index": "shopping","_type": "_doc","_id": "CdR7sHgBaKNfVnMbsJb9","_score": 1,"_source": {"title": "华为手机","category": "华为","images": "http://www.gulixueyuan.com/xm.jpg","price": 1999}}]},"aggregations": {"price_group": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": 1999,"doc_count": 5},{"key": 3999,"doc_count": 1}]}}
}
上面返回结果会附带原始数据的。若不想要不附带原始数据的结果,在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"aggs":{"price_group":{"terms":{"field":"price"}}},"size":0
}
返回结果如下:
{"took": 60,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"price_group": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": 1999,"doc_count": 5},{"key": 3999,"doc_count": 1}]}}
}
若想对所有手机价格求平均值。
在 Postman 中,向 ES 服务器发 GET请求 : http://127.0.0.1:9200/shopping/_search,附带JSON体如下:
{"aggs":{"price_avg":{//名称,随意起名"avg":{//求平均"field":"price"}}},"size":0
}
返回结果如下:
{"took": 14,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": null,"hits": []},"aggregations": {"price_avg": {"value": 2332.3333333333335}}
}
映射操作
有了索引库,等于有了数据库中的 database。
接下来就需要建索引库(index)中的映射了,类似于数据库(database)中的表结构(table)。
创建数据库表需要设置字段名称,类型,长度,约束等;索引库也一样,需要知道这个类型下有哪些字段,每个字段有哪些约束信息,这就叫做映射(mapping)。
创建映射
先创建一个索引:
# PUT http://127.0.0.1:9200/user
返回结果:
{"acknowledged": true,"shards_acknowledged": true,"index": "user"
}
创建映射:
# PUT http://127.0.0.1:9200/user/_mapping{"properties": {"name":{"type": "text","index": true},"sex":{"type": "keyword","index": true},"tel":{"type": "keyword","index": false}}
}
返回结果如下:
{"acknowledged": true
}
查询映射
#GET http://127.0.0.1:9200/user/_mapping
返回结果如下:
{"user": {"mappings": {"properties": {"name": {"type": "text"},"sex": {"type": "keyword"},"tel": {"type": "keyword","index": false}}}}
}
增加数据:
#PUT http://127.0.0.1:9200/user/_create/1001
{"name":"小米","sex":"男的","tel":"1111"
}
返回结果如下:
{"_index": "user","_type": "_doc","_id": "1001","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 0,"_primary_term": 1
}
查找name含有”小“数据:
#GET http://127.0.0.1:9200/user/_search
{"query":{"match":{"name":"小"}}
}
返回结果如下:
{"took": 495,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 0.2876821,"hits": [{"_index": "user","_type": "_doc","_id": "1001","_score": 0.2876821,"_source": {"name": "小米","sex": "男的","tel": "1111"}}]}
}
查找sex含有”男“数据:
#GET http://127.0.0.1:9200/user/_search
{"query":{"match":{"sex":"男"}}
}
返回结果如下:
{"took": 1,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 0,"relation": "eq"},"max_score": null,"hits": []}
}
找不想要的结果,只因创建映射时"sex"的类型为"keyword"。
"sex"只能完全为”男的“,才能得出原数据。
#GET http://127.0.0.1:9200/user/_search
{"query":{"match":{"sex":"男的"}}
}
返回结果如下:
{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 0.2876821,"hits": [{"_index": "user","_type": "_doc","_id": "1001","_score": 0.2876821,"_source": {"name": "小米","sex": "男的","tel": "1111"}}]}
}
查询电话
# GET http://127.0.0.1:9200/user/_search
{"query":{"match":{"tel":"11"}}
}
返回结果如下:
{"error": {"root_cause": [{"type": "query_shard_exception","reason": "failed to create query: Cannot search on field [tel] since it is not indexed.","index_uuid": "ivLnMfQKROS7Skb2MTFOew","index": "user"}],"type": "search_phase_execution_exception","reason": "all shards failed","phase": "query","grouped": true,"failed_shards": [{"shard": 0,"index": "user","node": "4P7dIRfXSbezE5JTiuylew","reason": {"type": "query_shard_exception","reason": "failed to create query: Cannot search on field [tel] since it is not indexed.","index_uuid": "ivLnMfQKROS7Skb2MTFOew","index": "user","caused_by": {"type": "illegal_argument_exception","reason": "Cannot search on field [tel] since it is not indexed."}}}]},"status": 400
}
报错只因创建映射时"tel"的"index"为false。
相关文章:
【Elasticsearch7】3-基本操作
目录 RESTful 数据格式 HTTP操作 索引操作 倒排索引 创建索引 查看所有索引 查看单个索引 删除索引 文档操作 创建文档 查看文档 编辑 全量修改 编辑局部修改 删除文档 条件删除文档 高级查询 条件查询 URL带参查询 请求体带参查询 带请求体方式的查…...
给定一整数数组,其中有p种数出现了奇数次,其他数都出现了偶数次,怎么找到这p个数?
给定一长度为m的整数数组 ,其中有p种不为0的数出现了奇数次,其他数都出现了偶数次,找到这p个数。 要求:时间复杂度不大于O(n),空间复杂度不大于O(1)。 由于时间复杂度不大于O(n),则不能在遍历数组中嵌套遍…...
RICHTEK立锜科技 WIFI 7电源参考设计
什么是WIFI 7? WiFi 7(Wi-Fi 7)是下一代Wi-Fi标准,对应的是IEEE 802.11将发布新的修订标准IEEE 802.11be –极高吞吐量EHT(Extremely High Throughput )。Wi-Fi 7是在Wi-Fi 6的基础上引入了320MHz带宽、4096-QAM、Mu…...
CUDA编程00 - 配置CUDA开发环境
第一步: 在一台装有Nvidia显卡和驱动的机器上,用nvidia-smi命令查看显卡所支持cuda版本 第二步: 到Nvidia官网下载CUDA Toolkit并安装,CUDA Toolkit Archive | NVIDIA Developer 安装时按提示下一步即可,安装完成用 …...
HTML5大作业三农有机,农产品,农庄,农旅网站源码
文章目录 1.设计来源1.1 轮播图页面头部效果1.2 栏目列表页面效果1.3 页面底部导航效果 2.效果和源码2.1 源代码 源码下载万套模板,程序开发,在线开发,在线沟通 作者:xcLeigh 文章地址:https://blog.csdn.net/weixin_4…...
Spark的动态资源分配算法
文章目录 前言基于任务需求进行资源请求的整体过程资源申请的生成过程详解资源申请的生成过程的简单例子资源调度算法的代码解析 申请资源以后的处理:Executor的启动或者结束对于新启动的Container的处理对于结束的Container的处理 基于资源分配结果进行任务调度Pen…...
Python 爬虫技术 第06节 HTTP协议与Web基础知识
HTTP(Hypertext Transfer Protocol)是用于从Web服务器传输超文本到本地浏览器的传输协议。它是互联网上应用最为广泛的一种网络协议,几乎所有的网页数据都是通过HTTP协议进行传输的。下面,我将结合一个简单的Python案例来详细讲解…...
js | 原型链
为什么前者会输出Lucas 后者不会?call动作具体干了什么? http://dmitrysoshnikov.com/ecmascript/javascript-the-core/ function Foo(){this.bar"Lucas" } let obj{}; obj.__proto__Foo.prototype; Foo.call(obj) console.log(obj.bar); // 输出Lucas/…...
Volatility:分析MS10-061攻击
1、概述 # 1)什么是 Volatility Volatility是开源的Windows,Linux,MaC,Android的内存取证分析工具。基于Python开发而成,可以分析内存中的各种数据。Volatility支持对32位或64位Wnidows、Linux、Mac、Android操作系统…...
水表数字识别3:Pytorch CRNN实现水表数字识别(含训练代码和数据集)
水表数字识别3:Pytorch CRNN实现水表数字识别(含训练代码和数据集) 目录 水表数字识别3:Pytorch CRNN实现水表数字识别(含训练代码和数据集) 1.前言 2. 水表数字识别的方法 3. 水表数字识别数据集 4. 水表数字分割模型训练 5. 水表数字识别模型训…...
oracle数据文件损坏和误删dbf文件处理方法
加油,新时代打工人! 打开sqlplus sqlplus> “/as sysdba” (命令行登录sqlplus) SQL>shutdown abort; (关闭oracle数据库服务器) SQL>startup mount ;(挂载oracle数据库,这…...
postMessageXss续2
原文地址如下:https://research.securitum.com/art-of-bug-bounty-a-way-from-js-file-analysis-to-xss/ 在19年我写了一篇文章,是基于postMessageXss漏洞的入门教学:https://www.cnblogs.com/piaomiaohongchen/p/14727871.html 这几天浏览mXss技术的时候ÿ…...
【深度学习】sdxl的Lora训练技巧
在进行SDXL LoRA训练时,有一些技巧和最佳实践可以帮助你获得更好的结果。以下是一些重要的建议: 图像选择与标注: 选择多样化的高质量图像是关键,建议至少使用30到50张分辨率为1024x1024的图像【8†source】【9†source】。使用Vi…...
推荐一款 Android 手机端的 SSH 远程连接工具
https://andi.cn/page/621590.html...
3.1、matlab双目相机标定实验
1、双目相机标定原理及流程 双目相机标定是将双目相机系统的内外参数计算出来,从而实现双目视觉中的立体测量和深度感知。标定的目的是确定各个摄像头的内部参数(如焦距、主点、畸变等)和外部参数(如相机位置、朝向等),以便将双目相机捕获的图像转换为三维空间坐标。 双…...
IntelliJ IDEA 直接在软件中更新为最新版
当我们的 IDEA 工具许久没有更新,已经拖了好几个版本,想跨大版本更新,比如从2020.2.1 -> 2023.x.x 此时,我们菜单栏点击 Help -> Check for Updates… ,右下角会有提示更新,如下图: 点…...
库卡机器人示教器 KPC2 00107-264 KPC200.107-264
库卡驱动器是一种高性能的控制器,其作用类似于变频器在普通交流马达中的应用。它通过位置、速度和力矩三种方式对伺服马达进行控制,以满足各种高精度定位系统的需求。库卡驱动器是伺服系统的重要组成部分,广泛应用于各种工业自动化领域。 库…...
数据传输安全--VPN
目录 前置知识 VPN概念 VPN诞生的原因 VPN分类 根据建设的单位不同分类 企业自建的VPN 运营商搭建的VPN 根据组网方式不同来进行分类 Client to LAN VPN LAN to LAN VPN按层次划分 VPN常用技术 VPN的核心技术 VPN封装过程的角色 VPN包含的技术 身份认证技术 加…...
【人工智能】人工智能可解释性和透明度的详细探讨
人工智能的可解释性和透明度是当前AI领域的重要议题,它们对于AI系统的公正性、可靠性、用户信任以及合规性等方面都具有深远的影响。以下是对人工智能可解释性和透明度的详细探讨: 一、人工智能的可解释性 定义: 可解释性是指机器学习模型…...
vscode+wsl2+anaconda环境的配置与使用
目录 下载anaconda Anaconda使用参考 vscodeubuntuanaconda 先用vscode连接本地ubuntu。 如果没有安装wsl2与ubuntu,可点击下面的链接。 问题:wsl install 无法解析服务器 成功记录: 在vscode终端用ubuntu安装anaconda。 创建pytho…...
【Linux网络】套接字编程
本篇博客整理了 socket 套接字编程的相关内容,包括 socket 网络通信原理、socket 相关的系统调用接口等,分别演示了基于UDP协议、TCP协议的 socket 网络编程,旨在让读者更加深入理解网络通信原理和设计,对网络编程有初步的认识和掌…...
在线 PDF 制作者泄露用户上传的文档
两家在线 PDF 制作者泄露了数万份用户文档,包括护照、驾驶执照、证书以及用户上传的其他个人信息。 我们都经历过这样的情况:非常匆忙,努力快速制作 PDF 并提交表单。许多人向在线 PDF 制作者寻求帮助,许多人的祈祷得到了回应。 …...
SQL概述及其规则与规范
SQL概述及其规则与规范 1.SQL概述 1.1 SQL背景知识 1946年,世界第一台电脑诞生,如今,互联网已经非常壮大,在这几十年间互联网得到了飞速的发展,无数的技术在其中起起伏伏,但是有一门技术从未消失…...
开源模型应用落地-FastAPI-助力模型交互-进阶篇-RequestDataclasses(三)
一、前言 FastAPI 的高级用法可以为开发人员带来许多好处。它能帮助实现更复杂的路由逻辑和参数处理,使应用程序能够处理各种不同的请求场景,提高应用程序的灵活性和可扩展性。 在数据验证和转换方面,高级用法提供了更精细和准确的控制&#…...
2024.7.20 暑期训练记录(6)
CF 1391D - 505(思维状压dp) 首先简化问题,发现一个矩阵如果要满足条件,那它其中的每一个 2 2 2\times 2 22 的小矩阵都要满足条件,于是很容易发现 4 4 4\times4 44 的矩阵是一定不满足条件的(因为是…...
firefly rk3288 ubuntu23.10 网卡名为end0 改为eth0
1、内核源码修改u-boot/include/env_default.h文件第32行的bootargs参数,修改后: "bootargs net.ifrenames0 " CONFIG_BOOTARGS "\0"2、修改rootfs里的lib/systemd/network/99-default.link文件: [M…...
git使用总结
概述 简介 Git是一种代码托管技术,很多代码托管平台也是基于Git来实现的。 Git可以帮我们做到很多的事情,比如代码的版本控制,分支管理等。 网址 git官网:https://git-scm.com/ 版本控制系统【VCS】 可以完整保存项目的快照&#…...
使用多进程和多线程实现服务器并发【C语言实现】
在TCP通信过程中,服务器端启动之后可以同时和多个客户端建立连接,并进行网络通信,但是在一个单进程的服务器的时候,提供的服务器代码却不能完成这样的需求,先简单的看一下之前的服务器代码的处理思路,再来分…...
深入理解Linux网络(三):TCP对象创建
深入理解Linux网络(三):TCP对象创建 TCP对象创建inet_createsock_init_data TCP对象创建 常见的三句TCP编程: int main() {int sk socket(AF_INET, SOCK_STREAM, 0);connect(sk, ...)recv(sk, ...) }简单的两三⾏代码ÿ…...
windows server——4.安装DNS管理器
windows server——4.安装DNS管理器 一、准备二、安装DNS管理器1.打开服务器管理器2.添加dns服务器 三、验证 一、准备 windows server电脑(已安装IIS) 静态网站数据包 二、安装DNS管理器 1.打开服务器管理器 2.添加dns服务器 点击管理——添加角色和…...
江西移动网站/广告公司职位
tidb在插入一个新数据的时候报错: Table has no partition for value xxx 表明表中没有这个分区,需要新增 查一下新增语句 ALTER TABLE fans_medal ADD PARTITION (PARTITION p20200828 VALUES LESS THAN (20200829));然后再插入我需要的数据 解决&…...
微信公众号采集插件wordpress/seo查询工具网站
像排序这种事情,用C/C可以写,但很麻烦,交给sort就好了,功能很强大的。1、按照多个列排序(列间空格分开):测试数据:先按照第1列排序,再第2列的命令:2011-11-20补充:必须加…...
办公设备网站推广怎么做/seo站内优化包括
第一章 绪论 问题的提出 基本概念: NLU、 CL、 NLP CL 计算语言学 侧重于: 基础理论和方法 NLU 自然语言理解 :模仿人类 自然语言处理方法 和 实现技术 NLP 自然语言处理 : 对语言文本进行处理和加工 包括对词法 句法 语义和语…...
个人网站怎么做cps/昆明网络营销
1.创建UserPackage.java //-- 所在的包名,也就是位置。包在物理上就是一个文件夹,逻辑上代表一个分类的概念。 package Other; //-- 引入包Company中的Manager类 import Company.Manager;//-- 入口类(主类),必须与文件…...
制作网站软件都在什么公司/国内优秀个人网站欣赏
本章提出一种基于经验模态分解的血管内膜中层厚度测量的方法并,它涉及一种基于希尔伯特黄变换的图像特征提取方法。它解决了当前有些图像特征提取方法无法分离特征层以改善提取精度的问题。与之前的方法相比具有精度更高的优点。算法结构如下图所示: 给我…...
青海省住房和建设厅网站首页/市场调研与分析
https://mp.weixin.qq.com https://mp.weixin.qq.com/s?__bizMzI3NzE0NjcwMg...