0

我正在使用以下查询对象进行多重匹配搜索script_score

{
    _source: [
        'baseline',
        'cpcr',
        'date',
        'description',
        'dev_status',
        'element',
        'event',
        'id'
    ],
    track_total_hits: true,
    query: {
       script_score: {
           query: {
               bool: {
                   filter: []
               },
           },
           script: {
               source: "def v=doc['description'].value; def score = 10000; score += v.length(); score -= " + "\"" + searchObject.query + "\"" + ".indexOf(v)*50;", // throws error
               params: { highlights: 3 }
           }
       }
    },
    highlight: { fields: { '*': {} } },
    sort: [],
    from: 0,
    size: 50
}

我希望结果按其突出显示匹配的数量排序。例如,第一条记录将有 5 个< em >,第二条记录将有 4 个< em >匹配,依此类推。目前我的结果不是这样排序的。

elasticsearch.config.ts

"settings": {
        "analysis": {
            "analyzer": {
                "search_synonyms": {
                    "tokenizer": "whitespace",
                    "filter": [
                        "graph_synonyms",
                        "lowercase",
                        "asciifolding"
                    ],
                }
            }
        }
    },

    "mappings": {
        "properties": {
            "description": {
                "type": "text",
                "analyzer": "search_synonyms"
            },
            "narrative": {
                "type":"object",
                "properties":{
                    "_all":{
                        "type": "text",
                        "analyzer": "search_synonyms"
                    }
                }
            },
        }
    }

样本数据

4

1 回答 1

0

我不认为这是可能的。

当您考虑它时,由于您使用的是multi_match,因此具有最多字段匹配的文档可能得分最高,这增加了他们也拥有最多<em>s 的机会。仍然可以hits对 num 进行后处理和排序。的发生。

不可能的原因是,highligting 机制sortAPI 之外工作,一个无法到达另一个。人们总是可以用一些花哨的脚本来“破解”它,但没有直接的方法可以做到这一点。


附录

查看此相关答案以访问脚本中的多个字段:https ://stackoverflow.com/a/61620705/8160318

于 2020-05-18T11:51:28.280 回答