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" : "[email protected]",
          "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" : [
            "Low Tide Media"
          ]
        }
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" : [
            "Men's Clothing",
            "Men's Shoes",
            "Men's Clothing",
            "Men's Clothing"
          ],
          "manufacturer" : [
            "Low Tide Media"
          ]
        }

聚合搜索

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
          }
        }
      ]
    }
  }

你可能感兴趣的:(Elasticsearch学习笔记)