首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >定义文档boost字段

定义文档boost字段
EN

Stack Overflow用户
提问于 2013-07-17 17:06:55
回答 1查看 100关注 0票数 0

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

代码语言:javascript
复制
{
    "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_boost的document boost字段,缺省值为1

下面是索引创建后的meta:

代码语言:javascript
复制
{

    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: [ ]

}

然后,我尝试对两个文档进行索引:

代码语言:javascript
复制
{
    "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的结果一样高。所以我的要求是:

代码语言:javascript
复制
{
  "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文档上获得更高的分数,但以下是我得到的响应,以及解释输出:

代码语言:javascript
复制
{

    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
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }

}

这两个文档具有相同的分数。有人能给我解释一下我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-17 21:00:05

这里的问题是您没有执行任何全文搜索。正如您从explain输出中看到的,所有查询都映射到范围查询,这不涉及任何评分。事实上,他们只是匹配或不匹配,你不能说多少,不是吗?这就是在explain输出中找到ConstantScoreQuery的原因,也是不考虑文档boost的原因。

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

为了解决您遇到的问题,我建议您不要使用索引时间提升。它不灵活,因为它需要重新索引您的文档才能对其进行更改。我更喜欢使用查询时间提升。在elasticsearch中有不同的查询可以让你修改分数,看看this other question可以了解更多。

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

代码语言:javascript
复制
"custom_score" : {
    "query" : {
        ....
    },
    "script" : "_score * doc['doc_boost'].value"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17695612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档