ES索引常用命令
ES创建索引
PUT /product/_doc/1
{
"name": "张三",
"desc": "测试数据",
"price": 1299,
"tag": ["张三","里斯","王五"]
}
PUT /product/_doc/2
{
"name":"李四",
"desc":"xiaomi",
"price":3999,
"tag":["m1","m2","m3"]
}
ES删除索引
DELETE /product/_doc/3
ES修改索引
修改单个字段
POST /product/_update/3
{
"doc": {
"price": 1999
}
}
全量替换
PUT /product/_doc/3
{
"name":"wangwu",
"desc":"huawei",
"price":1999,
"tag":["h1","h2","h3"]
}
ES查询
查询全部 GET /product/_search 根据id查询 GET /product/_doc/3
ES多条件查询
ElasticSearch之bool
1、must (must字段对应的是个列表,也就是说可以有多个并列的查询条件,一个文档满足各个子条件后才最终返回)
2、should (只要符合其中一个条件就返回)
3、must_not (与must相反,也就是说可以有多个并列的查询条件,一个文档各个子条件后才最终的结果都不满足)
4、filter(条件过滤查询,过滤条件的范围用range表示gt表示大于、lt表示小于、gte表示大于等于、lte表示小于等于)
GET /product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"price": "1999"
}
},
{
"match": {
"name": "wangwu"
}
}
]
}
}
}
GET /product/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"price": "1999"
}
},
{
"match": {
"price": "9999"
}
}
]
}
}
}
GET /product/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"price": "1999"
}
},
{
"match": {
"price": "3999"
}
}
]
}
}
}
GET /product/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"price": "1999"
}
}
]
}
}
}
GET /product/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"price": {
"gte": 1999,
"lte": 5000
}
}
}
]
}
}
}
bool查询总结
query,根据你的查询条件,去计算文档的匹配度得到一个分数,并且根据分数进行排序,不会做缓存的。
filter,根据你的查询条件去查询文档,不去计算分数,而且filter会对经常被过滤的数据进行缓存。
must:与关系,相当于关系型数据库中的 and。
should:或关系,相当于关系型数据库中的 or。
must_not:非关系,相当于关系型数据库中的 not。
filter:过滤条件。
range:条件筛选范围。
gt:大于,相当于关系型数据库中的 >。
gte:大于等于,相当于关系型数据库中的 >=。
lt:小于,相当于关系型数据库中的 <。
lte:小于等于,相当于关系型数据库中的 <=
ES查询高亮显示
GET /product/_search
{
"query": {
"match": {
"desc": "vivo"
}
},
"highlight": {
"fields": {
"desc": {}
},
"pre_tags": "<font color='red'>",
"post_tags": "</font>",
"fragment_size": 10
}
}
match 查询的同时,加上一个highlight 属性:pre_tags:前置标签post_tags:后置标签fields:需要高亮的字段title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置,也可以空