查询参数
暴露的端口
- _search:
- _mapping
常用的请求参数
- match_all
- query + match:匹配match中的单词:例如:mill lane会匹配所有包含address为mill 或者 lane或者mill lane的文档
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill lane" } }
}
'
- query+match_phrase:匹配match_phrase中的参数为指定短语的文档
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_phrase": { "address": "mill lane" } }
}
'
- 构造一个bool(布尔)类型的查询:你可以使用bool来组成一个多条件的查询标准。你可以设计这个标准为must match \should match\must not match等
query–bool—must—match–field:target
must查询
query–bool–must_not–match–field:target
must_not查询:会被treated as a filter,他会 影响文档是否应该被包含到结果中,但是它不会影响到how documents are scored
query–bool–should–match–field:target
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
- query+bool+must+filter+range+gte+lte
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
- aggregation query聚合查询
聚合查询,aggs;可以nested;"aggregations" : { "<aggregation_name>" : { "<aggregation_type>" : { <aggregation_body> } [,"meta" : { [<meta_data_body>] } ]? [,"aggregations" : { [<sub_aggregation>]+ } ]? } [,"<aggregation_name_2>" : { ... } ]*
}
```
- avg aggregation
POST /exams/_search?size=0
{
"aggs" : {
"avg_grade" : { "avg" : { "field" : "grade" } }
}
}
{
...
"aggregations": {
"avg_grade": {
"value": 75.0
}
}
}
- cardinality aggregation 基数聚集(大概数据);
基于hyperloglog算法。快速,类似于redis里的hyperloglog。对于大数据量的数据,计算大概值很快。
POST /sales/_search?size=0
{
"aggs" : {
"type_count" : {
"cardinality" : {
"field" : "type"
}
}
}
}
{
...
"aggregations" : {
"type_count" : {
"value" : 3
}
}
}
- precision control 精准控制,在cardinality上的精准控制
- extended_stats 扩展 的状态数据;
A multi-value metrics aggregation that computes stats over numeric values extracted from the aggregated documents.
The extended_stats aggregations is an extended version of the stats aggregation, where additional metrics are added such as sum_of_squares, variance, std_deviation and std_deviation_bounds.
GET /exams/_search
{
"size": 0,
"aggs" : {
"grades_stats" : { "extended_stats" : { "field" : "grade" } }
}
}
{
...
"aggregations": {
"grades_stats": {
"count": 2,
"min": 50.0,
"max": 100.0,
"avg": 75.0,
"sum": 150.0,
"sum_of_squares": 12500.0,
"variance": 625.0,
"std_deviation": 25.0,
"std_deviation_bounds": {
"upper": 125.0,
"lower": 25.0
}
}
}
}