弹性搜索 - 标记强度(嵌套/子文档增强)(Elastic search - tagging str

2019-07-02 18:14发布

由于有标签的集合后的通俗的例子,让我们说,我们希望每个标签比一个字符串,但字符串的元组和双这标志着该标签的强度更大。

如何将一个查询职位,这些得分基于标签的强度之和(假设我们在标签名称搜索准确的术语)

Answer 1:

它可以通过索引标签作为完成嵌套的文件 ,然后使用嵌套与组合查询定制比分查询。 在下面的例子中,术语查询查找匹配的标签,自定义分数查询使用“标签”的文件作为得分和嵌套查询的“怀特”字段的值,使用这些分数的总和作为顶层文件的最后得分。

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


文章来源: Elastic search - tagging strength (nested/child document boosting)