我有以下代码:
export const getUserIds = async (search) => {
const requestBody = {
query_string: {
query: `*${search}*`,
fields: [
`firstName^${FIELD_PRIORITIES.FIRSTNAME}`,
`lastName^${FIELD_PRIORITIES.LASTNAME}`,
`username^${FIELD_PRIORITIES.USERNAME}`,
],
},
};
const resultOfElastic = await client.search({
...ES_CONFIG.INDEX,
...ES_CONFIG.USER.TYPE,
body: {
query: requestBody,
},
size: 1000,
});
const orderedHits = _.orderBy(resultOfElastic.hits.hits, ['_score', 'desc']);
return orderedHits.map((user) => stringToObjectId(user._id));
};
因为ES不在_score
字段上对结果进行排序,所以我需要在代码中进行处理。 ElasticSearch的结果为resultOfElastic.hits.hits
,每个匹配项都有_score
字段。我尝试使用Lodash来按_score
排序匹配,但是什么也没发生。如何按字段_score
对匹配进行排序?