摘要:https://blog.csdn.net/qq_40846086/article/details/81238598
摘要2:https://blog.csdn.net/alex_xfboy/article/details/85332335#2.14%C2%A0Profile%20API
基本概念
Elasticsearch 是 面向文档型数据库,这意味着它存储的是整个对象或者 文档,它不但会存储它们,还会为他们建立索引,这样你就可以搜索他们了。你可以在 Elasticsearch 中索引、搜索、排序和过滤这些文档。不需要成行成列的数据。这将会是完全不同的一种面对数据的思考方式,这也是为什么 Elasticsearch 可以执行复杂的全文搜索的原因。
通过 HTTP 向 RESTful API 传送 json
- 相应的 HTTP 请求方法 或者 变量 : GET, POST, PUT, HEAD 或者 DELETE。
- 集群中任意一个节点的访问协议、主机名以及端口。
- 请求的路径。
- 任意一个查询后再加上 ?pretty 就可以生成 更加美观 的JSON反馈,以增强可读性。
- 一个 JSON 编码的请求主体(如果需要的话)。
在 Elasticsearch 中,文档属于一种 类型(type),各种各样的类型存在于一个 索引 中。你也可以通过类比传统的关系数据库得到一些大致的相似之处:
- 关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns)
- Elasticsearch ⇒ 索引 ⇒ 类型 ⇒ 文档 ⇒ 字段(Fields)
可以在curl后加-i参数得到响应头
curl -i -XGET http://localhost:9200/website/blog/124?pretty
使用postman进行的一些简单操作,kibana会有些差异,但不大
1.查看集群健康状况
GET /_cat/health?v&pretty
2.查看my_index的mapping和setting的相关信息
GET /my_index?pretty
3.查看所有的index
GET /_cat/indices?v&pretty
创建只有一个主分片,没有复制分片的小索引。
PUT /my_temp_index
{
"settings": {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}
用 update-index-settings API 动态修改复制分片个数:
PUT /my_temp_index/_settings
{
"number_of_replicas": 1
}
删除索引
DELETE /my_index
删除多个索引
DELETE /index_one,index_two
DELETE /index_*
删除多个索引
DELETE /_all
创建索引
put 9200/index_ik_m1/resource/_mapping?include_type_name=true
- index: index_ik_m1
- type: resource
注意:es7创建索引时,不再指定type,直接使用`9200/index_ik_m1`即可创建索引,默认的_doc作为type
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"age": {
"type": "keyword"
}
}
}
查看创建的索引
get 9200/index_ik_m1?pretty
{
"index_ik_m1": {
"aliases": {},
"mappings": {
"properties": {
"age": {
"type": "keyword"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "index_ik_m1",
"creation_date": "1624001748642",
"number_of_replicas": "1",
"uuid": "Xdzv1cPGRcWjG6J3fFtMkw",
"version": {
"created": "7130199"
}
}
}
}
}
不指定id,创建一条新的记录
post 9200/index_ik_m1/resource/
{
"content":"现场勘验检查笔录、提取痕迹、物证登记表及涿州市公安局刑事科学技术室出具的说明证实",
"age":76
}
指定id,创建一条记录。如果记录存在,则更新记录
post 9200/index_ik_m1/resource/1
id: 1
{
"content":"现场勘验检查笔录、提取痕迹、物证登记表及涿州市公安局刑事科学技术室出具的说明证实",
"age":78
}
删除一条记录
delete 9200/index_ik_m1/resource/1
id: 1
查询全部数据
post 9200/index_ik_m1/resource/_search
{
"query":{
"match_all":{}
}
}
根据条件查询分页
post 9200/index_ik_m1/resource/_search
{
"query":{
"match":{
"content":"上海"
}
},
"from": 0,
"size": 5,
"sort": {
"age": {
"order":"asc"
}
}
}
项目中requests请求样例
url = "http://192.168.237.130:9200/index_ik_m1/resource/_search"
# query_json = {
# "query": {
# "query_string": {
# "query": "上海",
# "fields": ["content"]
# }
# }
# }
query_json = {
"query": {
"match": {
"content": "上海"
}
}
}
r9 = requests.post(url=url, data=json.dumps(query_json), headers={"Content-Type": "application/json"})
print(r9.text)