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

ElasticSearch入门学习-基础示例(1)

时间:2018-01-22 12:01:11      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:utf-8   表示   ted   没有   对象类型   false   created   标题   自动生成   

基于 ElasticSearch-6.1.2


关于文档元数据


参考官方权威指南 文档元数据

一个文档有三个必须的元数据元素:


  • _index:表示文档存放在哪个 index 中;

  • _type:文档表示的对象类型;

  • _id:文档的唯一标识;


1. 索引新文档


通过使用 index API,使得文档可以被 索引 -- 即存储文档,以及使得文档可以被搜索。


1.1 使用自定义的ID


以下为索引一篇blog的例子,其中:index 为 website,类型为 blog,自定义的ID为 123


PUT /website/blog/123 HTTP/1.1
{    "date": "2014/01/01", 
    "text": "Still trying this out...", 
    "title": "My second blog entry"}


ES 的响应体如下:


HTTP/1.1 201 Created
Location: /website/blog/123
content-encoding: gzip
content-length: 143
content-type: application/json; charset=UTF-8

{    "_id": "123", 
    "_index": "website", 
    "_primary_term": 1, 
    "_seq_no": 0, 
    "_shards": {        "failed": 0, 
        "successful": 1, 
        "total": 2
    }, 
    "_type": "blog", 
    "_version": 1, 
    "result": "created"
}


在 ES 中,每个文档都会有个版本号(响应中的 _version 字段),每次修改和删除,_version 都会自增。


1.2 使用 ES 自动生成的ID


采用 POST 提交索引请求:


POST /website/blog/ HTTP/1.1

{    "date": "2014/01/01", 
    "text": "Still trying this out...", 
    "title": "My second blog entry"
}


如下为 ES 的响应:


HTTP/1.1 201 Created
Location: /website/blog/UALcG2EBr-dnzPFB0zH1
content-encoding: gzip
content-length: 165
content-type: application/json; charset=UTF-8

{    "_id": "UALcG2EBr-dnzPFB0zH1", 
    "_index": "website", 
    "_primary_term": 1, 
    "_seq_no": 0, 
    "_shards": {        "failed": 0, 
        "successful": 1, 
        "total": 2
    }, 
    "_type": "blog", 
    "_version": 1, 
    "result": "created"}


除了 _id 是 ES 自动生成的之外,其他响应字段都和上面的类似。


自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零。


2. 检索文档


2.1 检索指定ID的文档


查询 ID 为 123 的 blog 的请求:


GET /website/blog/123?pretty HTTP/1.1

请求后面的 pretty 参数使得 ES 在输出时调用 prety-print 功能,使得 JSON 响应体更加可读。


ES 响应如下:


HTTP/1.1 200 OK
content-encoding: gzip
content-length: 173
content-type: application/json; charset=UTF-8

{    "_id": "123", 
    "_index": "website", 
    "_source": {        "date": "2014/01/01", 
        "text": "Still trying this out...", 
        "title": "My second blog entry"
    }, 
    "_type": "blog", 
    "_version": 1, 
    "found": true}


响应体中的 {"found": true} 表示文档已经检索到,如果没有指定的文档,则会返回 found = false,如下:


GET /website/blog/124?pretty HTTP/1.1

HTTP/1.1 404 Not Found
content-encoding: gzip
content-length: 87
content-type: application/json; charset=UTF-8

{    "_id": "124", 
    "_index": "website", 
    "_type": "blog", 
    "found": false}


2.2 返回文档的一部分


如下只返回 blog 的标题字段,而不是默认的返回所有字段:


GET /website/blog/123?pretty&_source=title HTTP/1.1

HTTP/1.1 200 OK
content-encoding: gzip
content-length: 136
content-type: application/json; charset=UTF-8

{    "_id": "123", 
    "_index": "website", 
    "_source": {        "title": "My second blog entry"
    }, 
    "_type": "blog", 
    "_version": 1, 
    "found": true}


2.3 只返回文档内容,不需要返回元信息


GET /website/blog/123/_source?pretty HTTP/1.1

HTTP/1.1 200 OK
content-encoding: gzip
content-length: 113
content-type: application/json; charset=UTF-8

{    "date": "2014/01/01", 
    "text": "Still trying this out...", 
    "title": "My second blog entry"}

ElasticSearch入门学习-基础示例(1)

标签:utf-8   表示   ted   没有   对象类型   false   created   标题   自动生成   

原文地址:http://blog.51cto.com/quietmadman/2063586

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