我想从嵌套的JSON数据中获取所有实体,其中“ ai_id”的值= 0

时间:2019-07-10 15:44:00

标签: elasticsearch

我有下面的JSON数据,我想在Elasticsearch中编写一个Query,查询是, (给我所有“ ai_id”的值= 0的实体)。

JSON数据专家:

      {
        "_index": "try1",
        "_type": "_doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "target": {
            "br_id": 0,
            "an_id": 0,
            "ai_id": 0,
            "explanation": [
              "element 1",
              "element 2"
            ]
          },
          "process": {
            "an_id": 1311,
            "pa_name": "micha"
          },
          "text": "hello world"
        }
      },
      {
        "_index": "try1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "target": {
            "br_id": 0,
            "an_id": 1,
            "ai_id": 1,
            "explanation": [
              "element 3",
              "element 4"
            ]
          },
          "process": {
            "an_id": 1311,
            "pa_name": "luca"
          },
          "text": "the all People are good"
        }
      }
    ]
  }
}

我尝试了此方法,但似乎没有用,请任何帮助,我将不胜感激。

GET try1\_search   
{
  "query":{
        { "match_all": { "ai_id":   0}}
  }
}

这也行不通,

GET try1/_search
{
    "query": {
        "nested" : {
            "query" : {
                    "must" : [
                    { "match" : {"ai_id" : 0} }
                    ]
            }
        }
    }
}

请提出建议。

1 个答案:

答案 0 :(得分:0)

您需要像这样在您的target对象上嵌套查询-

GET /try1/_search
{
    "query": {
        "nested" : {
            "path" : "target",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"target.ai_id" : 0} }
                    ]
                }
            }
        }
    }
}

参考 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html