elasticsearch查询

es索引中的数据如下:

1.简单查询

先新建book索引,类型为小说,字段有world_count(字数),author(作者),title(标题),publish_date(发布日期)

查询id为1的文档

2.条件查询

查询所有:默认指查询10条

分页:

关键词查询:查询title含有ElasticSearch的文档

结果:

{
	"took": 6,
	"timed_out": false,
	"_shards": {
		"total": 3,
		"successful": 3,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": 4,
		"max_score": 1.3469357,
		"hits": [
			{
				"_index": "book",
				"_type": "novel",
				"_id": "AKl9yGwBp8L3WHXTw3AR",
				"_score": 1.3469357,
				"_source": {
					"author": "瓦力",
					"title": "ElasticSearch入门",
					"world_count": "3000",
					"publish_date": "2017-08-20"
				}
			},
			{
				"_index": "book",
				"_type": "novel",
				"_id": "2",
				"_score": 0.5531392,
				"_source": {
					"author": "很胖的瓦力",
					"title": "ElasticSearch精通",
					"world_count": "3000",
					"publish_date": "2017-10-01"
				}
			},
			{
				"_index": "book",
				"_type": "novel",
				"_id": "11",
				"_score": 0.5531392,
				"_source": {
					"author": "林卫平",
					"title": "elasticsearch入门",
					"world_count": "2000",
					"publish_date": "2017-05-08"
				}
			},
			{
				"_index": "book",
				"_type": "novel",
				"_id": "5",
				"_score": 0.4889865,
				"_source": {
					"author": "李四",
					"title": "ElasticSearch大法好",
					"world_count": "2000",
					"publish_date": "2018-10-01"
				}
			}
		]
	}
}

排序:根据发布日期排序

{
	"query": {
		"match": {
			"title": "ElasticSearch"

		}
	},
	"sort": [{
		"publish_date": {
			"order": "desc"
		}
	}]

}

3.聚合查询

根据world_count聚合

 

 

代码:

localhost:9200/book/novel/_search

{
	"aggs":{
		"group_by_word_count":{
			"terms":{
				"field":"world_count"
			}
		}
		
	}
	
}

多个聚合

{
	"aggs":{
		"group_by_word_count":{
			"terms":{
				"field":"world_count"
			}
		},
		"group_by_publish_date":{
			"terms":{
				"field":"publish_date"
			}
		}
		
	}
	
}

求平均值,总和,最大,最小值

代码

localhost:9200/book/novel/_search

{
	"aggs":{
		"group_by_word_count":{
			"stats":{
				"field":"world_count"
			}
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值