0

我正在使用 ES v0.90.1。我希望能够使用其中一个字段来提升索引的特定类型的文档。如官方文档中所述,我这样定义了我的映射:

{
    "mappings": {
        "mytesttype": {
            "_boost": {
                "name": "doc_boost",
                "null_value": 1.0
            },
            "properties": {
                "date_start": {
                    "type": "date",
                    "format": "date_time"
                },
                "date_end": {
                    "type": "date",
                    "format": "date_time"
                }
            }
        }
    }
}

所以,在我看来,我的意思是我的索引将有一个类型,该类型具有一个名为默认值的mytesttype文档提升字段。doc_boost1

这是创建后索引的元数据:

{

    state: open
    settings: {
        index.number_of_shards: 1
        index.number_of_replicas: 0
        index.version.created: 900199
    }
    mappings: {
        mytesttype: {
            _boost: {
                null_value: 1
                name: doc_boost
            }
            properties: {
                date_end: {
                    format: date_time
                    type: date
                }
                date_start: {
                    format: date_time
                    type: date
                }
                y: {
                    type: long
                }
                x: {
                    type: long
                }
            }
        }
    }
    aliases: [ ]

}

然后我尝试索引两个文档:

{
    "ref": "ref-1",
    "date_start": "2013-07-01T00:00:00.000+0000",
    "date_end": "2016-07-01T00:00:00.000+0000",
    "y": 100,
    "x": 100,
    "doc_boost": 1.0
}

{
    "ref": "ref-2",
    "date_start": "2013-07-01T00:00:00.000+0000",
    "date_end": "2016-07-01T00:00:00.000+0000",
    "y": 100,
    "x": 100,
    "doc_boost": 2.0
}

doc_boost除了字段和ref值之外,这两个文档是相同的。

现在我的目标是做一个简单的请求,该请求将同时获取两个文档,但具有与doc_boost = 2. 所以这是我的要求:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "x": {
              "query": 100,
              "type": "boolean"
            }
          }
        },
        {
          "match": {
            "y": {
              "query": 100,
              "type": "boolean"
            }
          }
        },
        {
          "range": {
            "date_start": {
              "from": null,
              "to": "now",
              "include_lower": true,
              "include_upper": true
            }
          }
        },
        {
          "range": {
            "date_end": {
              "from": "now",
              "to": null,
              "include_lower": true,
              "include_upper": true
            }
          }
        }
      ]
    }
  }
}

我希望在ref-2文档上获得更高的分数,但这是我得到的响应以及解释输出:

{

    took: 3
    timed_out: false
    _shards: {
        total: 1
        successful: 1
        failed: 0
    }
    hits: {
        total: 2
        max_score: 2
        hits: [
            {
                _shard: 0
                _node: 99cl3dO9TFecp3fDiR3e6A
                _index: test_elasticsearchtest
                _type: mytesttype
                _id: mkwrfEswSj-T5x0c5AObuw
                _score: 2
                _source: {
                    ref: ref-1
                    date_start: 2013-07-01T00:00:00.000+0000
                    date_end: 2016-07-01T00:00:00.000+0000
                    y: 100
                    x: 100
                    doc_boost: 1
                }
                _explanation: {
                    value: 2
                    description: sum of:
                    details: [
                        {
                            value: 0.5
                            description: ConstantScore(x:[100 TO 100]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                        {
                            value: 0.5
                            description: ConstantScore(y:[100 TO 100]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                        {
                            value: 0.5
                            description: ConstantScore(date_start:[* TO 1374063073249]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                        {
                            value: 0.5
                            description: ConstantScore(date_end:[1374063073249 TO *]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                    ]
                }
            }
            {
                _shard: 0
                _node: 99cl3dO9TFecp3fDiR3e6A
                _index: test_elasticsearchtest
                _type: mytesttype
                _id: uvtIJ3n2RTad6CHnzENHgA
                _score: 2
                _source: {
                    ref: ref-2
                    date_start: 2013-07-01T00:00:00.000+0000
                    date_end: 2016-07-01T00:00:00.000+0000
                    y: 100
                    x: 100
                    doc_boost: 2
                }
                _explanation: {
                    value: 2
                    description: sum of:
                    details: [
                        {
                            value: 0.5
                            description: ConstantScore(x:[100 TO 100]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                        {
                            value: 0.5
                            description: ConstantScore(y:[100 TO 100]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                        {
                            value: 0.5
                            description: ConstantScore(date_start:[* TO 1374063073249]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                        {
                            value: 0.5
                            description: ConstantScore(date_end:[1374063073249 TO *]), product of:
                            details: [
                                {
                                    value: 1
                                    description: boost
                                }
                                {
                                    value: 0.5
                                    description: queryNorm
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }

}

两份文件的分数相同。有人可以向我解释我做错了什么吗?

4

1 回答 1

1

这里的问题是您没有执行任何全文搜索。正如您从解释输出中看到的那样,您的所有查询都映射到不涉及任何评分的范围查询。事实上他们只是匹配与否,你不能说多少,对吧?这就是您ConstantScoreQuery在解释输出中找到的原因,这就是为什么不考虑文档提升的原因。

当需要计算分数以说明文档与某个查询的匹配程度时,通常会考虑索引时间提升(可以在文档级别或每个字段)。您将在解释输出的字段规范部分中看到索引时间提升因子。

要解决您遇到的问题,我建议您不要使用索引时间提升。它不灵活,因为它需要重新索引您的文档才能更改它。我宁愿使用查询时间提升。elasticsearch 中有不同的查询可让您修改分数,请查看此其他问题以了解更多信息。

如果需要,您仍然可以依赖doc_boost文档中的字段,这意味着您仍然必须重新索引文档才能更改该值。您只需_boost要从映射中删除片段,因为您将在查询时应用提升因子。然后,您可以将查询包装到自定义分数查询中,并使用脚本来修改分数,例如将其乘以doc_boost.

"custom_score" : {
    "query" : {
        ....
    },
    "script" : "_score * doc['doc_boost'].value"
}
于 2013-07-17T13:00:04.827 回答