查询和过滤
可以使用两种结构化语句: 结构化查询(Query DSL)和结构化过滤(Filter DSL)。 查询与过滤语句非常相似,但是它们由于使用目的不同而稍有差异。
复合查询
查全部
GET /ecommerce/product/_search{"query": { "match_all": {} }}
查询不存在的字段
GET index/_search{"query": {"bool": {"must_not": {"exists":{"field":"filedX"}}}}}
bool 查询
该模板适用于所有情况,尤其适用于侧边栏多级多条件联合查询
{"query": {"bool": {"must": [{"match": {"countryArea": "United States"}},{"bool": {"should": [{"match": {"sponsor": "National Science Foundation"}},{"match": {"sponsor": "David and Lucile Packard Foundation"}}],"minimum_should_match": 1}},{"bool": {"should": {"nested": {"path": "researchAreas","query": {"bool": {"should": [{"match": {"researchAreas.subjectName": "Arts and Humanities"}},{"match": {"researchAreas.subjectName": "Ecology"}}],"minimum_should_match": 1}}}}}}],"must_not": {"match": {"fundingType": "Program or Curriculum Development or Provision"}}}}}
range
gt: greater than 大于
gte: greater than or equal 大于等于
lt: less than 小于
lte: less than or equal 小于等于
GET /salemgr_user_bigtable/_doc/_search?{"from":0,"query":{"bool":{"filter":[{"range":{"data_officeloginon":{"lt":0}}}]}},"size":20}
bool过滤
一条过滤语句会询问每个文档的字段值是否包含着特定值:
- created 的日期范围是否在 2013 到 2014 ?
- status 字段中是否包含单词 “published” ?
- lat_lon 字段中的地理位置与目标点相距是否不超过10km ?
GET / ecommerce / product / _search{"query": {"bool": {"must": {"match": {"name": "yagao"}},"filter": {"range": {"price": {"gt": 25}}}}}}
过滤后排序
{"size":5,"query":{"bool":{"filter":[{"range":{"startTime":{"from":1517046960000,"to":1517048760000}}}]}},"sort":[{"startTime":{"order":"desc"}}]}
脚本排序
```java String str = “(doc[‘heat’].size()==0 ? 0 : doc[‘heat’].value) + “ +
Script script = new Script(str);"(doc['unfold'].size()==0 ? 0 : doc['unfold'].value)";
ScriptSortBuilder scriptSortBuilder = SortBuilders .scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER) .order(SortOrder.DESC); searchSourceBuilder.sort(scriptSortBuilder);
<a name="MZr8P"></a>## multi_match`multi_match`查询提供了一个简便的方法用来对多个字段执行相同的查询。<a name="NSeFp"></a># 聚合<a name="24bOi"></a>## 按组内字段排序AggregationBuilder aggregation =<br /> AggregationBuilders<br /> .terms("agg").field("gender")<br /> .subAggregation(<br /> AggregationBuilders.topHits("top")<br /> .explain(true)<br /> .size(1)<br /> .from(10)<br /> .sort("sortFiled", SortOrder.ASC)<br /> );<a name="bUTCn"></a>## 按聚合后指标排序1. 通过key排序TermsAggregationBuilder termsAggs = AggregationBuilders._terms_("聚合名称").field("分组字段")<br /> .order(BucketOrder._key_(true));2. 文档数聚合.order(BucketOrder.count(true))<a name="2451b9c1"></a>## 根据聚合结果排序 根据字聚合 responseTime.avg```json{"query" : {"bool" : {"filter" : [{"term" : {"type" : {"value" : "URL","boost" : 1.0}}}]}},"aggregations" : {"CATEGORY" : {"terms" : {"field" : "name","size" : 5,"order" : {"responseTime.avg" : "asc" }},"aggregations" : {"responseTime" : {"extended_stats" : {"field" : "durationInMillis","sigma" : 2.0}},"error" : {"sum" : {"script" : {"inline" : "def errorTemp=doc['status'].value; if(errorTemp=='0'){return 0;}else{return 1;}","lang" : "painless"}}},"apdex" : {"avg" : {"script" : {"inline" : "def responseTemp=doc['durationInMillis'].value; if(responseTemp>params.threshold){return 0.5;}else{return 1;}","lang" : "painless","params" : {"threshold" : 20.0}}}},"errorRate" : {"percentile_ranks" : {"script" : {"inline" : "def errorTemp=doc['status'].value; if(errorTemp=='0'){return 1;}else{return 0;}","lang" : "painless"},"values" : [0.0],"keyed" : true,"tdigest" : {"compression" : 100.0}}}}}}}
按聚合名称标识对桶进行排序
AggregationBuilders
.terms(“genders”)
.field(“gender”)
.order(BucketOrder.aggregation(“avg_height”, false))
.subAggregation(
AggregationBuilders.avg(“avg_height”).field(“height”)
)
按多个聚合指标对桶进行排序
AggregationBuilders
.terms(“genders”)
.field(“gender”)
.order(BucketOrder.compound( // in order of priority:
BucketOrder.aggregation(“avg_height”, false), // sort by sub-aggregation first
BucketOrder.count(true))) // then bucket count as a tie-breaker
.subAggregation(
AggregationBuilders.avg(“avg_height”).field(“height”)
)
聚合nested
GET audio/info/_search
{
"size": 0,
"query":{
"bool": {
"must": [
{
"range": {
"info.inputTime": {
"gte": "2019-07-01 05:51:47",
"lte": "2019-07-09 09:59:59"
}
}
}
]
}
},
"aggs": {
"wordgroup": {
"nested": {
"path": "wordFrequency"
},
"aggs": {
"word": {
"terms": {
"field": "wordFrequency.keyword",
"size": 50,
"exclude": ["这个"],
"order": {
"wordnum.value": "desc"
}
},
"aggs": {
"wordnum": {
"sum": {
"field": "wordFrequency.count"
}
}
}
}
}
}
}
}
GET /community_user_bigtable/_doc/_search?{
“size”:0,
“_source”:false,
“aggs”:{
“class_aggs”:{
“terms”:{
“field”:”classid”,
“order”: [{“isfillpaper_sum”: “desc”} ]
},
“aggs”:{
“isfillpaper_sum”:{
“sum”:{
“field”:”isfillpaper”
}
},
“wechat_sum”:{
“sum”:{
“field”:”isaddwechat”
}
},
“login_filter”:{
“filter”:{
“range”: {
“data_logintime”: {
“gte”: 0
}
}
}
},
“login_sum2”:{
“filter”:{
“range”: {
“data_logintime”: {
“lte”: 0
}
}
},
“aggs”:{
“term”:{
“sum”:{
“field”:”isfillpaper”
}
}
}
}
}
}
}
}
