入门
一些基础操作:
环境:ElasticSearch 6.8.2 , PostMan
1.创建indices,types,documents,fields
PUT http://127.0.0.1:9200/megacorp/employee/1
{
"first_name":"John",
"last_name":"Smith",
"age":25,
"about":"I love to go rock climbing",
"interests":["sports","music"]
}
PUT http://127.0.0.1:9200/megacorp/employee/1
{
"first_name":"John",
"last_name":"Smith",
"age":25,
"about":"I love to go rock climbing",
"interests":["sports","music"]
}
PUT http://127.0.0.1:9200/megacorp/employee/1
{
"first_name":"John",
"last_name":"Smith",
"age":25,
"about":"I love to go rock climbing",
"interests":["sports","music"]
}
2.通过documentId获取文档内容:
get http://127.0.0.1:9200/megacorp/employee/1
get http://127.0.0.1:9200/megacorp/employee/2
get http://127.0.0.1:9200/megacorp/employee/3
3.进行简单的查询(本例通过last_name查询)
POST http://127.0.0.1:9200/megacorp/employee/_search
{
"query":{
"match":{
"last_name":"Smith"
}
}
}
4.进行复杂查询(进一步过滤结果)
查找出last_name = “Smith”,并且年龄 > 30 的员工
{
"query":{
"bool":{
"must":{
"match":{
"last_name":"Smith"
}
},
"filter":{
"range":{
"age":{"gt":30}
}
}
}
}
}
5.全文搜索
模糊查询,会对查询词组分词,根据相关度显示查询结果
{
"query":{
"match":{
"about":"rock climbing"
}
}
}
词组查询
{
"query":{
"match_phrase":{
"about":"rock climbing"
}
}
}
6.高亮结果
{
"query":{
"match":{
"about":"rock climbing"
}
},
"highlight":{
"fields":{
"about":{}
}
}
}
6.聚合分析
相当于SQL中的group by
{
"aggs":{
"all_interests":{
"terms":{
"field":"interests"
}
}
}
}
分级汇总:
{
"aggs":{
"all_interests":{
"terms":{
"field":"interests"
},
"aggs":{
"avg_age":{
"avg":{
"field":"age"
}
}
}
}
}
}