gpt4 book ai didi

json - 弹性嵌套查询-仅显示前2个内部匹配

转载 作者:行者123 更新时间:2023-12-03 00:52:47 25 4
gpt4 key购买 nike

如何更改查询以仅显示订单簿中的前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个以上的内部匹配数。我该怎么做才能只返回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

关于json - 弹性嵌套查询-仅显示前2个内部匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50380934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com