zoukankan      html  css  js  c++  java
  • elasticsearch增删改查操作

    1. 插入数据

    关于下面的代码如何使用,可以借助于kibana的console,浏览器打开地址:

        http://xxx.xxx.xxx.xxx:5601/app/kibana#/dev_tools/console?_g=()
    

    在console中输入代码,然后运行即可,也可以自己改成curl形式在命令行输入

    插入数据可以指定id或者不指定id

    1> 使用自定义的id

    使用put方式,并自己提供id

    类似于下面的格式

    PUT /{index}/{type}/{id}
    {
      "field": "value",
      ...
    }
    

    请求

    PUT /website/blog/123
    {
      "title": "My first blog entry",
      "text":  "Just trying this out...",
      "date":  "2014/01/01"
    }
    

    响应

    {
       "_index":    "website",
       "_type":     "blog",
       "_id":       "123",
       "_version":  1,
       "created":   true
    }
    

    在 Elasticsearch 中每个文档都有一个版本号。当每次对文档进行修改时(包括删除), _version 的值会递增。

    2> 自动生成id

    使用post方式

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

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

    2. 更改数据

    控制台输入

    PUT /website/blog/123
    {
      "title": "My first blog entry",
      "text":  "Just trying this out...",
      "date":  "2014/01/01"
    }
    

    在响应体中,我们能看到 Elasticsearch 已经增加了 _version 字段值,created 标志设置成 false ,是因为相同的索引、类型和 ID 的文档已经存在。

    {
      "_index": "website",
      "_type": "blog",
      "_id": "123",
      "_version": 2,
      "created": false
    }
    

    3. 删除数据

    DELETE /website/blog/123
    

    如果找到该文档,Elasticsearch 将要返回一个 200 ok 的 HTTP 响应码,和一个类似以下结构的响应体。注意,字段 _version 值已经增加:

    {
      "found" :    true,
      "_index" :   "website",
      "_type" :    "blog",
      "_id" :      "123",
      "_version" : 3
    }
    

    如果文档没有 找到,我们将得到 404 Not Found 的响应码和类似这样的响应体:

    {
      "found" :    false,
      "_index" :   "website",
      "_type" :    "blog",
      "_id" :      "123",
      "_version" : 4
    }
    

    4. 检索文档

    这里只是先简单的介绍下如何检索文档,后面会详细介绍这部分内容

    1> 检索id为1的员工

    在bibana的console中输入运行

    GET /megacorp/employee/1
    

    返回结果包含了文档的一些元数据,以及 _source 属性,内容是 John Smith 雇员的原始 JSON 文档:

    {
      "_index": "megacorp",
      "_type": "employee",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name": "John",
        "last_name": "Smith",
        "age": 25,
        "about": "I love to go rock climbing",
        "interests": [
          "sports",
          "music"
        ]
      }
    }
    

    2> 搜索所有雇员

    GET /megacorp/employee/_search
    

    返回结果包括了所有三个文档,放在数组 hits 中。一个搜索默认返回十条结果

    elasticsearch 提供了两种查询模式

    1> Query-string:通过url参数来搜索,被称为查询字符串搜索。

    GET /megacorp/employee/_search?q=last_name:Smith
    

    2> Query-DSL:使用查询表达式搜索,被称为DSL查询,它支持构建更加复杂和健壮的查询,一般来说我们重点学习这种方法。

    GET /megacorp/employee/_search
    {
        "query" : {
            "match" : {
                "last_name" : "Smith"
            }
        }
    }
    

    3> 更复杂的搜索

    GET /megacorp/employee/_search
    {
        "query" : {
            "bool": {
                "must": {
                    "match" : {
                        "last_name" : "smith" 
                    }
                },
                "filter": {
                    "range" : {
                        "age" : { "gt" : 30 } 
                    }
                }
            }
        }
    }
    

    注意这里搜索last_name是smith的人,同时年龄大于30。注意下语法bool里面有must,filter类型,当然以后还会学到更多类型,这里先有个意识。

    4> 全文搜索

    比如想要搜索下所有喜欢攀岩(rock climbing)的雇员

    GET /megacorp/employee/_search
    {
        "query" : {
            "match" : {
                "about" : "rock climbing"
            }
        }
    }
    
    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 2,
        "max_score": 0.53484553,
        "hits": [
          {
            "_index": "megacorp",
            "_type": "employee",
            "_id": "1",
            "_score": 0.53484553,
            "_source": {
              "first_name": "John",
              "last_name": "Smith",
              "age": 25,
              "about": "I love to go rock climbing",
              "interests": [
                "sports",
                "music"
              ]
            }
          },
          {
            "_index": "megacorp",
            "_type": "employee",
            "_id": "2",
            "_score": 0.26742277,
            "_source": {
              "first_name": "Jane",
              "last_name": "Smith",
              "age": 32,
              "about": "I like to collect rock albums",
              "interests": [
                "music"
              ]
            }
          }
        ]
      }
    }
    

    注意这里稍有不同,about字段中包含两个单词,搜索的结果并不是完全匹配,是根据单词去做了相关性匹配。

    Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。

    Elasticsearch中的 相关性 概念非常重要,也是完全区别于传统关系型数据库的一个概念,数据库中的一条记录要么匹配要么不匹配。

    5> 短语搜索

    上面如果一个短语包含多个单词,那岂不是不能精确查询了,当然不是,可以使用短语搜索。

    GET /megacorp/employee/_search
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        }
    }
    

    这样结果就是精确匹配了,仅匹配同时包含 “rock” 和 “climbing” ,并且 二者以短语 “rock climbing” 的形式紧挨着的结果

    6> 高亮搜索

    GET /megacorp/employee/_search
    {
        "query" : {
            "match_phrase" : {
                "about" : "rock climbing"
            }
        },
        "highlight": {
            "fields" : {
                "about" : {}
            }
        }
    }
    

    结果中多了一个highlight部分

    {
       ...
       "hits": {
          "total":      1,
          "max_score":  0.23013961,
          "hits": [
             {
                ...
                "_score":         0.23013961,
                "_source": {
                   "first_name":  "John",
                   "last_name":   "Smith",
                   "age":         25,
                   "about":       "I love to go rock climbing",
                   "interests": [ "sports", "music" ]
                },
                "highlight": {
                   "about": [
                      "I love to go <em>rock</em> <em>climbing</em>" 
                   ]
                }
             }
          ]
       }
    }
    

    7> 聚合

    聚合类似于SQL中的GROUP_BY,但功能更强大

    GET /megacorp/employee/_search
    {
      "aggs": {
        "all_interests": {
          "terms": { "field": "interests" }
        }
      }
    }
    

    结果

    {
       ...
       "hits": { ... },
       "aggregations": {
          "all_interests": {
             "buckets": [
                {
                   "key":       "music",
                   "doc_count": 2
                },
                {
                   "key":       "forestry",
                   "doc_count": 1
                },
                {
                   "key":       "sports",
                   "doc_count": 1
                }
             ]
          }
       }
    }
    

    以上是对所有的雇员进行统计,我们也可以其中的一部分雇员进行组合查询统计,比如我们想知道叫smith的雇员最受欢迎的兴趣爱好。

    GET /megacorp/employee/_search
    {
      "query": {
        "match": {
          "last_name": "smith"
        }
      },
      "aggs": {
        "all_interests": {
          "terms": {
            "field": "interests"
          }
        }
      }
    }
    

    聚合还支持分级汇总,查询特定兴趣爱好的 员工的平均年龄

    GET /megacorp/employee/_search
    {
        "aggs" : {
            "all_interests" : {
                "terms" : { "field" : "interests" },
                "aggs" : {
                    "avg_age" : {
                        "avg" : { "field" : "age" }
                    }
                }
            }
        }
    }
    

    结果

      ...
      "all_interests": {
         "buckets": [
            {
               "key": "music",
               "doc_count": 2,
               "avg_age": {
                  "value": 28.5
               }
            },
            {
               "key": "forestry",
               "doc_count": 1,
               "avg_age": {
                  "value": 35
               }
            },
            {
               "key": "sports",
               "doc_count": 1,
               "avg_age": {
                  "value": 25
               }
            }
         ]
      }
    
  • 相关阅读:
    【贪心】【堆】Gym
    【并查集】Gym
    【拓扑排序】【bitset】Gym
    【递归】【线段树】【堆】AtCoder Regular Contest 080 E
    【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel
    【动态规划】【滚动数组】【bitset】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal
    【二分】【字符串哈希】【二分图最大匹配】【最大流】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem I. Minimum Prefix
    【枚举】【最小表示法】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem F. Matrix Game
    【推导】【构造】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem E. Space Tourists
    【推导】【贪心】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures
  • 原文地址:https://www.cnblogs.com/redirect/p/10066794.html
Copyright © 2011-2022 走看看