为 ElasticSearch 测试用例创建虚拟的 SearchResponse 实例

10

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

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

以下是我直接查询Elasticsearch时得到的JSON实际值。

{
  "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[]中的值。请告诉我如何做到这一点。


请建议如何使用预定义数据创建实例。 - bagui
2个回答

8
这段代码可以实现您想要的功能:
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);

如果你在searchResponse上调用toString(),它将返回:
{
    "_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"}
        } ]
    }
}

1
如何模拟一个 InternalAggregation? - Jonathan
2
这看起来是一个很老的答案,我正在使用6.3.2 API,请问你能否提供一种方法? - Raghuveer
聚合已解决!请在此链接中查看答案:https://dev59.com/Yarka4cB1Zd3GeqPfpGB#57117845 - technocrat

3

在 ElasticsearchS 6.5 中,它对我有效。

BytesReference source = new BytesArray(
            "{your json response come here}" );
    SearchHit hit = new SearchHit( 1 );
    hit.sourceRef( source );
    SearchHits hits = new SearchHits( new SearchHit[] { hit }, 5, 10 );
    SearchResponseSections searchResponseSections = new SearchResponseSections( hits, null, null, false, null, null, 5 );
    SearchResponse searchResponse = new SearchResponse( searchResponseSections, null, 8, 8, 0, 8, new ShardSearchFailure[] {} );
    return searchResponse;

聚合函数怎么样? - Krishna Gangaraju
1
聚合查询问题已解决!请查看答案:https://dev59.com/Yarka4cB1Zd3GeqPfpGB#57117845 - technocrat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接