GET _cat/indices
GET _cat/plugins
PUT rs_user_info
{
"mappings": {
"properties" : {
"createTime" : {
"type" : "date",
"format" : "yyyy-MM-dd HH:mm:ss"
},
"nickName" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"userAge" : {
"type" : "integer"
},
"userAvatar" : {
"type" : "keyword",
"index" : false
},
"userHobby" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"userId" : {
"type" : "keyword"
},
"userName" : {
"type" : "keyword"
}
}
}
}
GET rs_user_info
GET tb_user_info/_search
{
"query": {
"match_all": {}
}
}
GET tb_user_info/_search
{
"query": {
"match": {
"nickName": "花花"
}
}
}
GET tb_user_info/_search
{
"query": {
"match": {
"nickName": "花花"
}
}
, "sort": [
{
"age": {
"order": "desc"
}
}
]
}
GET tb_user_info/_search
{
"query": {
"match": {
"nickName": "花花"
}
}
, "sort": [
{
"age": {
"order": "desc"
},
"createTime": {
"order": "asc"
}
}
]
}
GET tb_user_info/_search
{
"query": {
"match": {
"nickName": "花花"
}
}
, "sort": [
{
"age": {
"order": "desc"
},
"createTime": {
"order": "asc"
}
}
],
"from": 0,
"size": 5
}
GET tb_user_info/_search
{
"query": {
"match": {
"nickName": "花花"
}
}
, "sort": [
{
"age": {
"order": "desc"
},
"createTime": {
"order": "asc"
}
}
],
"from": 0,
"size": 5,
"_source": [
"userName",
"nickName"
]
}
索引中必须有 同时
有这两个单词的才会在结果中。
GET es_user_info/_search
{
"query": {
"match_phrase": {
"nickName": "梦里的画"
}
}
}
GET tb_user_info/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"nickName": "花花"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 20,
"lte": 30
}
}
}
]
}
}
}
GET es_user_info/_search
{
"query": {
"match_phrase": {
"nickName": "花花"
}
},
"highlight": {
"fields": {
"nickName": {}
}
}
}
GET es_user_info/_search
{
"query": {
"match": {
"nickName": {
"query": "花花世界",
"operator": "and"
}
}
},
"highlight": {
"fields": {
"nickName": {}
}
}
}
match 还还有一种情况,就是必须满足分词结果中百分比的词,比如搜索词被分成这样子:java 程序员 书 推荐,这里就有 4 个词,假如要求 50% 命中其中两个词就返回,我们可以这样:
当然,这种需求也可以用 must、must_not、should 匹配同一个字段进行组合来查询
GET es_user_info/_search
{
"query": {
"match": {
"nickName": {
"query": "花花世界",
"minimum_should_match": "50%"
}
}
},
"highlight": {
"fields": {
"nickName": {}
}
}
}
GET es_user_info/_search
{
"query": {
"multi_match": {
"query": "花花",
"fields": ["nickName","hobby"]
}
}
}
elasticsearch高级功能之聚合查询,是比较复杂的的高级功能,其中包含相关的统计功能,比如最大值、最小值、平均值、求和等,又比如筛选完数据后,还需要过滤筛选项,今天我们详细分析一下:
1、基本的统计指标
1.1、计算基本类型的平均值
GET es_user_info/_search
{
"size": 0,
"aggs": {
"ageAve": {
"avg": {
"field": "age"
}
}
}
}
1.2、返回多个统计数据
GET es_user_info/_search
{
"size": 0,
"aggs": {
"mystatus": {
"stats": {
"field": "age"
}
}
}
}
1.1 按照用户性别聚合查询
GET es_user_info/_search
{
"size": 0,
"aggs": {
"myaggs": {
"terms": {
"field": "gender"
}
}
}
}
1.2、范围聚合查询
GET /es_user_info/_search
{
"size": 0,
"aggs": {
"my_agg": {
"range": {
"field": "age",
"ranges": [
{
"to": 20
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 100
}
]
}
}
}
}
1.3、按照性别维度聚合查询平均年龄
GET /es_user_info/_search
{
"size": 0,
"aggs": {
"my_agg": {
"terms": {
"field": "gender"
},
"aggs": {
"my_sum": {
"avg": {
"field": "age",
"missing": 100
}
}
}
}
}
}
1、先查询在聚合
GET /es_user_info/_search
{
"size": 0,
"query": {
"term": {
"gender": {
"value": "woman"
}
}
},
"aggs": {
"my_agg": {
"avg": {
"field": "age"
}
}
}
}