我有一个问题,如何获得找到的文档(嵌入文档(的集合项目的分数(_score
(。
例如,我正在寻找具有特定零件的产品。所以我想得到哪个部分最匹配的信息。
准确地说:我不想限制结果集中内部文档(产品部件(的数量。我只需要知道哪个部分最匹配。
我的映射:
"products": {
"properties": {
"Id": {
"type": "string"
},
"Name": {
"type": "string"
},
"RelationshipId": {
"type": "string"
},
"Parts": {
"properties": {
"PartName": {
"type": "string"
},
"CompanyId": {
"type": "string"
},
"IsPrimary": {
"type": "boolean"
}
}
}
}
}
我的查询结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "localhost",
"_type": "products",
"_id": "9AEEA518-D4F1-E611-8274-8CDCD44D0F98",
"_score": 1,
"_source": {
"Id": "9AEEA518-D4F1-E611-8274-8CDCD44D0F98",
"Name": "Test1",
"RelationshipId": "B51AA2C8-D3F1-E611-8274-8CDCD44D0F98",
"Parts": [
{
"PartName": "abc 1",
"CompanyId": "9EEEA518-D4F1-E611-8274-8CDCD44D0F98",
"IsPrimary": "1"
},
{
"PartName": "wer 2",
"CompanyId": "BAAF7E32-D4F1-E611-8274-8CDCD44D0F98",
"IsPrimary": "0"
}
]
}
}
]
}
}
事实证明,唯一的解决方案是使用nested
集合。没有它,集合中的所有文档都会扁平化为一组"变量",您无法知道命中来自哪个文档。
若要将嵌套集合与每个元素的score
一起使用,请执行以下操作:
1( 使用以下条目将内部集合转换为nested
:"type": "nested"
2( 调整查询以使用运算符nested
。
3( 要查询(使用嵌套集合的内容(,请"inner_hits" : {}
条目。
4(使用查询结果,score
将被"隐藏"在路径中:hits.hits.inner_hits.COLLECTION_NAME.hits
。解析它并通过Id
与主结果连接(并非每个集合项都可能已被命中(。使用每个内部文档score
值对主结果集中的源集合进行排序。
就是这样。详细信息在文档中。