Elasticsearch学习笔记

kibana

添加索引

PUT /pet/
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    }
  }
}

返回结果

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "pet"
}

获取索引信息

GET /pet/_settings

返回结果

{
  "pet" : {
    "settings" : {
      "index" : {
        "creation_date" : "1575006677881",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "-oax1qVFSpaEkEfjjDQH5w",
        "version" : {
          "created" : "7040299"
        },
        "provided_name" : "pet"
      }
    }
  }
}

添加记录

PUT /users/_create/1
{
  "nickName": "风来了",
  "title": "萌新"
}

返回结果

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
GET /users/_doc/1

返回结果

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "nickName" : "风来了",
    "title" : "萌新"
  }
}

更新记录

POST /users/_update/1
{
  "doc": {
    "title": "大佬"
  }
}

返回结果

{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "noop",
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

以下使用kibana测试数据 kibana_sample_data_ecommerce

查看所有记录

GET kibana_sample_data_ecommerce/_search

简单搜索

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "customer_first_name": "Eddie"
    }
  }
}

返回结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 100,
      "relation" : "eq"
    },
    "max_score" : 4.016948,
    "hits" : [
      {
        "_index" : "kibana_sample_data_ecommerce",
        "_type" : "_doc",
        "_id" : "LoyZtW4BmmBBunVzacA7",
        "_score" : 4.016948,
        "_source" : {
          "category" : [
            "Men's Clothing"
          ],
          "currency" : "EUR",
          "customer_first_name" : "Eddie",
          "customer_full_name" : "Eddie Underwood",
          "customer_gender" : "MALE",
          "customer_id" : 38,
          "customer_last_name" : "Underwood",
          "customer_phone" : "",
          "day_of_week" : "Monday",
          "day_of_week_i" : 0,
          "email" : "eddie@underwood-family.zzz",
          "manufacturer" : [
            "Elitelligence",
            "Oceanavigations"
          ],
          "order_date" : "2019-12-16T09:28:48+00:00",
          "order_id" : 584677,
          "products" : [
            {
              "base_price" : 11.99,
              "discount_percentage" : 0,
              "quantity" : 1,
              "manufacturer" : "Elitelligence",
              "tax_amount" : 0,
              "product_id" : 6283,
              "category" : "Men's Clothing",
              "sku" : "ZO0549605496",
              "taxless_price" : 11.99,
              "unit_discount_amount" : 0,
              "min_price" : 6.35,
              "_id" : "sold_product_584677_6283",
              "discount_amount" : 0,
              "created_on" : "2016-12-26T09:28:48+00:00",
              "product_name" : "Basic T-shirt - dark blue/white",
              "price" : 11.99,
              "taxful_price" : 11.99,
              "base_unit_price" : 11.99
            },
            {
              "base_price" : 24.99,
              "discount_percentage" : 0,
              "quantity" : 1,
              "manufacturer" : "Oceanavigations",
              "tax_amount" : 0,
              "product_id" : 19400,
              "category" : "Men's Clothing",
              "sku" : "ZO0299602996",
              "taxless_price" : 24.99,
              "unit_discount_amount" : 0,
              "min_price" : 11.75,
              "_id" : "sold_product_584677_19400",
              "discount_amount" : 0,
              "created_on" : "2016-12-26T09:28:48+00:00",
              "product_name" : "Sweatshirt - grey multicolor",
              "price" : 24.99,
              "taxful_price" : 24.99,
              "base_unit_price" : 24.99
            }
          ],
          "sku" : [
            "ZO0549605496",
            "ZO0299602996"
          ],
          "taxful_total_price" : 36.98,
          "taxless_total_price" : 36.98,
          "total_quantity" : 2,
          "total_unique_products" : 2,
          "type" : "order",
          "user" : "eddie",
          "geoip" : {
            "country_iso_code" : "EG",
            "location" : {
              "lon" : 31.3,
              "lat" : 30.1
            },
            "region_name" : "Cairo Governorate",
            "continent_name" : "Africa",
            "city_name" : "Cairo"
          }
        }
      },
  
}

复杂搜索

查找first_name为Eddie,并且order_date>="2019-12-16T09:28:48+00:00"

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "customer_first_name": "Eddie"
        }
      },
      "filter": {
        "range": {
          "order_date":{
            "gte": "2019-12-16T09:28:48+00:00"
          }
        }
      }
    }
  }
}

查找厂商含有Low 和 Media的记录
"manufacturer" : [
"Low Tide Media"
],

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "manufacturer": "Low Media"
    }
  }
}

短语搜索

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match_phrase": {
      "manufacturer": "Low Media"
    }
  }
}

无法找到Low Media的短语

高亮搜索

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "manufacturer": "Low Media"
    }
  },
  "highlight": {
    "fields": {
      "manufacturer": {}
    }
  }
}

部分返回结果

"_source" :{},
"highlight" : {
          "manufacturer" : [
            "<em>Low</em> Tide <em>Media</em>"
          ]
        }
GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "manufacturer": "Low Media"
          }
        },
        {
          "match": {
            "products.category": "Men's"
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "manufacturer": {},
      "products.category": {}
    }
  }
}

部分返回结果

"highlight" : {
          "products.category" : [
            "<em>Men's</em> Clothing",
            "<em>Men's</em> Shoes",
            "<em>Men's</em> Clothing",
            "<em>Men's</em> Clothing"
          ],
          "manufacturer" : [
            "<em>Low</em> Tide <em>Media</em>"
          ]
        }

聚合搜索

GROUP BY

GET kibana_sample_data_ecommerce/_search
{
  "aggs": {
    "all_categories": {
      "terms": {
        "field": "category.keyword",
        "size": 10
      }
    }
  }
}

部分返回结果

"aggregations" : {
    "all_categories" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Men's Clothing",
          "doc_count" : 2024
        },
        {
          "key" : "Women's Clothing",
          "doc_count" : 1903
        },
        {
          "key" : "Women's Shoes",
          "doc_count" : 1136
        },
        {
          "key" : "Men's Shoes",
          "doc_count" : 944
        },
        {
          "key" : "Women's Accessories",
          "doc_count" : 830
        },
        {
          "key" : "Men's Accessories",
          "doc_count" : 572
        }
      ]
    }
  }

查看名为Eddie的顾客的分类

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "customer_first_name": "Eddie"
    }
  }, 
  "aggs": {
    "all_categories": {
      "terms": {
        "field": "category.keyword",
        "size": 10
      }
    }
  }
}

部分返回结果

"aggregations" : {
    "all_categories" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Men's Clothing",
          "doc_count" : 91
        },
        {
          "key" : "Men's Shoes",
          "doc_count" : 40
        },
        {
          "key" : "Men's Accessories",
          "doc_count" : 25
        },
        {
          "key" : "Women's Accessories",
          "doc_count" : 10
        }
      ]
    }
  }

查看名为Eddie的特定厂商的基本价的平均值

GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match": {
      "customer_first_name": "Eddie"
    }
  }, 
  "aggs": {
    "all_categories": {
      "terms": {
        "field": "manufacturer.keyword",
        "size": 10
      },
      "aggs": {
        "avg_base_price": {
          "avg": {
            "field": "products.base_price"
          }
        }
      }
    }
  }
}

部分返回结果

"aggregations" : {
    "all_categories" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "Low Tide Media",
          "doc_count" : 68,
          "avg_base_price" : {
            "value" : 35.142938410194176
          }
        },
        {
          "key" : "Elitelligence",
          "doc_count" : 64,
          "avg_base_price" : {
            "value" : 31.453706781914892
          }
        },
        {
          "key" : "Oceanavigations",
          "doc_count" : 35,
          "avg_base_price" : {
            "value" : 37.85217927631579
          }
        },
        {
          "key" : "Microlutions",
          "doc_count" : 21,
          "avg_base_price" : {
            "value" : 35.28194304435484
          }
        },
        {
          "key" : "Spritechnologies",
          "doc_count" : 16,
          "avg_base_price" : {
            "value" : 35.200032552083336
          }
        },
        {
          "key" : "Angeldale",
          "doc_count" : 15,
          "avg_base_price" : {
            "value" : 49.162272135416664
          }
        },
        {
          "key" : "Spherecords",
          "doc_count" : 5,
          "avg_base_price" : {
            "value" : 28.134486607142858
          }
        },
        {
          "key" : "",
          "doc_count" : 2,
          "avg_base_price" : {
            "value" : 72.99609375
          }
        }
      ]
    }
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容