Elasticsearch -使用运行时字段脚本从嵌套类型内部的扁平字段中提取值

a64a0gku  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(1)|浏览(118)

我有这样一个Map,实际上是一个很大的文档,为简洁起见,排除了许多字段

{
    "items": {
        "mappings": {
            "dynamic": "false",
            "properties": {
                "id": {
                    "type": "keyword"
                },
                "costs": {
                    "type": "nested",
                    "properties": {
                        "id": {
                            "type": "keyword"
                        },
                        "costs_samples": {
                            "type": "flattened"
                        }
                    }
                }
            }
        }
    }
}

costs_samples是可能成本的平坦场类型的巨大集合(有时超过10 k个条目)。必须强调costs_sample不能位于costs之外,因为在查询时,costs中的一些conditions应该与shouldmatch子句组合在一起,比如(costs.country=this_value AND some_other costs_samples_condition。我希望能够在costs级别提取并最终注入一个新字段作为运行时字段,然后使用该字段进行排序、过滤和聚合。

{
 "runtime_mappings": {
    "costs.selected_cost": {
      "type": "long",
      "script": {
        "source":
         "for (def cost : doc['costs.costs_samples']) { if(cost.values!= null) {emit(Long.parseLong(cost.values.some_dynamic_identity_known_at_query_time.last))} }"
      }
    }
  },
    "query":{
        "nested": {
            "path": "costs",
            "query": {
                "bool": {
                    "filter": [
                        {
                            "terms": {
                             "costs.id": ["id-1","id-2"]
                             }   
                        },
                        {
                          "term": {
                              "costs.selected_cost": 10
                           } 
                        }
                
                    ]
                }
            
            }
        }
    },
    "fields": ["costs.selected_cost"]
}

问题是selected_cost它不是由ES创建/返回的。没有错误消息。我哪里做错了?文档没有帮助。也许值得一提的是,我也尝试了两个不同的文档,如itemscosts,然后执行某种连接操作,但性能测试真的很差。谢谢!

jaql4c8m

jaql4c8m1#

长话短说,没有办法在嵌套字段类型中使用运行时字段。

相关问题