【问题标题】:Changing way of calculating score in results改变计算结果分数的方式
【发布时间】:2017-09-13 21:31:14
【问题描述】:

我的索引是这样创建的:

'body' => [
    'settings' => [
        'analysis' => [
            'filter' => [
                'ngram_filter' => [
                    'type' => 'ngram',
                    'min_gram' => 2,
                    'max_gram' => 20,
                ],
            ],
            'analyzer' => [
                'ngram_analyzer' => [
                    'type' => 'custom',
                    'tokenizer' => 'standard',
                    'filter' => [
                        'lowercase',
                        'ngram_filter',
                    ],
                ],
            ],
        ],
    ],
    'mappings' => [
        'doc' => [
            '_all' => [
                'type' => 'text',
                'analyzer' => 'ngram_analyzer',
                'search_analyzer' => 'standard',
            ],
            'properties' => [
                'pagetitle' => [
                    'type' => 'text',
                    'include_in_all' => true,
                    'term_vector' => 'yes',
                    'analyzer' => 'ngram_analyzer',
                    'search_analyzer' => 'standard',
                ],
                'searchable_content' => [
                    'type' => 'text',
                    'include_in_all' => true,
                    'term_vector' => 'yes',
                    'analyzer' => 'ngram_analyzer',
                    'search_analyzer' => 'standard',
                ],
            ],
        ],
    ],
],

我会寻找这样的结果:

GET my_index/_search
{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "Loesungen",
                    "fields": ["pagetitle^2", "searchable_content"],
                    "fuzziness": "AUTO"
                }
            },
            "filter": {
                "bool": {
                    "must": {
                        "term": {
                            "category.weight": 10
                        }
                    }
                }
            }
        }
    },
    "size": 3,
    "highlight": {
        "fields": {
            "pagetitle": {},
            "searchable_content": {}
        }
    },    
}

想要的效果:

  • pagetitle 中包含 work 的文档比 searchable_content 中包含 word 的文档更重要
  • 在 pagetitle 和 searchable_content 中都包含单词的文档比仅在 pagetitle 中包含该单词的文档更重要

但是当我搜索时,我得到的结果是这样的:

{
    "highlight": {
        "pagetitle": [
            "<em>Lösungen</em>"
        ]
    },
    "_score": 470.29608,
}, {
    "highlight": {
        "searchable_content": [
            "text <em>Lösungen</em> text"
        ],
        "pagetitle": [
            "<em>Lösungen</em>"
        ]
    },
    "_score": 441.84506
}

因此,您看到标题中仅包含单词的文档比标题和内容中包含该单词的文档得分更高。

问题是 - 应该进行哪些更改才能使其按我描述的那样工作?在查询中创建索引或其他内容?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    尝试在您的 multi_match 查询中使用 most_fields。 默认值为`best_fields。 在您的情况下,文档的这一部分似乎很有希望:

    ...通过结合所有三个字段的分数,我们可以匹配尽可能多的 文档尽可能使用主字段,但使用第二个和 第三个字段将最相似的结果推到列表顶部。

    另外,Explain API 对于调试相关性很有用(认为有点复杂)。

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html

    Query : GET my_index/_search
    {
        "query": {
            "bool": {
                "must": {
                    "multi_match": {
                        "query": "Loesungen",
                        "fields": ["pagetitle^2", "searchable_content"],
                        "fuzziness": "AUTO",
                        "type":       "most_fields"
                    }
                },
                "filter": {
                    "bool": {
                        "must": {
                            "term": {
                                "category.weight": 10
                            }
                        }
                    }
                }
            }
        },
        "size": 3,
        "highlight": {
            "fields": {
                "pagetitle": {},
                "searchable_content": {}
            }
        },    
    }
    

    【讨论】:

    • 我验证了您写的内容,结果顺序相同,但得分为 441.84506 的结果得到了更高的结果 459.42462。奇怪的是,当我写出确切的单词(Lösungen 而不是Loesungen)时,结果的顺序与预期一致
    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2018-11-14
    • 2019-12-22
    • 2019-07-10
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多