嵌套聚合的脚本失败

时间:2018-03-08 10:26:04

标签: elasticsearch

我尝试使用脚本在嵌套模型上进行聚合,但它失败了。

我做错了什么?

我的查询

GET myindex/mymodel/_search
{
  "query": {
    "multi_match": {
      "query": "Some Name",
      "fields": ["name^9", "description^6", "categories.name"],
      "operator": "and"
    }
  },
  "aggs": {
    "categories_aggregation": {
      "nested": {
        "path": "categories"},
        "aggs": {
            "id_and_name":  {
              "terms": {
                "script": {
                    "source": "doc['categories.slug'].value + '|' + doc['categories.name'].value"
                } 
              } 
           }
        }
    }
  }
}

我的一个条目:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 648,
    "max_score": 1,
    "hits": [
      {
        "_index": "myindex",
        "_type": "mymodel",
        "_id": "5a62180c9d1fa22ba988a799",
        "_score": 1,
        "_source": {
          "name": "Some Name",
          "categories": [
            {
              "_id": {
                "$oid": "5a50eac49d1fa22c095fb070"
              },
              "name": "Category Name",
              "slug": "category-name"
            }
          ]
        }
      }
    ]
  }
}

错误:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
          "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
          "doc['categories.name'].value",
          "    ^---- HERE"
        ],
        "script": "doc['categories.name'].value",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "myindex",
        "node": "lZ0pbatxQmmL5ZvJ5UMCwQ",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
            "doc['categories.name'].value",
            "    ^---- HERE"
          ],
          "script": "doc['categories.slug'].value + '|' + doc['categories.name'].value",
          "lang": "painless",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [categories.name] in mapping with types [mymodel]"
          }
        }
      }
    ]
  },
  "status": 500
}

编辑:

我的映射:

{
  "myindex": {
    "mappings": {
      "mymodel": {
        "properties": {
          "categories": {
            "type": "nested",
            "properties": {
              "_id": {
                "properties": {
                  "$oid": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  }
                }
              },
              "id": {
                "type": "text"
              },
              "name": {
                "type": "text"
              },
              "slug": {
                "type": "text"
              },
              "slugs": {
                "type": "text"
              }
            }
          },

          "name": {
            "type": "text",
            "analyzer": "english"
          },

        }
      }
    }
  }
}

0 个答案:

没有答案
相关问题