使用JestClient从elasticsearch

时间:2017-03-17 13:17:35

标签: java elasticsearch

使用JestClient从elasticsearch获取Double值而不是Integer。

Index mapping -



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

我正在使用JestClient(在java中)获取字段值,我希望makeId和yearId值为整数,但值返回为Double,

Jest maven Depandency -

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

Java代码 -

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);

在此行之后,SearchResult将Source对象作为整数值(正如我所料)

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

回应: -

  

{&#34;把&#34;:29,&#34; TIMED_OUT&#34;:假,&#34; _shards&#34; {&#34;总&#34;:5,&#34 ;成功&#34;:5,&#34;失败&#34;:0},&#34;命中&#34; {&#34;总&#34;:1466,&#34; MAX_SCORE&#34 ;: 1.0&#34;命中&#34;:[{&#34; _index&#34;:&#34;车辆&#34;&#34; _type&#34;:&#34; vehicle_dev&#34;,& #34; _id&#34;:&#34; 836&#34;&#34; _score&#34;:1.0,&#34; _source&#34; {&#34; makeId&#34;:2,& #34; makeName&#34;:&#34;别克&#34;&#34; yearId&#34;:2012}},{&#34; _index&#34;:&#34;车辆&#34;,& #34; _type&#34;:&#34; vehicle_dev&#34;&#34; _id&#34;:&#34; 1915&#34;&#34; _score&#34;:1.0,&#34; _source&#34; {&#34; makeId&#34;:5,&#34; makeName&#34;:&#34;雪佛兰&#34;&#34; yearId&#34;:2012}},{& #34; _index&#34;:&#34;车辆&#34;&#34; _type&#34;:&#34; vehicle_dev&#34;&#34; _id&#34;:&#34; 2511&# 34;,&#34; _score&#34;:1.0,&#34; _source&#34; {&#34; makeId&#34;:5,&#34; makeName&#34;:&#34;雪佛兰&# 34;,&#34; yearId&#34;:2012}},{&#34; _index&#34;:&#34;车辆&#34;&#34; _type&#34;:&#34; vehicle_dev&# 34;,&#34; _id&#34;:&#34; 2324&#34;&#34; _score&#34;:1.0,& #34; _source&#34; {&#34; makeId&#34;:5,&#34; makeName&#34;:&#34;雪佛兰&#34;&#34; yearId&#34;:2012}}

(预期)

但是当下一行执行时 -

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

响应: -

  

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

意外的

值从2变为Double i..e 2.0。

1 个答案:

答案 0 :(得分:1)

@shubam

使用索引字段创建自己的模型(POJO)类并提供适当的类型

实施例

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

//Generate the setter and getters 

然后使用此类

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

这将为您提供定义类型POJO

的结果