gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:47:38 27 4
gpt4 key购买 nike

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

我的 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 个结果?

最佳答案

Elasticsearch 默认的结果数是 10。您需要指定有多少结果(我相信最大:10k?)

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

看这里: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-from-size

关于javascript - 来自 Apollo GraphQL 客户端的 Elasticsearch 查询的大小始终为十,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58062478/

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