根据键值约束检索对象列表

时间:2019-06-05 14:36:36

标签: elasticsearch

我在ES 6.2中有对象实例索引,我可以像这样查询:

POST /_search 

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "instanceId" : "I001"
                    }
                }
            ]
        }
    }
}

并接收特定的实例查询结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 15,
    "successful": 15,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 5.7745514,
    "hits": [
      {
        "_index": "instance",
        "_type": "searchinstance",
        "_id": "I001",
        "_score": 5.7745514,
        "_source": {
          "name": "someInstance",
          "uuid": "18fab6a6-0fc9-428e-ad60-a13a6a43e0ea",
          "id": "I001",
          "createdAt": 1559140971501,
          "completedAt": 1559140988024,
          "modifiedAt": 1559140988028,
          "description": "my description",
          "instanceId": "I001",
          "status": null,
          "attributes": [
            {
              "name": "response.result",
              "value": "0"
            },
            {
              "name": "response.value",
              "value": "123"
            }
          ],
          "createdBy": null
        }
      }
    ]
  }
}

如何查询具有"attributes.name": "response.result""attributes.value": "0"的所有此类实例(即instanceId值列表)?

我一直在尝试组合query_string,match,通配符和嵌套查询类型,但仍然没有成功。看来问题在于正确指定属性结构的路径。发布时:

{
    "query": {
        "nested": {
            "path": "attributes", 
            "query": {
              "bool": {
                "must": [ 
                  {
                    "match": {
                      "attributes.name": "response.result"
                    }
                  },
                  {
                    "match": {
                      "attributes.value": "0"
                    }
                  }
                ]
              }
            }
        }
    }
}

我收到失败原因

{
  "type": "query_shard_exception",
  "reason": "failed to create query: {...}",
  "index_uuid": "8Sr_2jvsRvqGmDjK71SFsw",
  "index": ".kibana",
  "caused_by": {
    "type": "illegal_state_exception",
    "reason": "[nested] failed to find nested object under path [attributes]"
  }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

Elasticsearch没有专用的数组类型。实际上,任何类型的任何字段都被视为值的数组。因此,假设您的attributes字段属于对象类型,则可以像对单个对象一样进行查询,例如:

{
    "query": {
        "bool": {
            "must": [ 
                {
                    "match": {
                        "attributes.name": "response.result"
                    }
                },
                {
                    "match": {
                        "attributes.value": "0"
                    }
                }
            ]
        }
    }
}