[英]ElasticSearch. How to get '_score' field of hit?
I have the following code : 我有以下代码:
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));
};
Because ES doesn't sort result on _score
field I need to do it in code. 因为ES不会在_score
字段上对结果进行排序, _score
我需要在代码中进行处理。 The result from ElasticSearch is resultOfElastic.hits.hits
and every hit has _score
field. ElasticSearch的结果为resultOfElastic.hits.hits
,每个命中都有_score
字段。 I try to use Lodash to order hits by _score
, but nothing happens. 我尝试使用Lodash通过命令命中_score
,但没有任何反应。 How to sort hits by field _score
? 如何按字段_score
排序匹配?
you can use bool query to sort on the basis of _sort 您可以使用布尔查询基于_sort进行排序
{
"from": 0,
"size": 50,
body: {
query: {
bool: {
must: [{
match:{
"firstName":${FIELD_PRIORITIES.FIRSTNAME}`,
"lastName":${FIELD_PRIORITIES.LASTNAME}`,
"username":${FIELD_PRIORITIES.USERNAME}`,
}
}, ],
should: [{
match:{
"meta": searchString
}
}, ]
},
},
sort: {
_score : {
"order": "desc"
}
},
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.