Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

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

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...