博主信息
Sky
博文
291
粉丝
0
评论
0
访问量
6500
积分:0
P豆:617

ElasticSearch API & 文档 curd 操作

2021年10月19日 20:04:43阅读数:20博客 / Sky

Search API

URI Search: uri 中带参数Request Body search: es 提供,基于json格式更加完备的DSL

指定查询索引

/_search :集群上所有索引/index1,index2/_doc/_search :index1,index2 索引/index*/_doc/_search :以index开头的索引

请求后响应

took: 花费时间total: 符合条件的总文档数hits: 结果集,默认前十_source: 文档原始信息

搜索相关性

搜索是用户和搜索引擎的对话用户关系搜索结果的相关性是否可以找到搜索相关内容有多少部相关内容被返回文档打分排序是否合理结合业务需求,平衡结果排名

Page Rank 算法

不仅仅是内容更重要的内容可信度

网站搜索引擎,其实是 销售的角色

提升销售业绩去除库存

衡量相关性

查准率 - 尽可能返回较少无关文档查全率 - 尽量返回较多的手机号拍卖平台相关文档排名 - 是否能按照相关度进行排序

文档的基本 CRUD 与批量操作

课程Demo

############Create Document############
#create document. 自动生成 _id
POST users/_doc
{
"user" : "Mike",
   "post_date" : "2019-04-15T14:12:12",
   "message" : "trying out Kibana"
}

#create document. 指定Id。如果id已经存在,报错
PUT users/_doc/1?op_type=create
{
   "user" : "Jack",
   "post_date" : "2019-05-15T14:12:12",
   "message" : "trying out Elasticsearch"
}

#create document. 指定 ID 如果已经存在,就报错
PUT users/_create/1
{
    "user" : "Jack",
   "post_date" : "2019-05-15T14:12:12",
   "message" : "trying out Elasticsearch"
}

### Get Document by ID
#Get the document by ID
GET users/_doc/1


###  Index & Update
#Update 指定 ID  (先删除,在写入)
GET users/_doc/1

PUT users/_doc/1
{
"user" : "Mike"

}


#GET users/_doc/1
#在原文档上增加字段
POST users/_update/1/
{
   "doc":{
       "post_date" : "2019-05-15T14:12:12",
       "message" : "trying out Elasticsearch"
   }
}



### Delete by Id
# 删除文档
DELETE users/_doc/1


### Bulk 操作
#执行两次,查看每次的结果

#执行第1次
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }


#执行第2次
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

### mget 操作
GET /_mget
{
   "docs" : [
       {
           "_index" : "test",
           "_id" : "1"
       },
       {
           "_index" : "test",
           "_id" : "2"
       }
   ]
}


#URI中指定index
GET /test/_mget
{
   "docs" : [
       {

           "_id" : "1"
       },
       {

           "_id" : "2"
       }
   ]
}


GET /_mget
{
   "docs" : [
       {
           "_index" : "test",
           "_id" : "1",
           "_source" : false
       },
       {
           "_index" : "test",
           "_id" : "2",
           "_source" : ["field3", "field4"]
       },
       {
           "_index" : "test",
           "_id" : "3",
           "_source" : {
               "include": ["user"],
               "exclude": ["user.location"]
           }
       }
   ]
}

### msearch 操作
POST kibana_sample_data_ecommerce/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}


### 清除测试数据
#清除数据
DELETE users
DELETE test
DELETE test2


版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • Proxy是ES6中提供的新的API,可以用来定义对象各种基本的自定义行为 (在中被称为traps,我觉得可以理解为一个针对对象各种行为的钩子),拿它可以做很多有意思的事情,在我们需要对一些对象的行为进行控制时将变得非常有效