create dummy SearchResponse instance for ElasticSearch test case

时光毁灭记忆、已成空白 提交于 2020-01-28 10:33:10

问题


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

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

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.


回答1:


This will do what you want:

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

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"}
        } ]
    }
}



回答2:


It works for me in 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;


来源:https://stackoverflow.com/questions/25425243/create-dummy-searchresponse-instance-for-elasticsearch-test-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!