这个ElasticSearch查询的排名背后的原因是什么?

TIM*_*MEX 1 lucene indexing elasticsearch

我有两个文件:

{
    id: 7,
    title: 'Wet',
    description: 'asdfasdfasdf'
}

{
    id: 6
    title: 'Wet wet',
    description: 'asdfasdfasdf'
}
Run Code Online (Sandbox Code Playgroud)

它们几乎完全相同,除了第二个文件中的额外单词.

我的查询是这样的:

var qobject = {
        query:{
            custom_score:{
                query:{
                   multi_match:{
                     query: q, //I searched for "wet"
                     fields: ['title','description'],
                   }
                },
                script: '_score '
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

好的,所以当我运行这个查询时,我得到这些结果:

{ total: 2,
  max_score: 1.8472979,
  hits: 
   [ { _index: 'products',
       _type: 'products',
       _id: '7',
       _score: 1.9808292,
       _source: [Object] },
     { _index: 'products',
       _type: 'products',
       _id: '6',
       _score:  1.7508222,
       _source: [Object] } ] }
Run Code Online (Sandbox Code Playgroud)

为什么id 7的排名高于id 6?得分背后的原因是什么?不应该排名更高,因为它有2个单词吗?

如果我想要更多单词=更多权重怎么办?如何对我的查询进行修改?

说明如下:

"_explanation": {
            "value": 1.9808292,
            "description": "custom score, product of:",
            "details": [
                {
                    "value": 1.9808292,
                    "description": "script score function: composed of:",
                    "details": [
                        {
                            "value": 1.9808292,
                            "description": "fieldWeight(title:wet in 0), product of:",
                            "details": [
                                {
                                    "value": 1,
                                    "description": "tf(termFreq(title:wet)=1)"
                                },
                                {
                                    "value": 1.9808292,
                                    "description": "idf(docFreq=2, maxDocs=8)"
                                },
                                {
                                    "value": 1,
                                    "description": "fieldNorm(field=title, doc=0)"
                                }
                            ]
                        }
                    ]
                },
                {
                    "value": 1,
                    "description": "queryBoost"
                }
            ]
        }

"_explanation": {
            "value": 1.7508222,
            "description": "custom score, product of:",
            "details": [
                {
                    "value": 1.7508222,
                    "description": "script score function: composed of:",
                    "details": [
                        {
                            "value": 1.7508222,
                            "description": "fieldWeight(title:wet in 0), product of:",
                            "details": [
                                {
                                    "value": 1.4142135,
                                    "description": "tf(termFreq(title:wet)=2)"
                                },
                                {
                                    "value": 1.9808292,
                                    "description": "idf(docFreq=2, maxDocs=8)"
                                },
                                {
                                    "value": 0.625,
                                    "description": "fieldNorm(field=title, doc=0)"
                                }
                            ]
                        }
                    ]
                },
                {
                    "value": 1,
                    "description": "queryBoost"
                }
            ]
        }
Run Code Online (Sandbox Code Playgroud)

jav*_*nna 7

看看你的查询的解释输出,知道原因.您可以使用explain api或添加"explain": true到当前搜索请求中.

默认情况下,lucene使用tf/idf(术语频率,反向文档频率)相似性来评分文档.对于与查询匹配的每个术语,会考虑不同的因素.以下是最重要的:

  • 期限频率:该术语在文档中的频率.越多越好.如果术语出现多次,则文档更匹配.
  • 反向文档频率:该术语在整个索引中的频率.越少越好.罕见的条款赢得了普通条款.
  • 规范:索引时间提升(默认为1,无提升)+字段范数,它定义较短的字段优于长字段.

根据您正在执行的查询,由于规范,文档的得分也不同.您可以在映射(和重新索引)中禁用规范,但这样您也将失去索引时间提升(我认为您无论如何都不会使用它).实际上在你的例子中,第二个文档得分较低,因为较低的字段规范,尽管有较高的术语频率(2而不是1).

Antoher解决方案将插入不同的lucene相似性:lucene 4提供更多的相似性,并且还允许定义每个场的相似性.这些功能已在elasticsearch 0.90中公开.