当前位置: 首页 > 面试题库 >

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

柳韬
2023-03-14
问题内容

我正在尝试通过将值手动传递给构造函数来创建虚拟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[]。请让我知道我该怎么做。


问题答案:

这将做您想要的:

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()searchResponse返回:

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


 类似资料:
  • 创建一台 Linux 系统的虚拟机,Linux 系统的类型选择的是 CentOS 7.x 。 创建项目 每个项目都在各自的文件夹里,你可以为每个项目单独去创建虚拟机。打开命令行工具,先为项目创建一个文件夹: cd ~/desktop mkdir awesome-project cd awesome-project 上面执行的命令就是在命令行下面,先进入到当前登录用户的桌面(desktop)上,在

  • 用于管理通过编排部署的虚拟机应用实例。 虚拟机实例用于管理基于应用市场部署的虚拟机应用。 入口:在云管平台单击左上角导航菜单,在弹出的左侧菜单栏中单击 “运维工具/编排/虚拟机实例” 菜单项,进入虚拟机实例页面。 新建虚拟机实例 该功能用于部署虚拟机实例。 在虚拟机实例页面,单击列表上方 “新建” 按钮,跳转到应用市场-虚拟机类型页面。 在应用市场页面部署应用,具体步骤请参考应用市场-部署虚拟机实

  • 本文向大家介绍创建Lua虚拟机,包括了创建Lua虚拟机的使用技巧和注意事项,需要的朋友参考一下 示例 5.1 5.1            

  • 我已经为web创建了mockito测试用例。我应该如何为服务层创建mockito测试用例。 我不能使用任何autowire,因为我没有应用程序上下文。它给了我错误: 其次,我有这个配置bean: 你能告诉我应该如何构造mockito测试类吗?如果我把所有的测试类放在web层,我就可以得到上下文。

  • JBDS + JDV 示例 软件安装 参照相关安装文档。 准备数据源 mariadb安装数据库 yum groupinstall mariadb mariadb-client -y systemctl start mariadb准备示例数据,执行 financials-mariadb.sql source financials-mariadb.sql JDV 拷贝 mysql-connector-

  • 打开命令行,执行: cd ~/desktop mkdir wenjiangs-lemp cd wenjiangs-lemp mkdir app vagrant init centos/7 用编辑器打开 wenjiangs-lemp 目录,编辑 Vagrant 配置文件:Vagrantfile macOS: Vagrant.configure("2") do |config| config.v