(JAVA, Elasticsearch) How can I get fields from SearchResponse?











up vote
0
down vote

favorite












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.










share|improve this question




























    up vote
    0
    down vote

    favorite












    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.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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.










      share|improve this question















      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.







      java elasticsearch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 11:14

























      asked Nov 20 at 11:06









      qumm

      455




      455
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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.






          share|improve this answer























          • 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.
            – qumm
            Nov 21 at 2:55










          • @qumm update my answer. Hope this helps.
            – Nishant Saini
            Nov 21 at 4:44










          • That's awesome, I'll try it that way. Thanks for letting me know that.
            – qumm
            Nov 22 at 3:51










          • @qumm have you tried? Did it solve the issue?
            – Nishant Saini
            Nov 23 at 6:51











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391654%2fjava-elasticsearch-how-can-i-get-fields-from-searchresponse%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          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.






          share|improve this answer























          • 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.
            – qumm
            Nov 21 at 2:55










          • @qumm update my answer. Hope this helps.
            – Nishant Saini
            Nov 21 at 4:44










          • That's awesome, I'll try it that way. Thanks for letting me know that.
            – qumm
            Nov 22 at 3:51










          • @qumm have you tried? Did it solve the issue?
            – Nishant Saini
            Nov 23 at 6:51















          up vote
          0
          down vote













          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.






          share|improve this answer























          • 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.
            – qumm
            Nov 21 at 2:55










          • @qumm update my answer. Hope this helps.
            – Nishant Saini
            Nov 21 at 4:44










          • That's awesome, I'll try it that way. Thanks for letting me know that.
            – qumm
            Nov 22 at 3:51










          • @qumm have you tried? Did it solve the issue?
            – Nishant Saini
            Nov 23 at 6:51













          up vote
          0
          down vote










          up vote
          0
          down vote









          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 4:27

























          answered Nov 20 at 11:16









          Nishant Saini

          465315




          465315












          • 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.
            – qumm
            Nov 21 at 2:55










          • @qumm update my answer. Hope this helps.
            – Nishant Saini
            Nov 21 at 4:44










          • That's awesome, I'll try it that way. Thanks for letting me know that.
            – qumm
            Nov 22 at 3:51










          • @qumm have you tried? Did it solve the issue?
            – Nishant Saini
            Nov 23 at 6:51


















          • 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.
            – qumm
            Nov 21 at 2:55










          • @qumm update my answer. Hope this helps.
            – Nishant Saini
            Nov 21 at 4:44










          • That's awesome, I'll try it that way. Thanks for letting me know that.
            – qumm
            Nov 22 at 3:51










          • @qumm have you tried? Did it solve the issue?
            – Nishant Saini
            Nov 23 at 6:51
















          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.
          – qumm
          Nov 21 at 2:55




          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.
          – qumm
          Nov 21 at 2:55












          @qumm update my answer. Hope this helps.
          – Nishant Saini
          Nov 21 at 4:44




          @qumm update my answer. Hope this helps.
          – Nishant Saini
          Nov 21 at 4:44












          That's awesome, I'll try it that way. Thanks for letting me know that.
          – qumm
          Nov 22 at 3:51




          That's awesome, I'll try it that way. Thanks for letting me know that.
          – qumm
          Nov 22 at 3:51












          @qumm have you tried? Did it solve the issue?
          – Nishant Saini
          Nov 23 at 6:51




          @qumm have you tried? Did it solve the issue?
          – Nishant Saini
          Nov 23 at 6:51


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391654%2fjava-elasticsearch-how-can-i-get-fields-from-searchresponse%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'