Elasticsearch是基于Lucene的全文检索库,本质也是存储数据,很多概念与MYSQL类似
对比关系:
Elasticsearch | MySQL |
---|---|
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" }
}
}
}
}
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
GET /索引库名/_search
{
"query": {
"查询类型": {
"查询条件": "查询条件值"
}
}
}
GET /heima/_search
{
"query": {
"match_all": {}
}
}
GET /heima/_search
{
"query": {
"match": {
"title": "小米"
}
}
}
# 可以输入匹配度
GET /heima/_search
{
"query": {
"match": {
"title": {
"query": "小米大手机",
"minimum_should_match": "20%" # 最小匹配度
}
}
}
}
GET /heima/_search
{
"query": {
"multi_match": {
"query": "超米",
"fields": ["title", "subTitle"]
}
}
}
词条的值是不可分割的单位
GET /heima/_search
{
"query": {
"term": {
"title": {
"value": "手机"
}
}
}
}
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把各种其他查询通过must(与),must_not(非),should(或)的方式进行组合
# 查询价格为9999的电视
GET /heima/_search
{
"query": {
"bool": {
"must": [
{
"match": { "title": "电视" }
},{
"term": {
"price":{ "value": 9999 }
}
}
]
}
}
}
# 查询价格在2000至5000的商品
GET /heima/_search
{
"query": {
"range": {
"price": {
"gt": 2000,
"lt": 5000
}
}
}
}
# 查询关键字为手机的商品
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中的聚合,包含多种类型,最常用的两种,一个叫桶,一个叫度量
# 描述:查询所有的颜色
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条的聚合数据
}
}
}
}