文档没有字段的价值!使用doc [< field>]。size()== 0检查文档是否缺少字段

发布于 2025-01-29 17:11:32 字数 5370 浏览 3 评论 0 原文

当我在查询之后运行时:

GET /annotations/_search
{
  "query": {
    "match_all": {}
  }
}

我会收到以下响应:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "annotations",
        "_type" : "_doc",
        "_id" : "I9nlA",
        "_score" : 1.0,
        "_source" : {
          "preferences" : {
            "id" : 1,
            "annotation_id" : "I9nlA",
            "answer_timer" : 24
          }
         // other fields here
        }
      },
      {
        "_index" : "annotations",
        "_type" : "_doc",
        "_id" : "XIP6L",
        "_score" : 1.0,
        "_source" : {
          "id" : "XIP6L",
          "preferences" : {
            "id" : 2,
            "annotation_id" : "XIP6L",
            "answer_timer" : 5
          },
        // other fields here
        }
      }
    ]
  }
}

如您所见,字段首选项没有空值。 我的问题是,当我基于首选项字段添加脚本字段时,它会引起错误。 查询:

GET /annotations/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "can_answer": {
      "script": {
        "source": """
          if (doc['creator_id'].value == params['user_id']){
            return true;
          }
          else{
            String nowString = params['now'];
            ZonedDateTime now = ZonedDateTime.parse(nowString);
            ZonedDateTime created = doc['created'].value;
            ZonedDateTime createdPlusAnswerTimer = created.plusHours(
              doc['preferences.answer_timer'].value
            );
            Duration d = Duration.between(now, createdPlusAnswerTimer);
            return d.toHours() > 0;
            }
        """,
        "params": {
          "user_id": 1,
          "now": "2022-05-17T16:17:49.366Z"
        }
      }
    }
  }
}

错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
          "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
          "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
          "createdPlusAnswerTimer = created.plusHours(\n              doc['preferences.answer_timer'].value\n            );\n            Duration ",
          "                                                                                         ^---- HERE"
        ],
        "script" : " ...",
        "lang" : "painless",
        "position" : {
          "offset" : 398,
          "start" : 309,
          "end" : 441
        }
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "annotations",
        "node" : "_ljs4uLdR7eBjL8ioYLqAg",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
            "createdPlusAnswerTimer = created.plusHours(\n              doc['preferences.answer_timer'].value\n            );\n            Duration ",
            "                                                                                         ^---- HERE"
          ],
          "script" : " ...",
          "lang" : "painless",
          "position" : {
            "offset" : 398,
            "start" : 309,
            "end" : 441
          },
          "caused_by" : {
            "type" : "illegal_state_exception",
            "reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
          }
        }
      }
    ]
  },
  "status" : 400
}

它说 doc ['preverences.answer_timer']。值有一些问题,但我不知道为什么。该字段在所有文档中都有价值。

问题是什么,我该如何解决? 谢谢。

更新:添加映射:

 {
  "annotations" : {
    "mappings" : {
      "properties" : {
        
        // other fields
        
        "preferences" : {
          "type" : "nested",
          "properties" : {
            "annotation_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "answer_timer" : {
              "type" : "long"
            },
            "id" : {
              "type" : "long"
            }
          }
        }
        
        // other fields
        
      }
    }
  }
}

注意 当我使用 params._source.preferences.answer_timer 时,它可以正常工作。但是在Elasticsearch文档中,有人说使用 _Source.Field 非常慢,所以我不想使用它。

when I run following query:

GET /annotations/_search
{
  "query": {
    "match_all": {}
  }
}

I receive following response:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "annotations",
        "_type" : "_doc",
        "_id" : "I9nlA",
        "_score" : 1.0,
        "_source" : {
          "preferences" : {
            "id" : 1,
            "annotation_id" : "I9nlA",
            "answer_timer" : 24
          }
         // other fields here
        }
      },
      {
        "_index" : "annotations",
        "_type" : "_doc",
        "_id" : "XIP6L",
        "_score" : 1.0,
        "_source" : {
          "id" : "XIP6L",
          "preferences" : {
            "id" : 2,
            "annotation_id" : "XIP6L",
            "answer_timer" : 5
          },
        // other fields here
        }
      }
    ]
  }
}

As you can see, the field Preferences has no null values.
My problem is that when I add a scripted field based on the Preferences field, it raises an error.
The query:

GET /annotations/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "can_answer": {
      "script": {
        "source": """
          if (doc['creator_id'].value == params['user_id']){
            return true;
          }
          else{
            String nowString = params['now'];
            ZonedDateTime now = ZonedDateTime.parse(nowString);
            ZonedDateTime created = doc['created'].value;
            ZonedDateTime createdPlusAnswerTimer = created.plusHours(
              doc['preferences.answer_timer'].value
            );
            Duration d = Duration.between(now, createdPlusAnswerTimer);
            return d.toHours() > 0;
            }
        """,
        "params": {
          "user_id": 1,
          "now": "2022-05-17T16:17:49.366Z"
        }
      }
    }
  }
}

The error:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "script_stack" : [
          "org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
          "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
          "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
          "createdPlusAnswerTimer = created.plusHours(\n              doc['preferences.answer_timer'].value\n            );\n            Duration ",
          "                                                                                         ^---- HERE"
        ],
        "script" : " ...",
        "lang" : "painless",
        "position" : {
          "offset" : 398,
          "start" : 309,
          "end" : 441
        }
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "annotations",
        "node" : "_ljs4uLdR7eBjL8ioYLqAg",
        "reason" : {
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
            "createdPlusAnswerTimer = created.plusHours(\n              doc['preferences.answer_timer'].value\n            );\n            Duration ",
            "                                                                                         ^---- HERE"
          ],
          "script" : " ...",
          "lang" : "painless",
          "position" : {
            "offset" : 398,
            "start" : 309,
            "end" : 441
          },
          "caused_by" : {
            "type" : "illegal_state_exception",
            "reason" : "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
          }
        }
      }
    ]
  },
  "status" : 400
}

It said there is some problems with doc['preferences.answer_timer'].value, but I don't know why. This field has value in all documents.

What is the problem and how can I solve it?
Thanks.

UPDATE: Mapping added:

 {
  "annotations" : {
    "mappings" : {
      "properties" : {
        
        // other fields
        
        "preferences" : {
          "type" : "nested",
          "properties" : {
            "annotation_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "answer_timer" : {
              "type" : "long"
            },
            "id" : {
              "type" : "long"
            }
          }
        }
        
        // other fields
        
      }
    }
  }
}

NOTE
when I use params._source.preferences.answer_timer, it works fine. But in the Elasticsearch documentation, it's been said that using _source.field is very slow, so I don't want to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2025-02-05 17:11:32

由于首选项字段已嵌套,因此我们需要在嵌套查询的上下文中调用脚本。父文档没有DOC_VALUE字段 - 因此出现错误。参考:

解决方案1:更改 preceences 类型to to to object 而不是嵌套。
解决方案2:在嵌套上下文中使用查询。

Since the preferences field is nested, we need to call the script in the context of the nested query. The parent document does not have a doc_value field - Thus giving an error. Ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html

Solution 1: Change preferences type to object instead of nested.
Solution 2: use query in nested context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文