#创建索引
PUT http://192.168.182.10:9200/school
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1,
"index.analysis.analyzer.default.type":"standard"
},
"mappings":{
"student":{
"properties":{
"stuName":{"type":"keyword","search_analyzer":"standard"},
"age":{"type":"integer"},
"resume":{"type":"text","analyzer":"ik_smart"},
"tuition":{"type":"scaled_float","scaling_factor":100},
"hobbies":{"type":"keyword"},
"address":{
"properties":{
"province":{"type":"keyword"},
"city":{"type":"keyword"},
"district":{"type":"keyword"}
}
}
}
}
}
}
//index:false
#删除索引
DELETE http://192.168.182.10:9200/school
#查看索引详情
GET http://192.168.182.10:9200/school/_settings
GET http://192.168.182.10:9200/school/_mappings
#新增数据(行ID系统默认生成类似UID,也可以指定ID)
POST http://192.168.182.10:9200/school/student/1
{
"stuName":"张三",
"age":"18",
"resume":"我是一个喜欢冒险的人",
"tuition":"26800.00",
"hobbies":"太极",
"address":{
"province":"江苏",
"city":"南京",
"district":"雨花"
}
}
#删除数据(指定ID)
DELETE http://192.168.182.10:9200/school/student/1
#修改数据(指定ID)
POST http://192.168.182.10:9200/school/student/1/_update
{
"doc":{
"age":"52"
}
}
###查询(默认是分词的)
#根据ID查询
GET http://192.168.182.10:9200/school/student/1
#term(精确匹配:<与match的区别:>单值查询结果相同,多值查询不同,match是随机匹配)
#"term":{"hobbies":"游泳"} <=> #"match":{"hobbies":"游泳"}
#"term":{"hobbies":"太极 游泳(整体)"} != #"match":{"hobbies":"太极 (或者) 游泳"}
#match(非精确模糊匹配)
{
"query":{
"match":{
"hobbies":"太极 动漫"
}
}
}
#根据id和字段查看分词情况
GET http://192.168.182.10:9200/school/student/1/_termvectors?fields=resume
#match_phrase,按分词器情况来检索 (我 是 一个 喜欢 冒险 的人)
{
"query":{
"match_phrase":{
"resume":{
"query":"我 冒险",
"slop":"3"
}
}
}
}
#特定查询_source excludes match_all
{
"from":0, (pageNo-1)*pageSize //分页查询
"size":2,
"_source":["stuName","age"], //只检索指定字段
"_source":{
"includes":["stuName","age"]
},
"_source":{ //排除指定字段
"excludes":["stuName","age"]
},
"sort":{ //排序
"tuition":{
"order":"desc"
}
},
"query":{
"match_all":{} //查询所有
}
}
#多字段匹配
{
"query":{
"multi_match":{
"query":"冒险",
"fields":["resume","hobbies"]
}
}
}
#aggs
{
"size":0,
"aggs":{
"agg_by_browser":{
"terms":{
"field":"browser"
}
}
}
}
#自定义分组统计
{
"query":{
"match_all":{}
},
"size":0,
"aggs":{
"agg_by_cmmtype":{
"terms":{
"field":"cmmType"
},
"aggs":{
"agg_min_price":{
"min":{
"field":"cmmPrice"
}
},
"agg_max_price":{
"max":{
"field":"cmmPrice"
}
},
"agg_avg_price":{
"avg":{
"field":"cmmPrice"
}
},
"agg_max_salenum":{
"max":{
"field":"saleNum"
}
},
"agg_sum_salenum":{
"sum":{
"field":"saleNum"
}
}
}
}
}
}
#分组统计函数聚合
{
"query":{
"match_all":{}
},
"size":0,
"aggs":{
"agg_by_cmmtype":{
"terms":{
"field":"cmmType"
},
"aggs":{
"stat_by_cmmprice":{
"stats / extended_stats":{
"field":"cmmPrice"
}
}
}
}
}
}
#扩展统计解释
"sum_of_squares": 3.9721605E7,
"variance": 1941338.979591837,
"std_deviation": 1393.3194104697734,
"std_deviation_bounds": {
"upper": 4718.781678082404,
"lower": -854.4959637966897
}
###------------ES聚合查询------------------#####
#聚合一般针对不分词keyword字段
PUT http://192.168.182.10:9200/eb
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"commodity":{
"properties":{
"shopId":{"type":"keyword","index":false},
"cmmName":{"type":"text","analyzer":"ik_smart"},
"cmmType":{"type":"keyword","index":true},
"cmmPrice":{"type":"scaled_float","scaling_factor":100},
"saleNum":{"type":"long"}
}
}
}
}
#text解决无法聚合的问题
PUT http://192.168.182.10:9200/taobao/_mapping/clientact/
{
"properties":{
"browser":{
"type":"text",
"fielddata":true
}
}
}