How to GET a Field from nested JSON Data using Query

Hello folks ,
i have this bellow JSON Data, and i want to write a Query , the Query is ,
(Give me all Entities where the "ai_id" has the Value = 0 ).

the JSON Data ,

      {
        "_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"
        }
      }
    ]
  }
}

I tried something like this , and did not work ,

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

Please any Idea or Suggestion , i will be thankfull.

Try:

GET try1\_search   
{
  "query":{
        { "match": { "target.ai_id":   0}}
  }
} 

thx , it worked fine .

Can we write your Query using the RestClient API in low level and Highlevel client, and how ?
thx in advance.

I wrote smothing like this , using the RestClient in Low Level Client, and it worked fine as "Match_all" , but the Above Query is not about Match_all,

        try {
            RestClient restClient = RestClient.builder(
                    new HttpHost("localhost", 9200, "http")).build();

            Response response1 = restClient.performRequest("GET", "/try1/_doc/_search?size=" + 10, Collections.singletonMap("pretty", "true"));
            String responseBody = EntityUtils.toString(response1.getEntity());
            System.out.println("result is : " + responseBody);

        } catch (UnknownHostException ex) {
            System.out.println("failure : " + ex.getMessage());
        }

Can you please expand my Code , so i can write(translate) the ("match": { "target.ai_id": 0}) , in it .
I will be thankfull.

Here we go with the RestHighLevelClient.

SearchResponse response = client.search(new SearchRequest("test").source(
        new SearchSourceBuilder().query(
                QueryBuilders.matchQuery("target.ai_id", 0)
        )
), RequestOptions.DEFAULT);

thx it worked fine , and i get this good result ,

"hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
 {
        "_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"
        }
      }
]

Is there any Method so i can just take the Content of the "hits_array"?
i need to get this Result like bellow ,

 {
 "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"
        }

You can't get this format exactly I think.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.