如何更改查询以仅显示订单中的5个第一个订单?
我的数据是这样的结构。 Order是嵌套类型。
Orderbook
|_ Orders
这是我的查询
GET /orderindex/_search
{
"size": 10,
"query": {
"term": { "_type": "orderbook" }
}
}
这是结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 1,
"hits": [
{
"_index": "orderindex",
"_type": "orderbook",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"exchange": "Exch1",
"label": "USD/CAD",
"length": 40,
"timestamp": "5/16/2018 4:33:31 AM",
"orders": [
{
"pair1": "USD",
"total": 0.00183244,
"quantity": 61,
"orderbookId": 0,
"price": 0.00003004,
"exchange": "Exch1",
"id": 5063,
"label": "USD/CAD",
"pair2": "CAD",
},
{
"pair1": "USD",
"total": 0.0231154,
"quantity": 770,
"orderbookId": 0,
"price": 0.00003002,
"exchange": "Exch1",
"id": 5064,
"label": "USD/CAD",
"pair2": "CAD",
},
...
..
.
另外,如何通过标签名称查询两个特定的订单并仅检索前两个订单?
我现在正在发送此查询,但问题是它返回订单包括其所有订单,然后在此之后它只返回2加内部命中。如何在没有查询第一部分订单簿附带的所有订单的情况下仅返回2个内部点击
GET /orderindex/_search
{
"query": {
"bool": {
"must": [
{
"term": { "_type": "orderbook" }
},
{
"nested": {
"path": "orders",
"query": {
"match_all": {}
},
"inner_hits": {
"size": 2
}
}
}
]
}
}}
答案 0 :(得分:1)
内部命中支持以下选项:
size
每个inner_hits返回的最大匹配数。默认情况下 返回前三个匹配的匹配。
这基本上意味着,你可以通过使用类似的查询
来做到这一点 {
"query": {
"bool": {
"must": [
{
#replace this one with your query for orderbook
"term": {
"user": "kimchy"
}
},
{
"nested": {
"path": "orders",
"query": {
"match_all": {}
},
"inner_hits": {
"size": 3 #we asks for only 3 inner hits
}
}
}
]
}
}
}
通过这样做,人们也希望filter _source
得到结果:
"_source": {
"includes": [ "obj1.*", "obj2.*" ],
"excludes": [ "*.description" ]
}
就您的订单而言 - 排除orders.*
有关此问题的更多信息 - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html