Elastic search - tagging strength (nested/child document boosting)

隐身守侯 提交于 2019-12-17 16:52:21

问题


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)


回答1:


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


来源:https://stackoverflow.com/questions/13095866/elastic-search-tagging-strength-nested-child-document-boosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!