为ElasticSearch测试用例创建虚拟SearchResponse实例 [英] create dummy SearchResponse instance for ElasticSearch test case

查看:329
本文介绍了为ElasticSearch测试用例创建虚拟SearchResponse实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将值传递给构造函数来创建一个虚拟的SearchResponse对象。我有一个JUnit测试类,我正在使用这个虚拟值来模拟实际的方法调用。尝试使用以下方法

I'm trying to create a dummy SearchResponse object by passing the values manually to the constructor. I have a JUnit test class for which I'm using this dummy value to mock the actual method call. Trying with the below method to

public SearchResponse actionGet() throws ElasticsearchException {
    ShardSearchFailure[] shardFailures = new ShardSearchFailure[0];
    int docId = 0;
    String id = "5YmRf-6OTvelt29V5dphmw";
    Map<String, SearchHitField> fields = null;

    InternalSearchHit internalSearchHit = new InternalSearchHit(docId, id,
            null, fields);
    InternalSearchHit[] internalSearchHit1 = { internalSearchHit };

    InternalSearchResponse EMPTY = new InternalSearchResponse(
            new InternalSearchHits(internalSearchHit1, 0, 0), null, null,
            null, false);
    SearchResponse searchResponse = new SearchResponse(EMPTY, "scrollId",
            1, 1, 1000, shardFailures);
    return searchResponse;
}

这里是我直接查询到弹性搜索时json的实际值。

and here is my actual value of json when query directly to elasticsearch.

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "failed": 0
  },
  "hits": {
    "total": 28,
    "max_score": null,
    "hits": [
      {
        "_index": "monitoring",
        "_type": "quota-management",
        "_id": "5YmRf-6OTvelt29V5dphmw",
        "_score": null,
        "_source": {
          "@timestamp": "2014-08-20T15:43:20.762Z",
          "category_name": "cat1111",
          "alert_message": "the new cpu threshold has been reached 80%",
          "alert_type": "Critical",
          "view_mode": "unread"
        },
        "sort": [
          1408549226173
        ]
      }
    ]
  }
}

我想创建类似的回应通过创建实际的SearchResponse对象。但是我找不到任何方式发送 InternalSearchHit [] 中的值。请让我知道如何做到这一点。

I want to create similar kind of response by creating the actual SearchResponse Object. But I couldn't find any way to send the values in InternalSearchHit[]. Please let me know how can I do this.

推荐答案

这将做你想要的:

SearchShardTarget shardTarget = new SearchShardTarget("1", "monitoring", 1);
ShardSearchFailure[] shardFailures = new ShardSearchFailure[0];
float score = 0.2345f;

BytesReference source = new BytesArray("{\"@timestamp\":\"2014-08-20T15:43:20.762Z\",\"category_name\""
        + ":\"cat1111\",\"alert_message\":\"the new cpu threshold has been reached 80%\",\"alert_type\":"
        + "\"Critical\",\"view_mode\":\"unread\"}");

InternalSearchHit hit = new InternalSearchHit(1, "5YmRf-6OTvelt29V5dphmw", new StringText("quota-management"),
        null);
hit.shardTarget(shardTarget);
hit.sourceRef(source);
hit.score(score);

InternalSearchHit[] hits = new InternalSearchHit[]{hit}; 
InternalSearchHits internalSearchHits = new InternalSearchHits(hits, 28, score);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(internalSearchHits, null, null,
      null, false);

SearchResponse searchResponse = new SearchResponse(internalSearchResponse, "scrollId", 1, 1, 1000, 
        shardFailures);

如果您调用 toString() code> searchResponse 它返回:

If you call toString() on searchResponse it returns:

{
    "_scroll_id" : "scrollId",
    "took" : 1000,
    "timed_out" : false,
    "_shards" : {
        "total" : 1,
        "successful" : 1,
        "failed" : 0
    },
    "hits" : {
        "total" : 28,
        "max_score" : 0.2345,
        "hits" : [ {
            "_index" : "monitoring",
            "_type" : "quota-management",
            "_id" : "5YmRf-6OTvelt29V5dphmw",
            "_score" : 0.2345,
            "_source":{"@timestamp":"2014-08-20T15:43:20.762Z","category_name":"cat1111","alert_message":"the new cpu threshold has been reached 80%","alert_type":"Critical","view_mode":"unread"}
        } ]
    }
}

这篇关于为ElasticSearch测试用例创建虚拟SearchResponse实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆