Elasticsearch搜索数组的长度

时间:2018-06-12 13:10:13

标签: elasticsearch

这是我的对象profile

{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "posts": {
          "properties": {
            "id": {
              "type": "text"
            },
            "create_date": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

我想进行搜索:返回所有配置文件 1.有名字" bob"
2.并且有超过5个帖子

这是我找到的一个例子,但它不起作用

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name": "bob"
          }
        }
      ],
      "filter": [
        {
          "script": {
            "script": "doc['posts'].values.size() > 5"
          }
        }
      ]
    }
  }
}

我收到错误"reason":"Variable [posts] is not defined."

将posts.id更新为关键字 {"id": {"type":"text"},"fields":{{"keyword":{"type":"keyword","ignore_above":256}}}}

相同错误

"caused_by":{"type":"script_exception","reason":"compile error","script_stack":["doc[posts.id].values.size() > ..."," ^---- HERE"],"script":"doc[posts.id].values.size() > 5","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"Variable [posts] is not defined."}}}}]},"status":400

1 个答案:

答案 0 :(得分:0)

根据https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html,这是因为一个文档缺少字段posts(我想)。

你可以添加一个过滤器:"如果现场帖子存在"或在脚本中使用条件"如果doc.containsKey('发布')...."