从inner_hits过滤对象中检索字段值抛出错误

时间:2016-10-25 00:54:16

标签: elasticsearch elasticsearch-2.0 elasticsearch-dsl

在我的映射文档中,我有一个嵌套的对象字段,如下所示

"availabilities":{                      
  "type": "nested",                     
  "dynamic": "strict",                      
  "properties": {                     
    "start":                        { "type": "date",   "format": "yyyy-MM-dd"  },
    "end":                          { "type": "date",   "format": "yyyy-MM-dd"  },
    "age":                          { "type": "integer"                         }
  }                     
}

我有一个很长的DSL查询,其中一个过滤器是:

{
  "nested": {
    "path": "availabilities",
    "inner_hits" : {
      "size": 1,
      "name": "selected_availabilities"
    },
    "query": {
      "bool": {
        "must": [
          {
            "range": {
              "availabilities.start": {
                "gte": "2016-10-08",
                "lte": "2016-10-08"
              }
            }
          },
          {
            "range": {
              "availabilities.end": {
                "gte": "2016-10-17",
                "lte": "2016-10-17"
              } 
            }
          }
        ]
      }
    }
  }
}

我正在尝试使用inner_hits获取所选的可用性对象,并返回如下内容:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 6000,
    "hits": [
      {
        "_index": "listings_v1",
        "_type": "listing",
        "_id": "228527",
        "_score": 6000,
        "_source": {
          ...my fields....
          ...my fields....
          ...my fields....
          ...my fields....
          ...availabilities has nested objects.....
        },
        "inner_hits": {
          "selected_availabilities": {
            "hits": {
              "total": 1,
              "max_score": 1.4142135,
              "hits": [
                {
                  "_type": "listing",
                  "_id": "228527",
                  "_nested": {
                    "field": "availabilities",
                    "offset": 3
                  },
                  "_score": 1.4142135,
                  "_source": {
                    "start": "2016-10-08",
                    "end": "2016-10-17",
                    "age": 23
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

我的目标是使用inner_hits中所选对象的一个​​文件来计算分数。 由于可用性对象可能有多个对象,但总会有一个匹配我的搜索条件。这就是我的查询:

function_score": {
  "query": {},
  "score_mode": "sum",
  "boost_mode": "replace"
  "functions": [
    {
      "script_score": {
        "params": {
          "move_in_date_boost": -1350,
          "desired_move_in_date": "2016-11-03"
        },
        "script": "return (inner_hits['selected_availabilities']['hits']['hits'][0]['_source']['age']);" 
      }
    },
    {
      ....more functions...
    }
  ]
}

但是当我使用上面的脚本时,我收到以下错误:

{
  "took": 239,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 4,
    "failed": 1,
    "failures": [
      {
        "shard": 0,
        "index": "xyz_v1",
        "node": "hgu7no8oo9wwe34wetw",
        "reason": {
          "type": "script_exception",
          "reason": "failed to run inline script [return (inner_hits['selected_availabilities']['hits']['hits'][0]['_source']['age']);] using lang [groovy]",
          "caused_by": {
            "type": "missing_property_exception",
            "reason": "missing_property_exception: No such property: inner_hits for class: 572da4fc5f5e591a0d7cfec2cde0c998b550b1f4"
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

如何在分数计算中获得selected_availabilities age字段?任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

尝试使用

return (doc['inner_hits']...

而不是

return (inner_hits...