es

创建索引

PUT /liysh
{
  "settings":{
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
}

查看索引

GET /liysh
GET *

删除索引

DELETE /liysh1

创建映射字段

后期可以随意添加字段

text表示分词 keyword表示不可以分词

PUT liysh/_mapping/goods
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "images": {
      "type": "keyword",
      "index": "false"
    },
    "price": {
      "type": "float"
    }
  }
}

查看映射关系

GET /liysh/_mapping

新增数据

随机生成id

POST /liysh/goods/1
{
    "title":"小米手机",
    "images":"http://image.tcat.com/12479122.jpg",
    "price":2699.00
}

指定id

POST /liysh/goods/2
{
    "title":"大米手机",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":2899.00,
    "sleable":true   #增加的字段
}
修改数据
PUT /liysh/goods/2
{
    "title":"小米电视4A",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":3899.00
}

删除数据

DELETE liysh/goods/2

查询

查询所有

GET /liysh/_search
{
  "query":{
        "match_all":{}
    }
}

匹配查询

GET /liysh/_search
{
  "query":{
        "match":{
            "title":"小米电视"
        }
    }
}

查询结果是title中包含"小米"和"电视"的数据

GET /liysh/_search
{
    "query":{
        "match": {
          "title": {
            "query": "小米电视",
            "operator": "and"
          }
        }
    }
}

查询结果是title中包含"小米电视"的数据

GET /liysh/_search
{
    "query":{
        "match":{
            "title":{
            	"query":"小米曲面电视",
            	"minimum_should_match": "75%"
            }
        }
    }
}

查询结果是title中包含"小米"、“曲面”、"电视"三个词中的两个的数据

多字段查询

GET /liysh/_search
{
    "query":{
        "multi_match": {
            "query":    "小米",
            "fields":   [ "title", "subTitle" ]
        }
	}
}

查询结果为title或subTitle中包含"小米"的数据

精确匹配

GET /liysh/_search
{
    "query":{
        "term":{
            "price":2699.00
        }
    }
}

查询结果为price为2699的数据

GET /liysh/_search
{
    "query":{
        "term":{
            "title":"小米手机"
        }
    }
}

查询结果为小米手机的结果,不可分割小米手机

GET /liysh/_search
{
    "query":{
        "terms":{
            "price":[2699.00,2899.00,3899.00]
        }
    }
}

查询结果为price为2699、2899、3899 的数据

结果过滤

GET /liysh/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "price": 2699
    }
  }
}

查询结果为price为2699的数据的title信息和price信息

指定要显示的字段和不显示的字段

GET /liysh/_search
{
  "_source": {
    "includes":["title","price"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}
GET /liysh/_search
{
  "_source": {
     "excludes": ["images"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}

布尔组合

GET /liysh/_search
{
    "query":{
        "bool":{
        	"must":     { "match": { "title": "大米" }},
        	"must_not": { "match": { "title":  "电视" }},
        	"should":   { "match": { "title": "手机" }}
        }
    }
}

查询结果为title字段必须有大米,不能有电视、可以有手机

范围查询

gt 大于 gte 大于等于 it 小于 ite 小于等于

GET /liysh/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  1000.0,
                "lt":   2800.00
            }
    	}
    }
}

查询结果为price在1000到2800之间的数据

模糊查询

GET /heima/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}

查询结果为与appla偏差的编辑距离不超过2的数据(如apple)

GET /liysh/_search
{
  "query": {
    "fuzzy": {
        "title": {
            "value":"appla",
            "fuzziness":1
        }
    }
  }
}

这里可以用fuzziness来指定允许的编辑距离

过滤

GET /liysh/_search
{
    "query":{
        "bool":{
        	"must":{ "match": { "title": "小米手机" }},
        	"filter":{
                "range":{"price":{"gt":2000.00,"lt":3800.00}}
        	}
        }
    }
}

查询结果为title必须包含小米或手机 ,price在2000到3800之间的数据

GET /liysh/_search
{
    "query":{
        "constant_score":   {
            "filter": {
            	 "range":{"price":{"gt":2000.00,"lt":3000.00}}
            }
        }
      }
}

查询结果为价格在2000到3000之间的数据

排序

单个字段排序

GET /liysh/_search
{
  "query": {
    "match": {
      "title": "小米手机"
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

查询结果为title中包含小米或手机的数据按价格降序排列

多字段排序

GET /goods/_search
{
    "query":{
        "bool":{
        	"must":{ "match": { "title": "小米手机" }},
        	"filter":{
                "range":{"price":{"gt":200000,"lt":300000}}
        	}
        }
    },
    "sort": [
      { "price": { "order": "desc" }},
      { "_score": { "order": "desc" }}
    ]
}

聚合

桶:按照某种方式对数据进行分组,每一组数据在es中称为一个桶。

Elasticsearch中提供的划分桶的方式有很多∶
Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组Histogram Aggregation:根据数值阶梯分组,与日期类似
Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组
Range Aggregation:教值和日期的范围分组,指定开始和结束,然后按段分组

度量:分组完成以后,我们一般会对组中的数据进行聚合运算。如:最大、最小、求和。这些在es中称为度量

比较常用的一些度量聚合方式:.

AvgAggregation:求平均值.

​ Max Aggregation:求最大值

​ Min Aggregation:求最小值
​ Percentiles Aggregation:求百分比
​ Stats Aggregation:同时返回avg、max、min、sum、count等

​ Sum Aggregation:求和
​ Top hits Aggregation:求前几

​ Value Count Aggregation:求总数

聚合为桶

GET /cars/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            }
        }
    }
}
  • size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
  • aggs:声明这是一个聚合查询,是aggregations的缩写
    • popular_colors:给这次聚合起一个名字,任意。
      • terms:划分桶的方式,这里是根据词条划分
        • field:划分桶的字段

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4
        },
        {
          "key": "blue",
          "doc_count": 2
        },
        {
          "key": "green",
          "doc_count": 2
        }
      ]
    }
  }
}
  • hits:查询结果为空,因为我们设置了size为0
  • aggregations:聚合的结果
  • popular_colors:我们定义的聚合名称
  • buckets:查找到的桶,每个不同的color字段值都会形成一个桶
    • key:这个桶对应的color字段的值
    • doc_count:这个桶中的文档数量

桶内度量

现在,我们为刚刚的聚合结果添加 求价格平均值的度量:

GET /cars/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            },
            "aggs":{
                "avg_price": { 
                   "avg": {
                      "field": "price" 
                   }
                }
            }
        }
    }
}
  • aggs:我们在上一个aggs(popular_colors)中添加新的aggs。可见度量也是一个聚合
  • avg_price:聚合的名称
  • avg:度量的类型,这里是求平均值
  • field:度量运算的字段

结果:

...
  "aggregations": {
    "popular_colors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "red",
          "doc_count": 4,
          "avg_price": {
            "value": 32500
          }
        },
        {
          "key": "blue",
          "doc_count": 2,
          "avg_price": {
            "value": 20000
          }
        },
        {
          "key": "green",
          "doc_count": 2,
          "avg_price": {
            "value": 21000
          }
        }
      ]
    }
  }
...

可以看到每个桶中都有自己的avg_price字段,这是度量聚合的结果

桶内嵌套桶

比如:我们想统计每种颜色的汽车中,分别属于哪个制造商,按照make字段再进行分桶

GET /cars/_search
{
    "size" : 0,
    "aggs" : { 
        "popular_colors" : { 
            "terms" : { 
              "field" : "color"
            },
            "aggs":{
                "avg_price": { 
                   "avg": {
                      "field": "price" 
                   }
                },
                "maker":{
                    "terms":{
                        "field":"make"
                    }
                }
            }
        }
    }
}
  • 原来的color桶和avg计算我们不变
  • maker:在嵌套的aggs下新添一个桶,叫做maker
  • terms:桶的划分类型依然是词条
  • filed:这里根据make字段进行划分

阶梯分桶

比如,我们对汽车的价格进行分组,指定间隔interval为5000:

GET /cars/_search
{
  "size":0,
  "aggs":{
    "price":{
      "histogram": {
        "field": "price",
        "interval": 5000
      }
    }
  }
}

我们可以增加一个参数min_doc_count为1,来约束最少文档数量为1,这样文档数量为0的桶会被过滤

示例:

GET /cars/_search
{
  "size":0,
  "aggs":{
    "price":{
      "histogram": {
        "field": "price",
        "interval": 5000,
        "min_doc_count": 1
      }
    }
  }
}

范围分桶

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值