访问 SearchDescriptor 属性以进行测试

Accessing SearchDescriptor properties for testing

我将查询构建和查询执行分开,所以我有一个 QueryBuilder returns SearchDescriptor<Video>

因此,将其置于我的 Web Api 控制器的上下文中:

SearchDescriptor<ElasticVideo> documentQuery = _queryBuilder.BuildDocumentQuery(elasticSearchParameters);
ISearchResponse<ElasticVideo> documentResults = _videoRepository.Search(documentQuery);

但是在测试 QueryBuilder 时,我无法访问返回的 SearchDescriptor 上的任何属性以验证我的测试,例如:

        [Test]
        public void BuildDocumentQuery_WithEmptyPhrase_ReturnsProperlyFormedQueryDescriptor()
        {
            var elasticSearchVideoQueryBuilder = new ElasticSearchVideoQueryBuilder();
            var descriptor = elasticSearchVideoQueryBuilder.BuildDocumentQuery(new ElasticSearchVideoParameters());
            descriptor.// How can I get the raw query?
        }

我在 source code 中看到似乎没有任何东西可以揭露这个?还有其他人有类似的问题吗?

好的,我找到了解决这个问题的方法,但这意味着我必须将 JSON 字符串解析回对象

        var elasticSearchVideoQueryBuilder = new ElasticSearchVideoQueryBuilder();
        var descriptor = elasticSearchVideoQueryBuilder.BuildDocumentQuery(new ElasticSearchVideoParameters());

        var settings = new ConnectionSettings(new Uri("http://localhost:123"));
        var client = new ElasticClient(settings);

        var jsonString = Encoding.UTF8.GetString(client.Serializer.Serialize(descriptor));

我不知道您是如何构建查询的,但您可以将 SearchDescriptor 转换为 ISearchRequest 以获取更多详细信息。

var searchDescriptor = new SearchDescriptor<ElasticsearchProject>()
    .From(0)
    .Size(20)
    .Query(q => q.Match(m => m.OnField(f => f.Country).Query("Country")));

var query = ((ISearchRequest) searchDescriptor).Query.Match.Query;

Assert.AreEqual("Country", query);