码迷,mamicode.com
首页 > 其他好文 > 详细

全文检索-Elasticsearch (二) CURD与DSL

时间:2018-05-25 01:31:54      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:ota   相同   event   failed   custom   class   success   应该   移除   

ElasticSearch6.0以上版本的增删改查基本操作
基于JSON的REST API与ElasticSearch进行通信。可以使用任何HTTP客户端来通信。当然ElasticSearch自己的文档中,所有示例都是使用curl的

一、创建和更新

  • 创建索引

请求示例:

创建customer索引

curl -XPUT http://localhost:9200/students -d 

释义:

customer为索引名称

json返回:

{
"acknowledged": true,
"shards_acknowledged": true,
"index": "students"
}
  •  索引文档

往索引中增加点文档数据

请求示例:

curl -XPUT "http://localhost:9200/students/student/1" -d{
    "name": "jack",
    "age": 12,
    "sex" :"boy"
}

释义:

put请求,在students索引中增加一条类型(type)为student,id为1的文档数据

注意:

  1. type只是Index中的虚拟逻辑分组,不同的Type应该有相似的结构。6.x版只允许每个Index包含一个Type,7.x 版将会彻底移除 Type
  2. id部分是可选的。如果不指定ID,ElasticSearch会为我们生成一个ID。 但是,如果不指定id,应该使用HTTP的POST而不是PUT请求。
  3. 索引名称是任意的。如果服务器上没有此名称的索引,则将使用默认配置来创建一个索引

json返回:

技术分享图片
{
  "_index": "students",
  "_type": "student",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}
View Code
  •  更新文档

只需要使用相同的ID索引它。即和上面的例子相同索引请求,即可更新,也就是说,我们向索引中加入文档,不存在相同ID的文档会插入,存在则更新。

  •  批处理

除了在单个文档上执行索引,更新和删除操作外,Elasticsearch还提供了批操作的功能,通过使用 bulk API完成。这个功能非常重要,因为它提供了一种非常高效的机制去通过更少的网络切换尽可能快的执行多个操作。

Elasticsearch将根据请求体中提供的数据自动创建映射,我们将使用其批量功能在此索引中添加多个JSON对象。

请求示例:

curl -XPOST localhost:9200/students/student/_bulk

  {"create":{"_id":2}}
  {"name":"小明","age":15,"sex":"girl"}
  {"create":{"_id":3}}
  {"name":"小东","age":16, "sex":"boy"}

释义:

批量插入两条ID为2和3的文档

1.bulk api对json 的语法有严格的要求,每个json串不能换行,同时一个json串和一个json串之间,必须有一个换行

2.必须以换行结束

json返回:

技术分享图片
{
    "took": 512,
    "errors": false,
    "items": [
        {
            "create": {
                "_index": "students",
                "_type": "student",
                "_id": "2",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 2,
                    "failed": 0
                },
                "_seq_no": 2,
                "_primary_term": 3,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "students",
                "_type": "student",
                "_id": "3",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 2,
                    "failed": 0
                },
                "_seq_no": 0,
                "_primary_term": 3,
                "status": 201
            }
        }
    ]
}
View Code

二 、简单查询

通过ID来快速获取文档

请求示例:

curl -XGET http://localhost:9200/students/student/1?

释义:

查询students索引,类型为student,ID为1的文档

json返回:

{
  "_index": "students",
  "_type": "student",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "jack",
    "age": 12,
    "sex": "boy"
  }
}

三、删除

请求示例:

curl -XDELETE ‘http://localhost:9200/students/student/1’  -d‘

curl -XDELETE localhost:9200/students -d

释义:

第一个是删除students索引,类型为student,ID为1的文档

第二个是直接删除students索引

json返回:

{
  "_index": "students",
  "_type": "student",
  "_id": "1",
  "_version": 2,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 2
}

 

四、搜索

 REST API可以使用_search端点来实现搜索

  • 查询students索引下所有文档数据,按age升序排序
http://192.168.0.111:9200/students/_search?sort=age:asc       
  • 查询students索引下类型为student的所有文档数据
http://192.168.0.111:9200/students/student/_search  

全文检索-Elasticsearch (二) CURD与DSL

标签:ota   相同   event   failed   custom   class   success   应该   移除   

原文地址:https://www.cnblogs.com/qiuguochao/p/9069975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!