【笔记】Elasticsearch——黑马程序员(乐优商城)

基本概念

Elasticsearch是基于Lucene的全文检索库,本质也是存储数据,很多概念与MYSQL类似
对比关系:

ElasticsearchMySQL
indices(索引库)Databases(数据库)
type(类型)Table(数据表)
Document(文档)Row(行)
Field(字段)Columns(列)

详细说明:

概念说明
索引库(indices)indices是index的复数,代表许多索引
类型(type)类型是模拟mysql中table概念,一个索引库下可以有不同类型的索引,商品索引,订单索引,其数据格式不同,不过这会导致索引库混乱,因此未来版本中会移除这个概念
文档(document)存入索引库原始的数据。比如每一条商品信息,就是一个文档
字段(field)文档中的属性
映射配置(mappings)字段的数据类型,属性,是否索引,是否存储等特性

创建数据库及索引

创建及查看索引库

# 创建索引库
PUT heima
{
  "settings": {
    "number_of_shards": 1,    # 分词数量
    "number_of_replicas": 0   # 副本数量
  }  
}

# 查看索引库
GET heima

创建及查看索引

# 创建索引
PUT heima/_mapping/goods
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "ik_max_word"
    },
    "images": {
      "type": "keyword",
      "index": "false"
    },
    "price": {
      "type": "float" 
    }
  }
}

# 查看索引
GET heima/_mapping

# 创建索引库并创建映射
PUT /cars
{
  "settings": {
    "number_of_shards": 1,    # 分词数量
    "number_of_replicas": 0   # 副本数量
  },
	"mappings": {
		"transactions": {
			"properties": {
				"color": { "type": "keyword" },
				"make": { "type": "keyword" }
			}
		}
	} 
}

查看所有索引库

GET _all/_settings

数据管理

新增数据

POST /heima/goods/
{
	"title": "小米手机",
	"images": "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1618309945,4014036594&fm=26&gp=0.jpg",
	"price": 2699.00
}

# 指定数据ID
POST /heima/goods/1
{
	"title": "红米手机",
	"images": "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1719187876,1243668359&fm=26&gp=0.jpg",
	"price": 3000.00
}

查询数据

# 查询索引库中所有数据
GET /heima/_search

修改数据

PUT /heima/goods/1
{
	"title": "百米手机",
	"images": "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1719187876,1243668359&fm=26&gp=0.jpg",
	"price": 3100.00
}

删除数据

DELETE /heima/goods/3

查询

  1. 基本语法
GET /索引库名/_search
{
	"query": {
		"查询类型": {
			"查询条件": "查询条件值"
		}
	}
}

基本查询

查询所有 (match_all)

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

匹配查询 (match)

GET /heima/_search
{
	"query": {
		"match": {
			"title": "小米"
		}
	}
}

# 可以输入匹配度
GET /heima/_search
{
	"query": {
		"match": {
			"title": {
				"query": "小米大手机",
				"minimum_should_match": "20%"  # 最小匹配度
			}
		}
	}
}

多字段查询 (multi_match)

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

词条查询 (term)

词条的值是不可分割的单位

GET /heima/_search
{
	"query": {
		"term": {
			"title": {
				"value": "手机"	
			}
		}
	}
}

多词条查询 (terms)

GET /heima/_search
{
	"query": {
		"terms": {
			"title": ["大米", "手机"]
		}
	}
}

结果集过滤

# 查询结果只返回title, price
GET /heima/_search
{
	"_source": ["title", "price"],
	"query": {
		"terms": {
			"title": ["大米", "手机"]
		}
	}
}

# 返回结果不包含字段title:
GET /heima/_search
{
	"_source": {
		"excludes": ["title"]
	},
	"query": {
		"terms": {
			"title": ["大米", "手机"]
		}
	}
}

高级查询

布尔组合 (bool)

bool把各种其他查询通过must(与),must_not(非),should(或)的方式进行组合

# 查询价格为9999的电视
GET /heima/_search
{
	"query": {
		"bool": {
			"must": [
				{
					"match": { "title": "电视" }
				},{
					"term": {
						"price"{ "value": 9999 }
					}
				}
			]
		}
	}
}

范围查询 (range)

# 查询价格在2000至5000的商品
GET /heima/_search
{
	"query": {
		"range": {
			"price": {
				"gt": 2000,
				"lt": 5000
			}
		}
	}
}

模糊查询 (fuzzy)

# 查询关键字为手机的商品
GET /heima/_search
{
	"query": {
		"fuzzy": {
			"title": "电视"
		}
	}
}

# 修改命中次数的查询
GET /heima/_search
{
	"query": {
		"fuzzy": {
			"title": {
				"value": "电视",
				"fuzziness": 1
			}
		}
	}
}

过滤

所有查询都会影响文档的评分及排名,如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么久不要把过滤条件作为查询条件来用。而是使用filter方式

GET /heima/_search
{
	"query": {
		"bool": {
			"must": [
				{
					"match": { "title": "手机" }
				}
			],
			"filter": {
				"range": {
					"price": { "gte": 2000, "lte": 5000 }
				}
			}
		}
	}
}

排序

# 描述:查询手机,并过滤出2000至5000的手机,结果以价格降序进行排列, 如果价格相同以id降序排列
GET /heima/_search
{
	"query": {
		"bool": {
			"must": [
				{
					"match": { "title": "手机" }
				}
			],
			"filter": {
				"range": {
					"price": { "gte": 2000, "lte": 5000 }
				}
			}
		}
	},
	"sort": [
		{"price" : { "order": "desc" }},
		{"_id": { "order": "desc" }}
	]
}

聚合

基本概念

elasticsearch中的聚合,包含多种类型,最常用的两种,一个叫桶,一个叫度量

  1. 桶(bucket)
  • 桶的作用是,按照某种方式对数据进行分组,每一组数据在ES中称为一个桶
  • bucket aggregations只负责对数据进行分组,并不进行计算
  1. 度量(metrics)
    分组完成以后,我们一般会对组中数据进行聚合计算
# 描述:查询所有的颜色
GET /cars/_search
{
	"aggs": {
		"xx_color": {
			"terms": { "field": "color" }
		}
	}
}

# 查询所有颜色的数据
GET /cars/_search
{
	"size": 0,
	"aggs": {
		"xx_color": {
			"terms": { "field": "color" }
		}
	}
}

# 查询所有颜色的价格平均值
GET /cars/_search
{
    "aggs":{
        "xx_color":{
            "terms":{
                "field":"color"
            },
            "aggs":{
                "price_avg":{
                    "avg":{
                        "field":"price"
                    }
                }
            }
        }
    }
}

# 查询阶梯价格区间的数据
GET /cars/_search
{
    "size": 0,
    "aggs": {
        "price_histo": {
            "histogram": {
                "field": "price",
                "interval": 1000,
                "min_doc_count": 1			# 可选,返回结果最少为1条的聚合数据
            }
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值