0

如何更改我的查询以仅显示订单簿中的 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 个加内部命中。我该如何做才能仅返回 2 个内部点击,而不返回查询第一部分中订单簿附带的所有订单

GET /orderindex/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": { "_type": "orderbook" }
        },
        {
          "nested": {
            "path": "orders",
            "query": {
              "match_all": {}
            },
            "inner_hits": {
              "size": 2 
            }
          }
        }
      ]
    }
  }}
4

1 回答 1

3

内部命中支持以下选项:

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
            }
          }
        }
      ]
    }
  }
}

人们也可能想通过这样做来过滤_source结果:

"_source": {
        "includes": [ "obj1.*", "obj2.*" ],
        "excludes": [ "*.description" ]
    }

在您的订单情况下 - 排除可能很有用orders.*

关于这一点的更多信息 - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html

于 2018-05-17T10:00:17.630 回答