基本知识:返回结果参数设置

返回结果属性:
took:查询结果花费的时间(ms)
timed_out:是否超时
_shards:分片信息:{total:总共的分片数,successful:成功查询的分片数:skipped跳过查询的分片数,failed:查询失败的分片数}
hits:返回结果信息:{ total:返回的结果总数, max_score:最大文档分数,hits:返回真实的结果:{ _index:索引, _type:类型, _id:id, _score:文档分数, _source:文档的fields } }
_score 称为文档分数(搜索结果中的_score字段)。分数是一个数字值,它是文档与我们指定的搜索查询匹配程度的相对度量。分数越高,文档越相关,分数越低,文档的相关性越低。

response filtering(响应过滤)
所有REST API接受一个filter_path参数,可以用来减少elasticsearch返回的响应。 此参数以逗号分隔的以点表示法表示的过滤器列表:
例如:1.
curl -XGET 'localhost:9200/_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score&pretty'

Responds:
{
  "took" : 3,
  "hits" : {
    "hits" : [
      {
        "_id" : "0",
        "_score" : 1.6375021
      }
    ]
  }
}
2.它还支持*通配符匹配任何字段或字段名称的一部分:
curl -XGET 'localhost:9200/_cluster/state?filter_path=metadata.indices.*.stat*&pretty'

Responds
{
  "metadata" : {
    "indices" : {
      "twitter": {"state": "open"}
    }
  }
}

3.并且**通配符可用于包括不知道确切路径的fields。 
curl -XGET 'localhost:9200/_cluster/state?filter_path=routing_table.indices.**.state&pretty'

Responds:
{
  "routing_table": {
    "indices": {
      "twitter": {
        "shards": {
          "0": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
          "1": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
          "2": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
          "3": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
          "4": [{"state": "STARTED"}, {"state": "UNASSIGNED"}]
        }
      }
    }
  }
}

4.你也可以通过字符“-”作为前缀去排除一个或多个fields:
GET /_count?filter_path=-_shards
Responds:
   {
  "count" : 5
}

5.为了更多的控制,包含和排他的过滤器可以在同一表达式中组合。 在这种情况下,将首先应用排他过滤器,并使用包含的过滤器再次对结果进行过滤:
curl -XGET 'localhost:9200/_cluster/state?filter_path=metadata.indices.*.state,-metadata.indices.logstash-*&pretty'

Responds:
{
  "metadata" : {
    "indices" : {
      "index-1" : {"state" : "open"},
      "index-2" : {"state" : "open"},
      "index-3" : {"state" : "open"}
    }
  }
}


Keyed Response

将“key”设置为true("keyed": true)可将唯一的字符串键与每个存储桶相关联,并将范围作为散列值而不是数组返回:
1.
curl -XPOST 'localhost:9200/sales/_search?size=0&pretty' -H 'Content-Type: application/json' -d'
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "date",
                "format": "MM-yyy",
                "ranges": [
                    { "to": "now-10M/M" },
                    { "from": "now-10M/M" }
                ],
                "keyed": true
            }
        }
    }
}
'
Response:
{
    ...
    "aggregations": {
        "range": {
            "buckets": {
                "*-10-2015": {
                    "to": 1.4436576E12,
                    "to_as_string": "10-2015",
                    "doc_count": 7
                },
                "10-2015-*": {
                    "from": 1.4436576E12,
                    "from_as_string": "10-2015",
                    "doc_count": 0
                }
            }
        }
    }
}
2.也可以自定义每个范围的键:
POST /sales/_search?size=0
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "date",
                "format": "MM-yyy",
                "ranges": [
                    { "from": "01-2015",  "to": "03-2015", "key": "quarter_01" },
                    { "from": "03-2015", "to": "06-2015", "key": "quarter_02" }
                ],
                "keyed": true
            }
        }
    }
}
Response:
{
    ...
    "aggregations": {
        "range": {
            "buckets": {
                "quarter_01": {
                    "from": 1.4200704E12,
                    "from_as_string": "01-2015",
                    "to": 1.425168E12,
                    "to_as_string": "03-2015",
                    "doc_count": 5
                },
                "quarter_02": {
                    "from": 1.425168E12,
                    "from_as_string": "03-2015",
                    "to": 1.4331168E12,
                    "to_as_string": "06-2015",
                    "doc_count": 2
                }
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值