ElasticSearch无痛:在Map中找不到 * 的字段

cdmah0mi  于 2022-12-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(152)

我是ElasticSearch的新手,我试图找出为什么painless不能识别我的文档中存在的字段,但它抱怨说在Map中找不到该字段。
请注意,我使用的是ElastciSearch 8.5.3
我的索引Map如下所示:

{
    "companies": {
        "mapping": [
            "dynamic_templates": [
                // ... other properties here
                "branch_working_days_nested": {
                    "path_match": "branch.*.working_days",
                    "mapping": { "type": "nested" }
                }
            ],
            "properties": {
                "branch": {
                    "properties": {
                        // ... other properties here
                        "working_days": {
                            "properties": {
                                "friday": {
                                    "properties": {
                                        "close": { "type": "long" },
                                        "open": { "type": "long" },
                                        "isOpen": { "type": "long" },
                                    }
                                },
                                // The other week days here with same properties
                            }
                        }
                    }
                },
                // Other properties here
            }
        ]
    }

我索引的典型文档如下所示:

{
  // other properties here
  "branch": [
    {
      // other properties here
      "working_days": {
        "sunday": [
          {
            "open": -1,
            "close": -1,
            "id": "1YyMEHJe",
            "isOpen": false
          }
        ],
        "monday": [
          {
            "open": 830,
            "close": 1645,
            "id": "TkyMEHJe",
            "isOpen": true
          },
          {
            "open": 1800,
            "close": 2200,
            "id": "TkyMEHFa",
            "isOpen": true
          }
        ],
        // other days here
      }
    },
    {
      // other properties here
      "working_days": {
        "sunday": [
          {
            "open": -1,
            "close": -1,
            "id": "1YyMEHJe",
            "isOpen": false
          }
        ],
        "monday": [
          {
            "open": 1800,
            "close": 2200,
            "id": "TkyMEHFa",
            "isOpen": true
          }
        ],
        // other days here
      }
    }
  ],
  // other properties here
}

然后当我尝试执行这样的查询时:

GET companies/_search
{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "source": """
            def branches = doc['branch'];

            if (1 > 0) { return 1; } else { return 2; }
          """,
          "lang": "painless"
        },
        "order": "asc"
      }
    }
  ]
}

出现以下错误:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.5.3/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
          "org.elasticsearch.server@8.5.3/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:171)",
          "org.elasticsearch.server@8.5.3/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
          """branches = doc['branch'];
            """,
          "               ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 32,
          "start": 17,
          "end": 55
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "companies",
        "node": "HdHD67GdSTOeliS3pzI-hA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.5.3/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
            "org.elasticsearch.server@8.5.3/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:171)",
            "org.elasticsearch.server@8.5.3/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            """branches = doc['branch'];
            """,
            "               ^---- HERE"
          ],
          "script": " ...",
          "lang": "painless",
          "position": {
            "offset": 32,
            "start": 17,
            "end": 55
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [branch] in mapping"
          }
        }
      }
    ]
  },
  "status": 400
}

你知道我为什么会有这个问题吗?
值得一提的是,我曾尝试将branchMap为array,但这会返回另一个错误,因为在默认情况下,似乎所有字段都可以是数组。
我不知道我还能做什么。谁能帮帮我?

oknrviil

oknrviil1#

您无法访问,因为branch字段是嵌套类型,无法使用doc访问。
您可以使用下面的脚本,它将为您的用例工作.

GET companies/_search
{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "source": """
            def branches = params._source.branch;
            for (nested in params._source.branch) {
              def a= nested;
              // your bussiness logic
            }
            if (1 > 0) { return 1; } else { return 2; }
          """,
          "lang": "painless"
        },
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "total_element": {
      "script": {
        "source": "params._source.branch.length"
      }
    }
  }
}

相关问题