1

I'm very new to ES but I'm trying to create a script that can query for an array to contain some matches but also filter based on the total array size.

Example data:

"_source": {
  "id": 5,
  "title": "Chicken & vegetable broth, soda farls & chicken liver toasts ",
  "ingredients": [
    "1 organic or free-range chicken",
    "5 large onions",
    "100g butter, for frying"
  ]
}

The following query would match the record above but would not return it because it has less than 5 ingredients.

{
"query": {
    "function_score": {
        "query": {
            "terms": {
                "ingredients": ["beef", "bacon"]
            }
        },
        "functions": [{
            "script_score": {
                "script": "ingredients-amount",
                "params": {
                    "my_modifier": 5
                }
            }
        }]
    }
  }
}

I thought ingredients-amount.mvel would simple be along the lines of:

doc['ingredients'].values.length > my_modifier

Thanks for the help

4

2 回答 2

1

索引数组的长度,所以你可以做一个简单的range过滤器。

如果为此使用脚本,您会将整个字段加载到内存中,而您可能不想这样做。

于 2014-07-16T16:56:29.730 回答
0

如果您想以脚本方式执行此操作,则表达式将是:

doc['ingredients'].values.size() > my_modifier

但是使用 doc 在性能方面确实不是最好的,因此您最好按照 Alex 的建议索引成分列表的长度。

于 2014-07-16T20:26:02.753 回答