我使用的是ES v0.90.1。我希望能够使用其中一个字段来提升索引的特定类型的文档。正如在the official documentation中所描述的,我这样定义了我的映射:
{
"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:
{
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
}
]
}
]
}
}
]
}
}这两个文档具有相同的分数。有人能给我解释一下我做错了什么吗?
发布于 2013-07-17 21:00:05
这里的问题是您没有执行任何全文搜索。正如您从explain输出中看到的,所有查询都映射到范围查询,这不涉及任何评分。事实上,他们只是匹配或不匹配,你不能说多少,不是吗?这就是在explain输出中找到ConstantScoreQuery的原因,也是不考虑文档boost的原因。
索引时间提升(可以是文档级别的索引时间提升,也可以是每个字段的索引时间提升)通常是在需要计算分数时考虑的,以便说明文档与特定查询的匹配程度。您将在explain输出的字段规范部分中看到索引时间提升因子。
为了解决您遇到的问题,我建议您不要使用索引时间提升。它不灵活,因为它需要重新索引您的文档才能对其进行更改。我更喜欢使用查询时间提升。在elasticsearch中有不同的查询可以让你修改分数,看看this other question可以了解更多。
如果愿意,您仍然可以依赖文档中的doc_boost字段,这意味着您仍然必须对文档重新编制索引才能更改该值。您只需要从映射中删除_boost片段,因为您将在查询时应用boost因子。然后,您可以将查询包装到一个custom score query中,并使用脚本修改分数,例如将其乘以doc_boost。
"custom_score" : {
"query" : {
....
},
"script" : "_score * doc['doc_boost'].value"
}https://stackoverflow.com/questions/17695612
复制相似问题