改变计算结果得分的方式

时间:2017-09-13 11:35:56

标签: elasticsearch

我的索引是这样创建的:

'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中工作的文档比searchable_content中带有单词的文档更重要
  • 在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
}

因此,当您在标题中看到包含单词的文档时,标题中包含此单词的文档得分会更高。

问题是 - 应该改变什么才能让它像我描述的那样工作?在查询中创建索引?

1 个答案:

答案 0 :(得分: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": {}
        }
    },    
}