我正在分离我的查询构建和查询执行,所以我有一个QueryBuilder,它返回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中看到,似乎没有什么可以揭露这个?还有其他人有类似的问题吗?
答案 0 :(得分:4)
我不知道您是如何构建查询的,但您可以将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);
答案 1 :(得分:1)
好的,我找到了解决方法,但这意味着我必须将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));