本文介绍了使用InMemoryConnection测试ElasticSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕我们对ElasticSearch的使用(在使用Nest 1.4.2的C#中使用)进行测试,并希望使用InMemoryConnection,但我缺少某些东西(我认为是这样)并且没有成功.

I'm trying to add testing around our use of ElasticSearch (in C# using Nest 1.4.2) and want to use InMemoryConnection but I'm missing something (I assume) and having no success.

我已经创建了这个简单的Nunit测试用例,作为我的问题的简化示例

I've created this simple Nunit test case as a boiled down example of my issue

using System;
using Elasticsearch.Net.Connection;
using FluentAssertions;
using Nest;
using NUnit.Framework;

namespace NestTest
{
    public class InMemoryConnections
    {
        public class TestThing
        {
            public string Stuff { get; }

            public TestThing(string stuff)
            {
                Stuff = stuff;
            }
        }

        [Test]
        public void CanBeQueried()
        {
            var connectionSettings = new ConnectionSettings(new Uri("http://foo.test"), "default_index");

            var c = new ElasticClient(connectionSettings, new InMemoryConnection(connectionSettings));
            c.Index(new TestThing("peter rabbit"));

            var result = c.Search<TestThing>(sd => sd);

            result.ConnectionStatus.Success.Should().BeTrue();
        }
    }
}

查询成功,但是找不到我刚刚编入索引的文档...

the query is successful but doesn't find the document I just indexed...

如果我更新到NEST版本2.3.3和新语法

If I update to NEST version 2.3.3 and the new syntax

        [Test]
        public void CanBeQueried()
        {

            var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
            var settings = new ConnectionSettings(connectionPool, new InMemoryConnection());
            settings.DefaultIndex("default");

            var c = new ElasticClient(settings);

            c.Index(new TestThing("peter rabbit"));

            var result = c.Search<TestThing>(sd => sd);

            result.CallDetails.Success.Should().BeTrue();
            result.Documents.Single().Stuff.Should().Be("peter rabbit");
        }

它以相同的方式失败...即查询报告为成功,但返回0个文档

it fails in the same way... i.e. the query is reported as successful but returns 0 documents

推荐答案

InMemoryConnection实际上没有从Elasticsearch发送任何请求或接收任何响应;与连接设置上的.SetConnectionStatusHandler()(或NEST 2.x +中的.OnRequestCompleted())结合使用,这是查看请求的序列化形式的便捷方法.

InMemoryConnection doesn't actually send any requests or receive any responses from Elasticsearch; used in conjunction with .SetConnectionStatusHandler() on Connection settings (or .OnRequestCompleted() in NEST 2.x+), it's a convenient way to see the serialized form of requests.

当不使用InMemoryConnection但仍设置.SetConnectionStatusHandler().OnRequestCompleted()时(取决于NEST版本),当在NEST 1.x中也设置了.ExposeRawResponse(true).DisableDirectStreaming()分别在NEST 2.x +中设置.

When not using InMemoryConnection but still setting .SetConnectionStatusHandler() or .OnRequestCompleted(), depending on NEST version, it's a convenient way to also see the responses, when .ExposeRawResponse(true) is also set in NEST 1.x, or .DisableDirectStreaming() is set in NEST 2.x+, respectively.

使用NEST 1.x的示例

An example with NEST 1.x

void Main()
{
    var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
        .ExposeRawResponse(true)
        .PrettyJson()
        .SetDefaultIndex("myIndexName")
        .MapDefaultTypeNames(d => d.Add(typeof(myPoco), string.Empty))
        .SetConnectionStatusHandler(r =>
        {
            // log out the requests
            if (r.Request != null)
            {
                Console.WriteLine("{0} {1} \n{2}\n", r.RequestMethod.ToUpperInvariant(), r.RequestUrl,
                    Encoding.UTF8.GetString(r.Request));
            }
            else
            {
                Console.WriteLine("{0} {1}\n", r.RequestMethod.ToUpperInvariant(), r.RequestUrl);
            }

            Console.WriteLine();

            if (r.ResponseRaw != null)
            {
                Console.WriteLine("Status: {0}\n{1}\n\n{2}\n", r.HttpStatusCode, Encoding.UTF8.GetString(r.ResponseRaw), new String('-', 30));
            }
            else
            {
                Console.WriteLine("Status: {0}\n\n{1}\n", r.HttpStatusCode, new String('-', 30));
            }
        });

    var client = new ElasticClient(settings, new InMemoryConnection());

    int skipCount = 0;
    int takeSize = 100;

    var searchResults = client.Search<myPoco>(x => x
        .Query(q => q
        .Bool(b => b
        .Must(m =>
            m.Match(mt1 => mt1.OnField(f1 => f1.status).Query("New")))))
        .Skip(skipCount)
        .Take(takeSize)
    );
}

public class myPoco
{
    public string status { get; set;}
}

收益

POST http://localhost:9200/myIndexName/_search?pretty=true 
{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "status": {
              "query": "New"
            }
          }
        }
      ]
    }
  }
}


Status: 0
{ "USING NEST IN MEMORY CONNECTION"  : null }

------------------------------

对于NEST 2.x

And for NEST 2.x

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "default-index";
    var connectionSettings = new ConnectionSettings(pool, new InMemoryConnection())
            .DefaultIndex(defaultIndex)
            .PrettyJson()
            .DisableDirectStreaming()
            .OnRequestCompleted(response =>
                {
                    // log out the request
                    if (response.RequestBodyInBytes != null)
                    {
                        Console.WriteLine(
                            $"{response.HttpMethod} {response.Uri} \n" +
                            $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
                    }
                    else
                    {
                        Console.WriteLine($"{response.HttpMethod} {response.Uri}");
                    }

                    Console.WriteLine();

                    // log out the response
                    if (response.ResponseBodyInBytes != null)
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                    else
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                });

    var client = new ElasticClient(connectionSettings);

    int skipCount = 0;
    int takeSize = 100;

    var searchResults = client.Search<myPoco>(x => x
        .Query(q => q
        .Bool(b => b
        .Must(m =>
            m.Match(mt1 => mt1.Field(f1 => f1.status).Query("New")))))
        .Skip(skipCount)
        .Take(takeSize)
    );
}

您当然可以使用自己喜欢的模拟框架并根据客户端界面从客户端进行模拟/存根响应IElasticClient,如果那是您要采用的路线,尽管断言请求的序列化形式符合您在SUT中的期望就可以了.

You can of course mock/stub responses from the client using your favourite mocking framework and depending on the client interface, IElasticClient, if that's a route you want to take, although asserting the serialized form of a request matches your expectations in the SUT may be sufficient.

这篇关于使用InMemoryConnection测试ElasticSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:44