我正在使用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
的文档增强字段,默认值为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_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
}
]
}
]
}
}
]
}
}
两个文件的分数相同。有人可以向我解释我做错了什么吗?
最佳答案
这里的问题是您没有执行任何全文本搜索。从说明输出中可以看到,所有查询都映射到范围查询,而不涉及任何评分。实际上,它们只是匹配还是不匹配,您不能说多少,可以吗?这就是为什么在说明输出中找到ConstantScoreQuery
的原因,这就是为什么不考虑文档提升的原因。
当需要计算分数以说明文档与某个查询匹配的数量时,通常会考虑使用索引时间提升(可以在文档级别或每个字段进行)。您将在说明输出的字段规范部分中看到索引时间提升因子。
要解决此问题,我建议您不要使用索引时间增加。它不灵活,因为需要重新索引文档才能进行更改。我宁愿使用查询时间增加。 elasticsearch中提供了不同的查询,这些查询可让您修改得分,看看this other question以了解更多信息。
如果需要,您仍然可以依赖文档中的doc_boost
字段,这意味着您仍然必须重新索引文档才能更改该值。您只需要从映射中删除_boost
片段,因为您将在查询时应用增强因子。然后,您可以将查询包装到custom score query中,并使用脚本来修改得分,例如将其乘以doc_boost
。
"custom_score" : {
"query" : {
....
},
"script" : "_score * doc['doc_boost'].value"
}
关于mapping - 定义文档提升字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17695612/