简体   繁体   中英

Getting the Double values instead of Integer using JestClient to retrieve document from elasticsearch

Getting the Double values instead of Integer using JestClient to retrieve document from elasticsearch.

Index mapping -



               "makeId": {
                  "type": "integer"
               },
               "makeName": {
                  "type": "keyword"
               },
               "yearId": {
                  "type": "integer"
               }

I am using JestClient (In java) to get the fields value, I'm expecting makeId and yearId values as integer, but values are returned as Double,

Jest maven Depandency -

<dependency>
   <groupId>io.searchbox</groupId>
   <artifactId>jest</artifactId>
   <version>2.0.0</version>
</dependency>

Java code -

String[] reqFields = { "makeName","makeId","yearId" };
 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
                    BoolQueryBuilder boolquery = QueryBuilders.boolQuery();
                    boolquery.must(QueryBuilders.matchQuery("yearId", yearId));
                    searchSourceBuilder.query(boolquery).fetchSource(reqFields, null).size(9000);

                    Search.Builder searchBuilder = new Search.Builder(searchSourceBuilder.toString()).addIndex(indexName).addType(indexType);
                    SearchResult result = jestClient.execute(searchBuilder.build());
                    List<Hit<Map, Void>> hits = result.getHits(Map.class);

after this line SearchResult gets Source object as integer values (as I am expecting)

SearchResult result = jestClient.execute(searchBuilder.build());

Response:-

{"took":29,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1466,"max_score":1.0,"hits":[{"_index":"vehicle","_type":"vehicle_dev","_id":"836","_score":1.0,"_source":{"makeId":2,"makeName":"BUICK","yearId":2012}},{"_index":"vehicle","_type":"vehicle_dev","_id":"1915","_score":1.0,"_source":{"makeId":5,"makeName":"CHEVROLET","yearId":2012}},{"_index":"vehicle","_type":"vehicle_dev","_id":"2511","_score":1.0,"_source":{"makeId":5,"makeName":"CHEVROLET","yearId":2012}},{"_index":"vehicle","_type":"vehicle_dev","_id":"2324","_score":1.0,"_source":{"makeId":5,"makeName":"CHEVROLET","yearId":2012}}

(Expected)

but when next line executed -

List<Hit<Map, Void>> hits = result.getHits(Map.class);

Response:-

{makeId=2.0, makeName=BUICK, yearId=2012.0, es_metadata_id=836}

Unexpected

Values got changed into Double i..e 2.0 from 2.

@shubam

Create your Own Model(POJO) class with the fields of index and give appropriate types

Example

public IndexModel
{
private integer makeId;
private String makeName;
private integer yearId;

//Generate the setter and getters 

and then use this class

List<IndexModel> IndexModelList = result.getSourceAsObjectList(IndexModel.class);
                for( IndexModel indexModel: IndexModelList ){
                    indexModel.getyearId();

This will give you the results in your define types of POJO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM