按对象类型字段弹性搜索的大小搜索

rub*_*nce 13 elasticsearch

我有这样的映射:

{
  "post": {
    "properties": {
      "author_gender": {
        "type": "string",
        "index": "not_analyzed",
        "omit_norms": true,
        "index_options": "docs"
      },
      "author_link": {
        "type": "string",
        "index": "no"
      },
      "content": {
        "type": "string"
      },
      "mentions": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "profile_image_url": {
            "type": "string"
          },
          "screen_name": {
            "type": "string"
          }
        }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我需要按mentions对象的大小进行搜索.我试过这个:

{
  "filter": {
    "script": {
      "script": "doc['mentions'].values.length == 2"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.给出错误

嵌套:ElasticSearchIllegalArgumentException [在使用类型[post]的映射中找不到[提及]的字段];

我也试过替换脚本部分doc['mentions.id'].value.length == 2.这也是错误的

嵌套:ArrayIndexOutOfBoundsException [10];

如何查询mentions对象大小为2的记录?

Sai*_*Sai 13

elasticsearch指南建议使用size()而不是对象的长度.试试这个:

{
 "filter": {
   "script": {
     "script": "doc['mentions.id'].values.size() == 2"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

祝一切顺利.

  • 它仍然给出错误`nested: ElasticSearchIllegalArgumentException[No field found for [mentions] in mapping with types [post]];`...它说`mentions`不是帖子的字段......在搜索位置`提及` 字段存在,我不得不像这样使用它`"exists": {"field": "mentions.id"}`。注意`"mentions.id"`,而不是`"mentions"` (2认同)
  • 抱歉@ Sai ..你是对的...当我做'doc ['mentions.id'].values.size()== 2`时,我得到了正确的结果.非常感谢你的帮助!!.也可以你发布链接,你发现`elasticsearch指南推荐使用size()而不是对象的长度.我检查了指南,但无法在这里找到http://www.elasticsearch.org/guide/reference/mapping/object-type/.也许这个链接对我将来的编码也很有帮助.再次感谢! (2认同)