无法使用field_value_factor

时间:2015-08-14 09:21:25

标签: elasticsearch

这是映射:

PUT books-index
{
  "mappings": {
    "books": {
      "properties": {
        "tags": {
          "type": "nested",
          "fields": {
            "name": {
              "type": "string"
            },
            "weight": {
              "type": "float"
            }
          }
        }
      }
    }
  }
}

然后使用field_value_factor执行嵌套查询失败并显示错误

GET books-index/books/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "score_mode": "sum",
      "query": {
        "function_score": {
          "query": {
            "match": {
              "tags.name": "world"
            }
          },
        "field_value_factor": {
            "field": "weight"
         }
        }
      }
    }
  }
}

错误:"嵌套:ElasticsearchException [无法找到字段[权重]的字段映射器]"

有趣的是,如果索引中有一本带有标签的图书 - 没有错误且查询效果很好。

为什么会这样?如果索引中没有带标签的书籍,我该如何防止错误?

有什么想法吗?

谢谢!

P.S。 github还有一个问题:https://github.com/elastic/elasticsearch/issues/12871

1 个答案:

答案 0 :(得分:1)

看起来您的映射不正确。

PUT您提供的映射后,尝试执行GET books-index/_mapping,它会显示以下结果:

"books-index": {
  "mappings": {
     "books": {
        "properties": {
           "tags": {
              "type": "nested"
           }
        }
     }
  }
}

它缺少名字和重量!映射的问题在于您使用的是fields而不是properties,或者您忘记输入第二个properties密钥。

我修改了您的映射,以反映您在标记中查找嵌套名称和类型,因为它看起来就像您的查询所需。

PUT books-index
{
  "mappings": {
    "books": {
      "properties": {
        "tags": {
          "type": "nested",
          "properties": {               // <--- HERE!
            "name": {
              "type": "string"
            },
            "weight": {
              "type": "float"
            }
          }
        }
      }
    }
  }
}