问题描述
鉴于有一个标签集合的帖子的流行示例,我们假设我们希望每个标签不仅仅是一个字符串,而是一个字符串和一个double的元组,这意味着所有标签的强度。 p>
一个查询如何根据标签优势的总和发布和评分这些(假设我们正在标签名称中搜索确切的术语)
可以通过将标签索引为,然后使用查询结合查询。在下面的示例中,术语查询找到匹配的标签,自定义分数查询使用标签文档的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:{
标签:{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 b $ b}]
}
'
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' // localhost:9200 / test-idx / _refresh'
echo
#自定义脚本的示例(较慢但更易弹性)
curl -XGET'http:// localhost:9200 / test- idx / doc / _search?pretty = true'-d'{
query:{
ne sted:{
path:tags,
score_mode:total,
query:{
custom_score:{
查询:{
条款:{
标签:[A,B,D],
minimum_match:1
}
},
script:doc ['\''weight'\]。value
}
}
}
},
fields:[]
}'
echo
Given the popular example of a post that has a collection of tags, let's say that we would want each tag to be more than a string but a tuple of a string and a double which signifies the strength of said tag.
How would one query posts and score these based on the sum of tag strengths (let's assume we are searching for exact terms in the tags names)
It can be done by indexing tags as nested documents and then using the nested query in combination with the custom score query. In the example below, the terms query finds matching tags, the custom score query uses values of the "wight" field of "tags" documents as scores and the nested query is using sum of these scores as the final score for the top level document.
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
这篇关于弹性搜索 - 标记强度(嵌套/小孩文档提升)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!