第一种:
{ "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "smith" } } } } }
在使用filtered的时候报错:
{ "error": { "root_cause": [ { "type": "parsing_exception", "reason": "no [query] registered for [filtered]", "line": 3, "col": 22 } ], "type": "parsing_exception", "reason": "no [query] registered for [filtered]", "line": 3, "col": 22 }, "status": 400 }
原因:过时,替换成:
{ "query" : { "bool" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "must" : { "match" : { "last_name" : "smith" } } } } }
第二种:
Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
具体的原因是聚合前需要大量的内存,聚合前,需要将相应的字段开启聚合:
所以:
PUT megacorp/_mapping/employee/ { "properties": { "interests": { "type": "text", "fielddata": true } } }
然后在进行查询:
GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" : { "field" : "interests" }, "aggs" : { "avg_age" : { "avg" : { "field" : "age" } } } } } }