ElasticSearch学习笔记(4)——基本操作

一. 数据操作

  1. 插入数据

    向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录:

    PUT localhost:9200/accounts/person/1 
    {
     "user": "张三",
     "title": "工程师",
     "desc": "数据库管理"
    }

    服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息:

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

    如果你仔细看,会发现请求路径是/accounts/person/1,最后的1是该条记录的 Id。它不一定是数字,任意字符串(比如abc)都可以。

    新增记录的时候,也可以不指定 Id,这时要改成 POST 请求:

    POST localhost:9200/accounts/person
    {
     "user": "李四",
     "title": "工程师",
     "desc": "系统管理"
    }

    上面代码中,向/accounts/person发出一个 POST 请求,添加一个记录。这时,服务器返回的 JSON 对象里面,_id字段就是一个随机字符串:

    {
       "_index": "accounts",
       "_type": "person",
       "_id": "3CEKZmUBGSRFZPmXxRma",
       "_version": 1,
       "result": "created",
       "_shards": {
           "total": 2,
           "successful": 2,
           "failed": 0
       },
       "_seq_no": 2,
       "_primary_term": 1
    }

    注意,如果没有先创建 Index,直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,插入数据的时候要小心,不要写错 Index 的名称。

  2. 查询数据

    向/Index/Type/Id发出 GET 请求,就可以查看这条记录。

    GET localhost:9200/accounts/person/1?pretty=true

    上面代码请求查看/accounts/person/1这条记录,URL 的参数pretty=true表示以易读的格式返回。

    返回的数据中,_found字段表示查询成功,__source字段返回原始记录。

    {
       "_index": "accounts",
       "_type": "person",
       "_id": "1",
       "_version": 2,
       "found": true,
       "_source": {
           "user": "张三",
           "title": "工程师",
           "desc": "数据库管理"
       }
    }

    如果 Id 不正确,就查不到数据,_found字段就是false。

    {
       "_index": "accounts",
       "_type": "person",
       "_id": "2",
       "found": false
    }
  3. 删除数据

    删除记录就是发出 DELETE 请求:

    DELETE localhost:9200/accounts/person/1
    {
       "_index": "accounts",
       "_type": "person",
       "_id": "1",
       "_version": 3,
       "result": "deleted",
       "_shards": {
           "total": 2,
           "successful": 2,
           "failed": 0
       },
       "_seq_no": 3,
       "_primary_term": 1
    }
  4. 更新数据

    更新记录就是使用 PUT 请求,重新发送一次数据:

    PUT localhost:9200/accounts/person/1
    {
       "user" : "张三",
       "title" : "工程师",
       "desc" : "数据库管理,软件开发"
    }
    

    插入数据时,如果数据不存在,会新建一条记录。可以看到返回值中,result为created表示新建

    {
       "_index": "accounts",
       "_type": "person",
       "_id": "1",
       "_version": 1,
       "result": "created",
       "_shards": {
           "total": 2,
           "successful": 2,
           "failed": 0
       },
       "_seq_no": 4,
       "_primary_term": 1
    }

二. 数据查询

  1. 查询所有数据

    使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录:

    localhost:9200/accounts/person/_search
    {
       "took": 261,
       "timed_out": false,
       "_shards": {
           "total": 5,
           "successful": 5,
           "skipped": 0,
           "failed": 0
       },
       "hits": {
           "total": 2,
           "max_score": 1,
           "hits": [
               {
                   "_index": "accounts",
                   "_type": "person",
                   "_id": "3CEKZmUBGSRFZPmXxRma",
                   "_score": 1,
                   "_source": {
                       "user": "李四",
                       "title": "工程师",
                       "desc": "系统管理"
                   }
               },
               {
                   "_index": "accounts",
                   "_type": "person",
                   "_id": "1",
                   "_score": 1,
                   "_source": {
                       "user": "张三",
                       "title": "工程师",
                       "desc": "数据库管理,软件开发"
                   }
               }
           ]
       }
    }

    上面代码中,返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义如下:

    • total:返回记录数,本例是2条。
    • max_score:最高的匹配程度,本例是1.0。
    • hits:返回的记录组成的数组。

    返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。

  2. 全文检索

    Elastic 的查询非常特别,使用自己的查询语法,要求POST请求带有数据体。

    这里只做简单演示,详细的查询方法请参考官方DSL文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl.html

    POST localhost:9200/accounts/person/_search
    {
     "query" : { "match" : { "desc" : "软件" }}
    }

    上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含”软件”这个词。返回结果如下:

    {
       "took": 256,
       "timed_out": false,
       "_shards": {
           "total": 5,
           "successful": 5,
           "skipped": 0,
           "failed": 0
       },
       "hits": {
           "total": 1,
           "max_score": 1.1978253,
           "hits": [
               {
                   "_index": "accounts",
                   "_type": "person",
                   "_id": "1",
                   "_score": 1.1978253,
                   "_source": {
                       "user": "张三",
                       "title": "工程师",
                       "desc": "数据库管理,软件开发"
                   }
               }
           ]
       }
    }

    Elastic 默认一次返回10条结果,可以通过size字段改变这个设置:

    POST localhost:9200/accounts/person/_search
    {
     "query" : { "match" : { "desc" : "软件" }},
     "size": 1
    }

    还可以通过from字段,指定位移:

    POST localhost:9200/accounts/person/_search
    {
     "query" : { "match" : { "desc" : "软件" }},
     "size": 1,
     "from": 1
    }

    上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。

猜你喜欢

转载自blog.csdn.net/weixin_34452850/article/details/81985400