3

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":"eyAiYm9vbCIgOiB7ICJtdXN0IiA6IFsgeyAidGVybSIgOiB7ICJBU1NFVF9JUCIgOiAiMTAuMTExLjI1Mi4xNiIgfSB9LCB7ICJ0ZXJtIiA6IHsgIkFDVElPTl9UWVBFX0NEIiA6ICIyIiB9IH0sIHsgInRlcm0iIDogeyAiRFNUX1BPUlQiIDogIjgwIiB9IH0gXSB9IH0="}}],"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:

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.

enter image description here

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.

1 Answer 1

2

I'm not clear what client you are using to query elastic. If you are using elasticsearch high level rest client then you can loop through hits and to get source you can use hit.getSourceAsMap() to get the key value of fields.

For your comment:

  • Firstly create a POJO class which corresponds to _source (i.e. index properties; the way data is store in elastic)
  • The use hit.getSourceAsString() to get _source in json format.
  • Use jackson ObjectMapper to map json to your pojo

Assuming you created a POJO class AssetLog

SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit searchHit : searchHits) {
      String hitJson = searchHit.getSourceAsString();
      ObjectMapper objectMapper = new ObjectMapper();
      AssetLog source = objectMapper.readValue(hitJson, AssetLog.class);
      //Store source to map/array
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I'm using it and 'getSourceAsMap()' method is helpful, thanks. However that method returns Map<String, Object> and I still wonder how I can put this key and value to 'fields' of 'SearchHit'.. If I can use 'fields', then the rest of the process is easier a lot.
That's awesome, I'll try it that way. Thanks for letting me know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.