How can I get fields from SearchResponse?


(Byeong Seok Park) #1

I just wonder how I get fields from SearchResponse which is result of my query.

Below is my query:

{"size":99,"timeout":"10s","query":{"bool":{"filter":[{"bool":{"must":[{"range":{"LOG_GEN_TIME":{"from":"2018-11-01 12:00:01+09:00","to":"2018-11-01 23:59:59+09:00","include_lower":true,"include_upper":true,"boost":1.0}}},{"wrapper":{"query":"eyAiYmvbCIgOiB7ICN0IiA6IFsgeyAidIgOiB7ICJBFVF9JUCIgOiAiMTAuMTExLjI1Mi4xNiIgfSB9LCB7ICJ0ZsgIkFDVElPTl9UWVBFX0NEIiA6ICgInRlcm0iIDogeyAiRFUlQiIDogIjggXSB9IH="}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"_source":{"includes":["LOG_GEN_TIME","LOG_NO","ASSET_NO"],"excludes":[]},"sort":[{"LOG_GEN_TIME":{"order":"desc"}},{"LOG_NO":{"order":"desc"}}]}

and when I query this, like below:

SearchRequestBuilder request = client.prepareSearch("indexName");
SearchResponse searchResponse = request.get();

I got right result:

{  
   "took":1071,
   "timed_out":false,
   "_shards":{  
      "total":14,
      "successful":14,
      "skipped":0,
      "failed":0
   },
   "_clusters":{  
      "total":0,
      "successful":0,
      "skipped":0
   },
   "hits":{  
      "total":2,
      "max_score":null,
      "hits":[  
         {  
            "_index":"log_20181101",
            "_type":"SEC",
            "_id":"1197132746951492963",
            "_score":null,
            "_source":{  
               "ASSET_NO":1,
               "LOG_NO":1197132746951492963,
               "LOG_GEN_TIME":"2018-11-01 09:46:28+09:00"
            },
            "sort":[  
               1541033188000,
               1197132746951492963
            ]
         },
         {  
            "_index":"log_20181101",
            "_type":"SEC",
            "_id":"1197132746951492963",
            "_score":null,
            "_source":{  
               "ASSET_NO":2,
               "LOG_NO":1197337264704454700,
               "LOG_GEN_TIME":"2018-11-01 23:00:06+09:00"
            },
            "sort":[  
               1541080806000,
               1197337264704454700
            ]
         }
      ]
   }
}

To use this result, I need to map this by field and value.

I think there's a way to map the field and value to the 'fields' parameter so that we could use it nicely, but I cannot find.

I hope I can use the result like this way:

SearchHit hit = ...
Map<String, SearchHitField> fields = hit.getFields();
String logNo = fields.get("LOG_NO").value();

And It seems like this is the common way to use..

Or am I misunderstanding something? Tell me other way if there's better way, please.

Any comment would be appreciated. Thanks.