一、基本概念

Cluster:集群, 由一组 es 实例组成

Node:节点, 单个 es 运行的实例

Index:索引, 类似于数据表

Type 类型ES7以后固定值是 _doc

Document: 文档, 类似于数据表中的一条数据

Field: 字段, 类似于数据表的字段

Mapping:映射, 类似于数据表的字段定义, 包括数据类型

二、索引-增删改查

2.1 创建索引,PUT请求,不能重复添加

http://192.168.0.104:9200/shopping

2.2 全查索引,GET请求

http://192.168.0.104:9200/_cat/indices?v

2.3 单个索引查询,GET请求

http://192.168.0.104:9200/shopping

2.4 删除索引,DELETE

http://192.168.0.104:9200/shopping

三、文档-增删改查

3.1 创建文档,POST请求

// 随机主键id
http://192.168.0.104:9200/shopping/_doc/

// 指定主键
http://192.168.0.104:9200/shopping/_doc/1

3.2 查询文档,GET请求

// 主键查询
http://192.168.0.104:9200/shopping/_doc/1

// 全查询
http://192.168.0.104:9200/shopping/_doc/_search

3.3 修改文档,POST请求

http://192.168.0.104:9200/shopping/_doc/1
// 全量修改的json参数
{
    "title": "苹果13",
    "category": "苹果",
    "price": 6999.00
}

// 局部修改的json参数
http://192.168.0.104:9200/shopping/_update/1
{
   "doc": {
        "title": "苹果13",
        "price": 6998.00
   } 
}

四、常见查询

4.1 条件查询,GET请求

http://192.168.0.104:9200/shopping/_search
// 查询category为苹果的文档,json参数
{
	"query":{
		"match":{
			"category":"苹果"
		}
	}
}

// 全条件查询
{
	"query":{
		"match_all":{}
	}
}

// 查询某一个字段
{
	"query":{
		"match_all":{}
	},
	"_source":["title"]
}

// 分页查询
{
	"query":{
		"match_all":{}
	},
	"from":0,
	"size":3
}

// 排序
{
	"query":{
		"match_all":{}
	},
	"sort":{
		"price":{
			"order":"desc"
		}
	}
}

// 多条件查询,must类似&& 
{
	"query":{
		"bool":{
			"must":[{
				"match":{
					"category":"苹果"
				}
			},{
				"match":{
					"price":8999.00
				}
			}]
		}
	}
}
// should类似||
{
	"query":{
		"bool":{
			"should":[{
				"match":{
					"category":"苹果"
				}
			},{
				"match":{
					"category":"华为"
				}
			}],
            "filter":{
                "range":{
                    "price":{
                        "gt":9000
                    }
                }
            }
		}
	}
}

五、全文检索、完全匹配、高亮查询

5.1 全文检索,类似我们百度搜索一样,输入“苹为”,可以查出苹果和华为的数据。

{
	"query":{
		"match":{
			"category" : "苹为"
		}
	}
}

5.2 完全匹配,只会筛选出苹果的数据

{
	"query":{
		"match_phrase":{
			"category" : "苹"
		}
	}
}

5.3 高亮查询

{
	"query":{
		"match_phrase":{
			"category" : "苹"
		}
	},
    "highlight":{
        "fields":{
            "category":{}//<----高亮这字段
        }
    }
}

返回结果中苹字被高亮展示

"<em>苹</em>果"

六、聚合查询(类似数据库的聚合查询)

6.1 分组

按照price分组

{
	"aggs":{//聚合操作
		"price_group":{//名称,随意起名
			"terms":{//分组
				"field":"price"//分组字段
			}
		}
	}
}

// 不带原始数据返回
{
	"aggs":{
		"price_group":{
			"terms":{
				"field":"price"
			}
		}
	},
    "size":0
}

但是如果按照category分组

{
	"aggs":{//聚合操作
		"category_group":{//名称,随意起名
			"terms":{//分组
				"field":"category"//分组字段
			}
		}
	}
}

则报错

Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [category] in order to load field data by uninverting the inverted index. Note that this can use significant memory.

度娘之后大概的意思就是category这个字段在建立的时候没有进行优化,默认没有索引。没有优化的字段es默认是禁止聚合/排序操作的。但是category分词之后的keyword是有索引的,所以可以对category.keyword进行聚合。

正确写法

{
	"aggs":{//聚合操作
		"category_group":{//名称,随意起名
			"terms":{//分组
				"field":"category.keyword"//分组字段
			}
		}
	}
}

不建议的另外一种方式(添加映射,设置fielddata=ture,取消翻转索引,占用大量内存)

http://192.168.0.104:9200/shopping/_mapping
{
    "properties": {
        "category":{
        	"type": "text",
        	"fielddata": true
        }
        
    }
}

七、映射

7.1 创建user映射(PUT)

http://192.168.0.104:9200/user/_mapping

{
    "properties": {
        "name":{
        	"type": "text",
        	"index": true
        },
        "sex":{
        	"type": "keyword",
        	"index": true
        },
        "tel":{
        	"type": "keyword",
        	"index": false
        }
    }
}

7.2 查看映射(GET)

http://192.168.0.104:9200/user/_mapping

7.3 增加数据(doc、PUT)

http://192.168.0.104:9200/user/_create/1001
{
	"name":"小苏",
	"sex":"男的",
	"tel":"1111"
}

7.4 查找name里面含有“小”的数据

http://192.168.0.104:9200/user/_search
{
	"query":{
		"match":{
			"name":"小"
		}
	}
}

7.5 查找sex含有“男”的数据

http://192.168.0.104:9200/user/_search
{
	"query":{
		"match":{
			"sex":"男"
		}
	}
}

返回结果为空,因为创建映射时sex的类型为keyword。只有全匹配的时候才能查找出来。

http://192.168.0.104:9200/user/_search
{
	"query":{
		"match":{
			"sex":"男的"
		}
	}
}

7.6 查找tel为1111的数据

http://192.168.0.104:9200/user/_search
{
	"query":{
		"match":{
			"tel":"1111"
		}
	}
}

报错

Cannot search on field [tel] since it is not indexed.

原因是建立映射的时候,把tel字段把i的index设置为不可以通过这个字段进行搜索,重新建立索引。

es的一些基本入门操作如上,测试工具用的是postman。经过一些基本操作之后,才会加深对概念的理解。