Elasticsearch查询操作
索引中最基本的单元叫做文档 document. 在es中文档的示例如下:
{
"_index": "questions",
"_type": "baichebao",
"_id": "4",
"_score": 1,
"_version" : 1,
"_source": {
"id": 4,
"content": "汽车常见故障的解决办法有哪些?",
"uid": 1,
"all_answer_count": 2,
"series_id": 0,
"score": 0,
"answer_count": 2
}
文档中下划线开头的是es自带的字段
_index 代表索引名_type 代表类型_id 代表文档id,如果插入文档的时候没有设置id的话,那么es会自动生成一个唯一id_score 这个不是文档自带的,而是进行搜索的时候返回的,代表这个文档和搜索的相关匹配分值_source 储存原始文本及分类好的字段_version 代表这个文档的版本一:terms查询
如果你想要找到所有售价等于10000美刀的车,那么可以使用一个terms查询:
GET /cars/transactions/_search
{
"query": {
"term": {
" PRice ": "10000"
}
}
}
二:filtered查询
如果你想要找到所有售价高于10000美刀的车,那么可以使用一个filtered查询:
POST /cars/transactions/_search
{
"query" : {
"filtered": {
"filter": {
"range": {
"price": {
"gte": 10000
}
}
}
}
}
}
该查询(包含了一个过滤器)返回文档的一个特定子集,然后聚合工作在该子集上。
三:agg聚合查询
对索引中全部的车计算其平均价格
POST /cars/transactions/_search
{
"aggs": {
"car_avg": {
"avg": {
"field": "price"
}
}
}
}
四:过滤聚合联合查询
如果你想要找到所有售价高于10000美刀的车,同时也对这些车计算其平均价格,那么可以这样查询:
POST /cars/transactions/_search
{
"query" : {
"filtered": {
"filter": {
"range": {
"price": {
"gte": 10000
}
}
}
}
},
"aggs" : {
" car_avg ": {
"avg" : { "field" : "price" }
}
}
五:聚合嵌套查询
如果你想要找到不同颜色的车的数量,并且计算不同颜色的车的平均价钱,那么可以这样查询
POST /cars/transactions/_search
{
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
六:聚合并列查询
如果你想要找到不同颜色的车的数量,还要知道全部车的平均价钱,那么可以这样查询
{
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"colors": {
"terms": {
"field": "color"
}
}
}
}
七:综合查询
场景:查询出某一天adtype(广告位id)为2的数据每小时有几条,每小时的总收益和平均收益
{
"query": {
"bool": {
"must": [
{
"term": {
"adtype": "2"
}
},
{
"match_all": {}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 20,
"sort": [],
"aggs": {
"adxtime": {
"date_histogram": {
"field": "adx_reporttime",
"interval": "1h",
"min_doc_count": 0
},
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
},
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
一:获取es客户端
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "pangu")
.put("client.transport.sniff", true) //允许嗅探
.build();
Client client = TransportClient.builder()
.settings(settings) //设置部分
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("192.168.33.203"), 9300));
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("192.168.33.204"), 9300));
二:简单查询(对应restful查询第一点)
BoolQueryBuilder qb = QueryBuilders.boolQuery();
qb.must(new QueryStringQueryBuilder("10000").field("" price "));
SearchResponse response = client
.prepareSearch("cars ")
.setTypes("transactions ")
.setSearchType(SearchType.DEFAULT).setFrom(0).setSize(10)
.setQuery(qb)
.execute().actionGet();
for (SearchHit hit : response.getHits().getHits()) {
System.out.println(hit.getSource().get("price"));
}
三:聚合嵌套查询(对应restful查询第五点)
AggregationBuilder aggs = AggregationBuilders.terms("colors").field(
"color");
aggs.subAggregation(AggregationBuilders.avg("avg_price").field("price"));
SearchResponse response = client
.prepareSearch("cars")
.setTypes("transactions")
.setSearchType(SearchType.DEFAULT)
.addAggregation(aggs)
.execute().actionGet();
Terms terms = response.getAggregations().get("colors");
for (Bucket b : terms.getBuckets()) {
System.out.print(b.getKey() + "有:");
System.out.println(b.getDocCount() + " 辆");
Avg avg = b.getAggregations().get("avg_price");
System.out.print(b.getKey() + "平均:");
System.out.println(avg.getValue()+ "元");
}
四:聚合并列查询(对应restful查询第六点)
AggregationBuilder aggs1 = AggregationBuilders.terms("avg_price").field(
"price");
AggregationBuilder aggs2 = AggregationBuilders.terms("colors").field(
"color");
SearchResponse response = client
.prepareSearch("cars")
.setTypes("transactions")
.setSearchType(SearchType.DEFAULT)
.addAggregation(aggs1)
.addAggregation(aggs2)
.execute().actionGet();
Avg avg = response.getAggregations().get("avg_price");
System.out.println(avg.getValue()+ "元");
Terms terms = response.getAggregations().get("colors");
for (Bucket b : terms.getBuckets()) {
System.out.print(b.getKey() + "有:");
System.out.println(b.getDocCount() + " 辆");
}
五:综合查询
场景:查询出某一天adtype(广告位id)为2的数据每小时有几条,每小时的总收益和平均收益
String str = "ad_detail_model";
String indexs[] = str.split(",");
BoolQueryBuilder qb = QueryBuilders.boolQuery();
qb.must(new QueryStringQueryBuilder("2").field("adtype"));
DateHistogramInterval d1 = new DateHistogramInterval("1h");
AggregationBuilder aggs1 = AggregationBuilders.dateHistogram("timeAgg")
.field("adx_reporttime").interval(d1).minDocCount(0);
aggs1.subAggregation(AggregationBuilders.sum("sumAgg").field("price"));
aggs1.subAggregation(AggregationBuilders.avg("avgAgg").field("price"));
SearchResponse response = client
.prepareSearch(indexs)
.setSearchType(SearchType.DEFAULT)
.setQuery(qb)
.addAggregation(aggs1)
.execute().actionGet();
Histogram agg = response.getAggregations().get("timeAgg");
System.out.println("===================================");
for (Histogram.Bucket entry : agg.getBuckets()) {
System.out.println("*******************************************");
Sum sum = entry.getAggregations().get("sumAgg");
System.out.print(entry.getKey() + "总共有:");
System.out.println(sum.getValue()+ "元");
Avg avg = entry.getAggregations().get("avgAgg");
System.out.print(entry.getKey() + "平均有:");
System.out.println(avg.getValue()+ "元");
String key = ""+ entry.getKey(); // Key
String keyAsString = entry.getKeyAsString(); // Key as String
long docCount = entry.getDocCount(); // Doc count
System.out.println(keyAsString+"有:"+docCount+" 个");
System.out.println("*******************************************");
}
System.out.println("===================================");
新闻热点
疑难解答
图片精选