来自Apollo GraphQL客户端的Elasticsearch查询的大小始终为十

时间:2019-09-23 12:29:27

标签: javascript elasticsearch graphql apollo react-apollo

我正在使用GraphQL进行后端到前端的通信,并使用Elasticsearch作为数据库。问题是,无论何时我从前端调用查询,尽管我在查询中将大小指定为1000,但大小始终为10。当我使用返回正确命中数的GraphQL游乐场时,会发生此行为。

我的ES搜索请求是:

async rangeSessions(_, args) {
  const { dateFrom, dateTo } = args;      

  const results = await client.search({
    index: 'logs-cdigm',
    size: 1000,
    body: {
      aggs: {
        sessions: {
          terms: {
            field: 'session_id.keyword',
            size: 1000
          },
          aggs: {
            start_time: { min: { field: 'ts' } },
            end_time: { max: { field: 'ts' } },
            events_count: { value_count: { field: 'sequence_id' } },
          },
        },
      },
      query: {
        range: {
          timestamp: {
            lte: dateTo * 1000000,
            gt: dateFrom * 1000000,
          },
        },
      },
    },
  });

  const res = [];    
  console.log(results.body.aggregations.sessions.buckets.length);

  results.body.aggregations.sessions.buckets.forEach((bucket) => {
    res.push({
      session_id: bucket.key,
      events_count: bucket.doc_count,
      start_time: bucket.start_time.value_as_string,
      end_time: bucket.end_time.value_as_string,
    });
  });

  return res;
}

我的GraphQL查询是:

query sessionSearch ($from: Float!, $to: Float!) {
  rangeSessions(dateFrom: $from, dateTo: $to) {
    session_id
    start_time
    end_time
    events_count
  }
}

我的要求是:

// from frontend
{"operationName":"sessionSearch","variables":{"from":1567555200000,"to":1567728000000},"query":"query sessionSearch($from: Float!, $to: Float!) {\n  rangeSessions(dateFrom: $from, dateTo: $to) {\n    session_id\n    start_time\n    end_time\n    events_count\n    __typename\n  }\n}\n"}


{
  body: null,
  statusCode: null,
  headers: null,
  warnings: null,
  meta: {
   context: null,
    request: { params: [Object], options: [Object], id: 2 },
    name: 'elasticsearch-js',
    connection: {
      url: 'http://localhost:9200/',
      id: 'http://localhost:9200/',
      headers: {},
      deadCount: 0,
      resurrectTimeout: 0,
      _openRequests: 0,
      status: 'alive',
      roles: [Object]
    },
    attempts: 0,
    aborted: false
  }
}

// request object
{
  params: {
    method: 'POST',
    path: '/logs-cdigm/_search',
    body: '{"aggs":{"sessions":{"terms":{"field":"session_id.keyword","size":1000},"aggs":{"start_time":{"min":{"field":"ts"}},"end_time":{"max":{"field":"ts"}},"events_count":{"value_count":{"field":"sequence_id"}}}}},"query":{"range":{"timestamp":{"lte":1567728000000000000,"gt":1567555200000000000}}}}',
    querystring: 'size=1000',
    headers: {
      'User-Agent': 'elasticsearch-js/7.3.0 (linux 4.15.0-64-generic-x64; Node.js v12.10.0)',
      'Content-Type': 'application/json',
      'Content-Length': '293'
    }
  },
  options: { warnings: null },
  id: 2
}

如何在前端获得10个以上的结果?

1 个答案:

答案 0 :(得分:1)

Elasticsearch的结果数量默认为10。 您需要指定多少个结果(我相信有一个Max:10k?)

大小的位置很重要,它应该在您请求的正文中:

请参阅此处: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-from-size