我有一个包含以下结构的文档索引:

{
  name: "The sound and the fury",
  tags: [{ name: "confusing", id: "uuid1"}, {name: "sad", id: "uuid-2"}]
}

我想获取所有具有两个以上标签的文档。

我已经尝试过以下方法:
{
 "query": {
 "bool": {
   "must": [
     {
      "script": {
        "script": "doc['tags'].value.size() == 1"
      }
     }
   ]
  }
 }
}

但是我收到一个脚本错误:No field found for [tags] in mapping with types。我收到"doc['tags'].size() == 1"脚本的相同错误

我正在运行elasticsearch 5.6;性能不是问题,因为我将其用于调试和测试目的。

最佳答案

您需要修改索引映射以启用字段数据。这是一个示例映射。

   put your_index_name/_mapping/doc
    {
       "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "tags": {
            "properties": {
              "id": {
                "type": "text",
                "fielddata":true,
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "name": {
                "type": "text","fielddata":true,
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }
        }
}

并使用以下查询
get your_index_name/_search
{
"query": {
 "bool": {
   "must": [
     {
      "script": {
        "script": "doc['tags.value'].length > 2"
      }
     }
   ]
  }
 }
}

关于elasticsearch - 按嵌套对象数过滤文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55013842/

10-09 20:18
查看更多