【问题标题】:PhpUnit testing elasticsearch results are emptyphpUnit测试elasticsearch结果为空
【发布时间】:2019-02-02 09:41:22
【问题描述】:

我正在编写我的应用程序的测试方法,并在我的应用程序中使用弹性搜索。当我运行一个应该使用 elasticsearch 返回值的测试方法时,响应总是空的。我该如何解决这个问题?这是我发送的代码。

public function testGetPosts()

{
    $brand = factory(Brand::class)->create();
    $account = factory(Account::class)->create();
    $post = factory(Post::class)->create();

    $response = $this->actingAs($this->owner)->json(
        'GET',
        ('/api/publish/posts'),
        ['account_id' => [(string) $account->id],
        'skip' => 0]
    );

    $response->assertStatus(200);
}

【问题讨论】:

  • 我知道问题出在哪里,但我不知道如何解决。弹性搜索不会索引创建的帖子。它应该被索引。

标签: php laravel elasticsearch testing phpunit


【解决方案1】:

我知道这篇文章很旧,但我在那里添加了我为这个问题找到的答案。 在查询之前确保您的数据被编入索引所需要做的就是在您刚刚编写的索引上调用refresh。 它强制 ES 索引数据,所以当你查询它时你确定数据就在那里! 它比作者建议的sleep(1); 更快=)

你可以找到关于它的官方 ElasticSearch 文档here

希望这会对某人有所帮助。

【讨论】:

    【解决方案2】:

    差不多一年后,我敢肯定你现在已经继续前进了。

    你说:

    弹性搜索不会索引创建的帖子。它应该被索引 为什么会被索引?当然,除非您有代码可以在您的 setUp() 中建立索引,或者您针对外部 ES 服务器进行测试并假设它始终可用并且包含您正在测试的确切数据。

    另一个解决方案是模拟请求,因为 Elasticsearch 返回 JSON。我们需要做的就是模拟一个状态为 200 的 HTTP 请求,并返回 JSON。我们可以将这个 JSON 文件放在我们的 tests/ 目录中,它将包含 Elasticsearch 将返回的示例结果。

    一个示例测试是这样的;

    $handler = new MockHandler([
        'status' => 200,
        'transfer_stats' => [
            'total_time' => 100
        ],
        'body' => fopen(base_path('tests/Unit/mockelasticsearch.json'), 'r')
    ]);
    
    $builder = ClientBuilder::create();
    $builder->setHosts(['testing']);
    $builder->setHandler($handler);
    $client = $builder->build();
    $response = $client->search([
        'index' => 'my_index',
        'type' => 'my_type',
        'body' => [
            [  
                'query' => [
                    'simple_query_string' => [
                        'query' => 'john',
                        'fields' => ['name']
                    ]
                ]
            ]
        ]
    ]);
    
    // Test against the "$response", i.e., $this->assertEquals(2 ...) etc.
    

    然后在 JSON 文件中,您需要根据您的索引进行自定义;

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 121668,
        "max_score": 1,
        "hits": [
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "1111",
            "_score": 1,
            "_source": {
              "id": "1111",
              "title": "Some Foo",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "2222",
            "_score": 1,
            "_source": {
              "id": "2222",
              "title": "Dolor Sit Amet",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "3333",
            "_score": 1,
            "_source": {
              "id": "3333",
              "title": "Consectetur Adipiscing Elit",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "4444",
            "_score": 1,
            "_source": {
              "id": "4444",
              "title": "Sed Do Eiusmod",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "5555",
            "_score": 1,
            "_source": {
              "id": "5555",
              "title": "Tempor Incididunt",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "6666",
            "_score": 1,
            "_source": {
              "id": "6666",
              "title": "Ut Labore Et Dolore",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "7777",
            "_score": 1,
            "_source": {
              "id": "7777",
              "title": "Magna Aliqua",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "8888",
            "_score": 1,
            "_source": {
              "id": "8888",
              "title": "Ut Enim Ad Minim",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "9999",
            "_score": 1,
            "_source": {
              "id": "9999",
              "title": "Veniam, Quis Nostrud",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          },
          {
            "_index": "test",
            "_type": "test-type",
            "_id": "0000",
            "_score": 1,
            "_source": {
              "id": "0000",
              "title": "Exercitation Ullamco Laboris",
              "timestamp": "2017-08-02T15:45:22-05:00"
            }
          }
        ]
      }
    }
    

    【讨论】:

    • 您好,感谢您一年后的努力。我很感激。我记得我在这里得到的解决方案。由于 elasticsearch 没有立即获得我在测试时创建的数据,因此将 sleep() 方法放置 1 秒就可以解决问题。这段时间给了弹性来获取测试期间创建的所有数据。
    猜你喜欢
    • 2013-10-06
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2011-02-03
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多