Elasticsearch:按带子字段的脚本排序

时间:2019-06-19 04:12:27

标签: node.js elasticsearch elasticsearch-6

我的数据如下:

{
    "name": "Data 1",
    "ancestor" : {
        "parent" : {
            "name" : "Apple",
            "date" : ISODate("2016-04-21T03:28:08.493Z")
        }
    }
},
{
    "name": "Data 2",
    "ancestor" : {
        "parent" : {
            "name" : "Google",
            "date" : ISODate("2016-04-21T03:28:08.493Z")
        }
    }
},
{
    "name": "Data 3",
    "ancestor" : {
        "parent" : {
            "name" : "Apple",
            "date" : ISODate("2016-11-21T03:28:08.493Z")
        }
    }
},
{
    "name": "Data 4",
    "ancestor" : {
        "parent" : {
            "name" : "Apple"
        }
    }
},
{
    "name": "Data 5",
    "ancestor" : {
        "parent" : {
            "name" : "Microsoft"
        }
    }
}

我索引了Elasticsearch服务器并实现了排序功能。

我仅通过ancestor.parent.date实现了name: Apple的排序功能。如果不是Apple,则将默认分数设置为0。此外,它的值为date

{
  "_script": {
    "type": "number",
    "order": "asc,
    "script": {
      "lang": "painless",
      "source": "def name = ''; if (doc.containsKey('ancestor') && doc['ancestor'].containsKey('parent') && doc['ancestor.parent'].containsKey('name')) { name = doc['ancestor.parent.name'].value; } if (name != 'Apple') { return 0; } else { return doc['ancestor.parent.date'].value; }"
    }
  }
}

但不适用于此脚本。有人可以给我建议吗?非常感谢:)

预期结果:

{
    "name": "Data 2",
    ...
},
{
    "name": "Data 5",
    ...
},
{
    "name": "Data 4",
    ...
},
{
    "name": "Data 1",
    ...
},
{
    "name": "Data 3",
    ...
}

0 个答案:

没有答案