gpt4 book ai didi

nosql - Elasticsearch - 标记强度(嵌套/子文档增强)

转载 作者:行者123 更新时间:2023-11-29 02:53:51 25 4
gpt4 key购买 nike

给出具有标签集合的帖子的流行示例,假设我们希望每个标签不仅仅是一个字符串,而是一个字符串元组和一个表示所述标签强度的 double 。

一个查询如何根据标签强度的总和来发布和评分(假设我们正在搜索标签名称中的确切术语)

最佳答案

可以通过将标签索引为 nested documents 来完成然后使用 nested结合 custom score 进行查询询问。在下面的示例中,术语查询找到匹配的标签,自定义分数查询使用“标签”文档的“wight”字段的值作为分数,嵌套查询使用这些分数的总和作为顶级文档的最终分数.

curl -XDELETE 'http://localhost:9200/test-idx'
echo
curl -XPUT 'http://localhost:9200/test-idx' -d '{
"mappings": {
"doc": {
"properties": {
"title": { "type": "string" },
"tags": {
"type": "nested",
"properties": {
"tag": { "type": "string", "index": "not_analyzed" },
"weight": { "type": "float" }
}
}
}
}
}
}'
echo
curl -XPUT 'http://localhost:9200/test-idx/doc/1' -d '{
"title": "1",
"tags": [{
"tag": "A",
"weight": 1
}, {
"tag": "B",
"weight": 2
}, {
"tag": "C",
"weight": 4
}]
}
'
echo
curl -XPUT 'http://localhost:9200/test-idx/doc/2' -d '{
"title": "2",
"tags": [{
"tag": "B",
"weight": 2
}, {
"tag": "C",
"weight": 3
}]
}
'
echo
curl -XPUT 'http://localhost:9200/test-idx/doc/3' -d '{
"title": "3",
"tags": [{
"tag": "B",
"weight": 2
}, {
"tag": "D",
"weight": 4
}]
}
'
echo
curl -XPOST 'http://localhost:9200/test-idx/_refresh'
echo
# Example with custom script (slower but more flexable)
curl -XGET 'http://localhost:9200/test-idx/doc/_search?pretty=true' -d '{
"query" : {
"nested": {
"path": "tags",
"score_mode": "total",
"query": {
"custom_score": {
"query": {
"terms": {
"tag": ["A", "B", "D"],
"minimum_match" : 1
}
},
"script" : "doc['\''weight'\''].value"
}
}
}
},
"fields": []
}'
echo

关于nosql - Elasticsearch - 标记强度(嵌套/子文档增强),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13095866/

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