弹性嵌套查询 - 仅显示前 2 个内部命中
Elastic Nested query - display only the first 2 inner hits
如何更改我的查询以仅显示订单簿中的前 5 个订单?
我的数据是这样的结构。订单是嵌套类型。
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 个订单?
我现在正在发送此查询,但问题是它 return 正在处理包括所有订单在内的订单簿,然后 return 只有 2 个内部命中。我该如何处理 return 只有 2 个内部命中,而不包含来自查询第一部分的订单簿附带的所有订单
GET /orderindex/_search
{
"query": {
"bool": {
"must": [
{
"term": { "_type": "orderbook" }
},
{
"nested": {
"path": "orders",
"query": {
"match_all": {}
},
"inner_hits": {
"size": 2
}
}
}
]
}
}}
Inner hits support the following options:
size
The maximum number of hits to return per inner_hits. By default the
top three matching hits are returned.
这基本上意味着,您可以通过使用类似的查询来做到这一点
{
"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
如何更改我的查询以仅显示订单簿中的前 5 个订单?
我的数据是这样的结构。订单是嵌套类型。
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 个订单?
我现在正在发送此查询,但问题是它 return 正在处理包括所有订单在内的订单簿,然后 return 只有 2 个内部命中。我该如何处理 return 只有 2 个内部命中,而不包含来自查询第一部分的订单簿附带的所有订单
GET /orderindex/_search
{
"query": {
"bool": {
"must": [
{
"term": { "_type": "orderbook" }
},
{
"nested": {
"path": "orders",
"query": {
"match_all": {}
},
"inner_hits": {
"size": 2
}
}
}
]
}
}}
Inner hits support the following options:
size
The maximum number of hits to return per inner_hits. By default the top three matching hits are returned.
这基本上意味着,您可以通过使用类似的查询来做到这一点
{
"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