Elasticsearch7.12常用操作总结

Elasticsearch是一个开源的高扩展的分布式、RESTful风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。
本文使用Elasticsearch7.12.0版本,使用Postman作为客户端访问工具

Elasticsearch数据格式

Elasticsearch是面向文档型数据库,一条数据在此就是一个文档,与MySQL对比如下:
elastic与mysql对比
ES里的Index可以看作是一个库,Types相当于表,Documents相当于表的行。在ES7.x中,Type的概念已经被删除

索引操作

创建索引

相当于关系型数据库中创建数据库,在Postman中使用PUT类型发起请求:http://localhost:9200/user
返回数据:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
}

acknowledged:响应结果
shards_acknowledged:分片结果
index:索引名称
如果重复添加会返回错误信息,索引已存在

{
    "error": {
        "root_cause": [
            {
                "type": "resource_already_exists_exception",
                "reason": "index [user/zF9iWH0dR-2Xt_dmr5BPZg] already exists",
                "index_uuid": "zF9iWH0dR-2Xt_dmr5BPZg",
                "index": "user"
            }
        ],
        "type": "resource_already_exists_exception",
        "reason": "index [user/zF9iWH0dR-2Xt_dmr5BPZg] already exists",
        "index_uuid": "zF9iWH0dR-2Xt_dmr5BPZg",
        "index": "user"
    },
    "status": 400
}

查看所有索引

在Postman中,使用GET类型发起请求:http://localhost:9200/_cat/indices?v
_cat表示查看,indices表示索引
返回结果:

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   user     zF9iWH0dR-2Xt_dmr5BPZg   1   1          0            0       208b           208b
字段解释
health当前服务器健康状态:green(集群完整);yellow(单点正常/集群不完整);red(单点不正常)
status索引打开/关闭状态
index索引名
uuid索引统一编号
pri主分片数量
rep副本数量
docs.count可用文档数量
docs.deleted文档删除状态(逻辑删除)
store.size主分片和副分片整体占空间大小
pri.store.size主分片占空间大小

查看单个索引

在Postman中,使用GET类型发起请求:http://localhost:9200/user

{
    "user": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "user",
                "creation_date": "1618148948222",
                "number_of_replicas": "1",
                "uuid": "zF9iWH0dR-2Xt_dmr5BPZg",
                "version": {
                    "created": "7120099"
                }
            }
        }
    }
}

注意:查看索引和创建索引的请求路径都是一样的,不同的是请求方式不同

删除索引

在Postman中,使用DELETE类型发起请求:http://localhost:9200/user
返回结果:

{
    "acknowledged": true
}

重新访问索引时,会返回响应:索引不存在

{
    "error": {
        "root_cause": [
            {
                "type": "index_not_found_exception",
                "reason": "no such index [user]",
                "resource.type": "index_or_alias",
                "resource.id": "user",
                "index_uuid": "_na_",
                "index": "user"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index [user]",
        "resource.type": "index_or_alias",
        "resource.id": "user",
        "index_uuid": "_na_",
        "index": "user"
    },
    "status": 404
}

文档操作

类似于关系型数据库中的表数据

创建文档

在Postman中,使用POST类型发起请求:http://localhost:9200/user/_doc

{
	"id": 100,
	"name": "张宝芸",
	"age": 18,
	"salary": 6000
}

响应结果为:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "nU1ewXgBnzNOgvN8P7th",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

_id是唯一性标识,如果没有指定,ES服务器会随机生成,如果需要自定义唯一性标识,需要在创建时指定id,即http://localhost:9200/user/_doc/1
注意:如果不指定主键,请求类型只能是POST,如果指定主键,请求类型可以是PUT

查看文档

在Postman中,使用GET请求类型发起请求:http://localhost:9200/user/_doc/1
响应结果:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "id": 101,
        "name": "萧峰",
        "age": 20,
        "salary": 8000
    }
}

其中found表示查询结果,true表示查询到,false表示未查询到

修改文档

在Postman中,使用POST类型发起请求:http://localhost:9200/user/_doc/1
响应结果:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

其中result表示结果,updated表示数据被更新

修改字段

也可以只修改某一条数据的局部信息
在Postman中,发起POST类型的请求:http://localhost:9200/user/_update/1

{
	"doc": {
		"salary": 12000
	}
}

响应结果:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

根据唯一性标识,查询文档数据发现已修改,http://localhost:9200/user/_doc/1

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "_seq_no": 3,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "id": 101,
        "name": "萧峰",
        "age": 22,
        "salary": 12000
    }
}

删除文档

删除文档时只会进行逻辑删除
在Postman中,发起DELETE类型请求:http://localhost:9200/user/_doc/1
响应结果为:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

其中result表示结果,deleted表示数据被标记为删除
删除文档数据后,再次查询会提示找不到

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "found": false
}

如果删除一个不存在的文档,result显示not_found

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "not_found",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

条件删除文档

删除数据时可以根据唯一性标识进行删除,也可以根据条件对对多条数据进行删除
在Postman中,发起POST类型请求:http://localhost:9200/user/_delete_by_query

{
	"query": {
		"match": {
			"salary": 6000
		}
	}
}

响应结果为:

{
    "took": 47,
    "timed_out": false,
    "total": 1,
    "deleted": 1,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

其中took表示耗时,timed_out表示是否超时,deleted表示删除数量

映射操作

索引创建后,就可以建索引库中的映射,类似于关系型数据库中的表结构(table)

创建映射

在Postman中,发起PUT类型请求:http://localhost:9200/user/_mapping

{
	"properties": {
		"name": {
			"type": "text",
			"index": true
		},
		"sex": {
			"type": "text",
			"index": false
		},
		"age": {
			"type": "long",
			"index": false
		}
	}
}

响应结果为:

{
    "acknowledged": true
}

说明:
字段名:可以随意填写
type:类型
String类型:text:可分词;keyword:不可分词
Numerical数值类型:
基本数据类型:long、integer、short、byte、double、float、half_float
浮点数高精度类型:scaled_float
Date日期类型
Array数组类型
Object对象
index:是否索引,默认true,字段会被索引,可以用来进行搜索;false,字段不会被索引,不能用于搜索
store:是否将数据进行独立存储,默认false
analyzer:分词器

查看映射

在Postman中,发起GET类型请求:http://localhost:9200/user/_mapping
响应结果为:

{
    "user": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "long",
                    "index": false
                },
                "name": {
                    "type": "text"
                },
                "sex": {
                    "type": "text",
                    "index": false
                }
            }
        }
    }
}

索引映射关联

在Postman中,发起PUT类型请求:http://localhost:9200/userinfo
请求报文为:

{
	"settings": {
	},
	"mappings": {
		"properties": {
			"name": {
				"type": "text",
				"index": true
			},
			"sex": {
				"type": "text",
				"index": false
			},
			"age": {
				"type": "long",
				"index": false
			}
		}
	}
}

响应结果为:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "userinfo"
}

高级查询

ES提供了基于JSON的查询DSL来定义查询

查询所有文档

在Postman中,发起GET类型请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"match_all": {
		}
	}
}

“query”:表示一个查询对象,可以有不同的查询属性
“match_all”:查询类型
响应结果为:

{
    "took": 517,//查询时间,单位ms
    "timed_out": false,//是否超时
    "_shards": {//分片信息
        "total": 1,//总数
        "successful": 1,//成功
        "skipped": 0,//忽略
        "failed": 0//失败
    },
    "hits": {//搜索命中结果
        "total": {//搜索条件匹配的文档总数
            "value": 5,//总命中计数的值
            "relation": "eq"//计数规则,eq表示计数准确,gte表示计数不准确
        },
        "max_score": 1.0,//匹配度分值
        "hits": [//命中结果集合
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.0,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "103",
                "_score": 1.0,
                "_source": {
                    "name": "zhaomin",
                    "nickname": "郡主",
                    "sex": "女",
                    "age": 22
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "104",
                "_score": 1.0,
                "_source": {
                    "name": "xiaolongnv",
                    "nickname": "古墓派",
                    "sex": "女",
                    "age": 18
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "105",
                "_score": 1.0,
                "_source": {
                    "name": "mafuren",
                    "nickname": "丐帮夫人",
                    "sex": "女",
                    "age": 30
                }
            }
        ]
    }
}

匹配查询

match匹配类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系。
在Postman中,发起GET类型请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"match": {
			"name": "xiaofeng"
		}
	}
}

响应结果为:

{
    "took": 23,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.3862942,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                }
            }
        ]
    }
}

字段匹配查询

multi_match与match类似,不同的是它可以在多个字段中查询。在postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"multi_match": {
			"query": "xiaofeng",
			"fields": ["name", "nickname"]
		}
	}
}

响应报文为:

{
    "took": 19,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.3862942,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                }
            }
        ]
    }
}

关键字精确查询

term查询,精确的关键词匹配查询,不对查询条件进行分词。在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"term": {
			"name": {
				"value": "xiaofeng"
			}
		}
	}
}

相应报文为:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.3862942,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                }
            }
        ]
    }
}

多关键字精确查询

terms查询和term查询一样,不同的是允许指定多值进行匹配,如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件,类似于mysql中的in。在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"terms": {
			"name": ["xiaofeng", "zhaomin"]
		}
	}
}

响应报文为:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.0,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "103",
                "_score": 1.0,
                "_source": {
                    "name": "zhaomin",
                    "nickname": "郡主",
                    "sex": "女",
                    "age": 22
                }
            }
        ]
    }
}

指定查询字段

通常情况下,ES在搜索结果中会把文档中保存在_source的所有字段都返回,如果只想获取其中的部分字段,可以添加_source过滤。在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"_source": ["name", "nickname"],
	"query": {
		"terms": {
			"name": ["zhangwuji"]
		}
	}
}

响应报文为:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主"
                }
            }
        ]
    }
}

过滤字段

includes:指定想要显示的字段
excludes:指定不想要显示的字段
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"_source": {
		"includes": ["name", "nickname"]
	},
	"query": {
		"terms": {
			"name": ["zhangwuji"]
		}
	}
}
{
	"_source": {
		"excludes": ["name", "nickname"]
	},
	"query": {
		"terms": {
			"name": ["zhangwuji"]
		}
	}
}

响应报文为:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主"
                }
            }
        ]
    }
}
{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "sex": "男",
                    "age": 30
                }
            }
        ]
    }
}

组合查询

bool把各种其他查询通过must、must_not、should的方式进行组合。在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"bool": {
			"must": [
				{
					"match": {
						"name": "zhangwuji"
					}
				}
			],
			"must_not": [
				{
					"match": {
						"age": 40
					}
				}
			],
			"should": [
				{
					"match": {
						"sex": "男"
					}
				}
			]
		}
	}
}

响应报文为:

{
    "took": 1006,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.3754225,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 2.3754225,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                }
            }
        ]
    }
}

范围查询

range查询找出那些落在指定区间内的数字或时间,可以有以下字符

操作符说明
gt大于>
gte大于等于>=
lt小于<
lte小于等于<=

在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"range": {
			"age": {
				"gte": 18,
				"lte": 40
			}
		}
	}
}

响应报文为:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.0,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "103",
                "_score": 1.0,
                "_source": {
                    "name": "zhaomin",
                    "nickname": "郡主",
                    "sex": "女",
                    "age": 22
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "104",
                "_score": 1.0,
                "_source": {
                    "name": "xiaolongnv",
                    "nickname": "古墓派",
                    "sex": "女",
                    "age": 18
                }
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "105",
                "_score": 1.0,
                "_source": {
                    "name": "mafuren",
                    "nickname": "丐帮夫人",
                    "sex": "女",
                    "age": 30
                }
            }
        ]
    }
}

模糊查询

返回包含于搜索字词相似的字词的文档,编辑距离是将一个术语所需的一个字符更改的次数,包括
更改字符(box -> fox)
删除字符(black -> lack)
插入字符(sic -> sick)
转置两个相邻字符(act->cat)
为了找到相似的术语,fuzzy 查询会在指定的编辑距离内创建一组搜索词的所有可能的变体
或扩展。然后查询返回每个扩展的完全匹配。
通过fuzziness 修改编辑距离。一般使用默认值 AUTO ,根据术语的长度生成编辑距离。
在Postman中,发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"fuzzy": {
			"name": {
				"value": "zhangwuji"
			}
		}
	}
}

响应报文为:

{
    "took": 62,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.4816045,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.4816045,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                }
            }
        ]
    }
}

单字段排序

sort可以按照不同的字段进行排序,通过order指定排序的方式,desc降序,asc升序。在Postman中,发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"match": {
			"name": "zhangwuji"
		}
	},
	"sort": [
		{
			"age": {
				"order": "desc"
			}
		}
	]
}

响应报文为:

{
    "took": 35,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": null,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                },
                "sort": [
                    30
                ]
            }
        ]
    }
}

多字段排序

先按年龄排序,再按相关性得分排序。在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"match_all": {
		}
	},
	"sort": [
		{
			"age": {
				"order": "desc"
			}
		},
		{
			"_score": {
				"order": "desc"
			}
		}
	]
}

响应报文为:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": 1.0,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                },
                "sort": [
                    32,
                    1.0
                ]
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.0,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                },
                "sort": [
                    30,
                    1.0
                ]
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "105",
                "_score": 1.0,
                "_source": {
                    "name": "mafuren",
                    "nickname": "丐帮夫人",
                    "sex": "女",
                    "age": 30
                },
                "sort": [
                    30,
                    1.0
                ]
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "103",
                "_score": 1.0,
                "_source": {
                    "name": "zhaomin",
                    "nickname": "郡主",
                    "sex": "女",
                    "age": 22
                },
                "sort": [
                    22,
                    1.0
                ]
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "104",
                "_score": 1.0,
                "_source": {
                    "name": "xiaolongnv",
                    "nickname": "古墓派",
                    "sex": "女",
                    "age": 18
                },
                "sort": [
                    18,
                    1.0
                ]
            }
        ]
    }
}

高亮查询

进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称为高亮。
在ES中可以对查询内容中的关键字部分进行标签和样式(高亮)的设置,在使用match查询的同时,加上highlight属性
pre_tags:前置标签
post_tags:后置标签
fields:需要高亮的字段
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"match": {
			"name": "zhangwuji"
		}
	},
	"highlight": {
		"pre_tags": "<font color='red'>",
		"post_tags": "</font>",
		"fields": {
			"name": {
			}
		}
	}
}

响应报文为:

{
    "took": 62,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": 1.3862942,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                },
                "highlight": {
                    "name": [
                        "<font color='red'>zhangwuji</font>"
                    ]
                }
            }
        ]
    }
}

分页查询

from:当前页的起始索引,默认从0开始。from = (pageNum - 1) * size
size:每页显示多少条
在Postman中,发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"query": {
		"match_all": {
		}
	},
	"sort": [
		{
			"age": {
				"order": "desc"
			}
		}
	],
	"from": 0,
	"size": 2
}

响应报文为:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "102",
                "_score": null,
                "_source": {
                    "name": "xiaofeng",
                    "nickname": "帮主",
                    "sex": "男",
                    "age": 32
                },
                "sort": [
                    32
                ]
            },
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "101",
                "_score": null,
                "_source": {
                    "name": "zhangwuji",
                    "nickname": "教主",
                    "sex": "男",
                    "age": 30
                },
                "sort": [
                    30
                ]
            }
        ]
    }
}

聚合查询

用于对ES文档进行统计分析
取最大值-max
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"max_age": {
			"max": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 48,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "max_age": {
            "value": 32.0
        }
    }
}

取最小值-min
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"min_age": {
			"min": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "min_age": {
            "value": 18.0
        }
    }
}

求和-sum
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"sum_age": {
			"sum": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "sum_age": {
            "value": 132.0
        }
    }
}

取平均值-avg
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"avg_age": {
			"avg": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "avg_age": {
            "value": 26.4
        }
    }
}

去重后取总数
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"distinct_age": {
			"cardinality": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 27,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "distinct_age": {
            "value": 4
        }
    }
}

State聚合
对某个字段一次性返回count,max,min,avg和sum
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"stats_age": {
			"stats": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "stats_age": {
            "count": 5,
            "min": 18.0,
            "max": 32.0,
            "avg": 26.4,
            "sum": 132.0
        }
    }
}

桶聚合查询

相当于sql中的group by
terms聚合,分组统计
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"age_groupby": {
			"terms": {
				"field": "age"
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 28,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "age_groupby": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 30,
                    "doc_count": 2
                },
                {
                    "key": 18,
                    "doc_count": 1
                },
                {
                    "key": 22,
                    "doc_count": 1
                },
                {
                    "key": 32,
                    "doc_count": 1
                }
            ]
        }
    }
}

在terms分组下再进行聚合
在Postman中发起GET请求:http://localhost:9200/user/_search
请求报文为:

{
	"aggs": {
		"age_groupby": {
			"terms": {
				"field": "age"
			},
			"aggs": {
				"sum_age": {
					"sum": {
						"field": "age"
					}
				}
			}
		}
	},
	"size": 0
}

响应报文为:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "age_groupby": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 30,
                    "doc_count": 2,
                    "sum_age": {
                        "value": 60.0
                    }
                },
                {
                    "key": 18,
                    "doc_count": 1,
                    "sum_age": {
                        "value": 18.0
                    }
                },
                {
                    "key": 22,
                    "doc_count": 1,
                    "sum_age": {
                        "value": 22.0
                    }
                },
                {
                    "key": 32,
                    "doc_count": 1,
                    "sum_age": {
                        "value": 32.0
                    }
                }
            ]
        }
    }
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值