elasticsearch,Mapping,elasticsearch" /> elasticsearch,Mapping,elasticsearch" />

Mapping 定义文档增强字段

Mapping 定义文档增强字段,mapping,elasticsearch,Mapping,elasticsearch,我使用的是ES v0.90.1。 我希望能够使用索引的某个字段来提升特定类型的文档。 如中所述,我将映射定义为: { "mappings": { "mytesttype": { "_boost": { "name": "doc_boost", "null_value": 1.0 }, "properties": {

我使用的是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\u boost
的文档增强字段,默认值为
1

以下是创建后索引的元数据:

{

    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\u boost
字段和
ref
值之外,这两个文档是相同的

现在,我的目标是执行一个简单的请求,该请求将获得两个文档,但得到的结果与
doc\u 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
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }

}

两份文件得分相同。有人能解释一下我做错了什么吗?

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

当需要计算分数以确定文档与某个查询的匹配程度时,通常会考虑索引时间提升(可以在文档级别或每个字段)。在explain输出的fieldnorm部分可以看到索引时间提升因子

为了解决这个问题,我建议你不要使用索引时间推进。这是不灵活的,因为它需要重新索引您的文档以更改它。我宁愿使用查询时间。elasticsearch中有不同的查询,允许您修改分数,请查看以了解更多信息

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

"custom_score" : {
    "query" : {
        ....
    },
    "script" : "_score * doc['doc_boost'].value"
}

谢谢你提出的详细问题。您介意在搜索请求中添加
explain=true
参数并发布结果吗?它应该解释这些分数背后的原因。@javanna我添加了解释激活的结果。我不知道这是可能的。谢谢你指出!谢谢你的解释。我真的不知道你需要全文搜索来考虑文档增强。我想我误解了分数是什么:我认为这更像是一个排名,而不是一个进球的实际分数(完美的查询匹配)。现在更有意义了。而自定义分数解决方案就像一个符咒。我认为这就是我们现在将要使用的,因为我们希望用它来管理优先级。再次感谢。