【发布时间】:2021-02-06 22:48:46
【问题描述】:
我需要返回一个包含尽可能多用户提供的标签的帖子的评分列表。标签作为对象存储在数组中,用户请求包含标签 ID 数组(例如["a", "c"])。单个文档看起来像这样:
{
id: 1,
meta: {
tags: [{id: "a", name: "Engine"}, {id: "b", name: "Street"}, {id: "c", name: "Sport"}]
}
}
遗憾的是,term 查询以几乎随机的顺序返回帖子,因为所有帖子的得分均为 1.0:
{
"query": {
"bool": {
"must": [
{
"terms": {
"meta.tags.id.keyword": [
"a", "c"
]
}
}
]
}
}
}
我认为如果所有标签都在一个字符串中,事情会很容易,因为这将是正常的全文搜索,但是如何为对象数组实现这样的事情呢?
这是一个映射异常(由 NEST 客户端动态创建):
"mappings" : {
"properties" : {
"categories" : {
"properties" : {
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"inFavourite" : {
"type" : "long"
},
"likes" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"photoPath" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"meta" : {
"properties" : {
"tags" : {
"properties" : {
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
}
},
}
},
"userId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"viewCounter" : {
"type" : "long"
}
}
}
【问题讨论】:
-
好问题,您是否介意分享您的索引映射、少量示例和预期文档,以便社区轻松重现您的问题并为您的数据集提供有效的解决方案。
-
我添加了一个映射。文档很大,所以最好处理一些自定义示例。
标签: elasticsearch