C#NEST查询无法正确打印出数据

时间:2018-08-14 13:38:33

标签: c# elasticsearch nest

我陷于一个问题,显然我可能误解了一些东西,但这就是问题所在。

我在数据库中有一些测试数据,但是当我运行它时:

与弹性连接

  private string uri = "http://localhost:9200/";
  private string testindex = "testconnectiones";

  private static ElasticClient GetClient(string testindex, string uri)
    {
        var pool = new SingleNodeConnectionPool(new Uri(uri));
        var connectionSettings =
            new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default).DefaultIndex(testindex);

        return new ElasticClient(connectionSettings);
    }

然后

 public void TestCreateIndexBaseOnOrderId1() 
 {
     var client = GetClient(testindex, uri);
     var searchResponse = client.Search<TestLogs>(s => s
            .AllTypes()
            .From(0)
            .Size(1000)
            .Analyzer("standard")
            .Query(q => q
                .Match(m => m
                    .Field(f => f.OrderID)
                    .Query("")

                )
            )
        );

     var eventTestArray = searchResponse.Documents.Select(x => new {x.OrderID }).ToArray();
     Console.WriteLine("searchResponse.Documents.Count: " + searchResponse.Documents.Count());

     var i = 0;
     var j = 0;
     foreach (var s in eventTestArray)
     {
         Console.WriteLine($"{i}:    " + s);
         i++;
     }

     Assert.AreNotEqual(eventTestArray, null);
 }

输出仅为:

searchResponse.Documents.Count: 0.

搜索响应中应该有10个订单。

我只是想知道我是否误解了什么。

数据库中的数据看起来像这样

{
  "_index": "testconnectiones",
  "_type": "logs",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
     "OrderId": 1,
     "Event": "CreateOrder"
  }
}

2 个答案:

答案 0 :(得分:0)

使用类型TestLogs时,客户如何知道以testconnectiones索引为目标?

有几件事要考虑:

  1. 确保查询testconnectiones索引
  2. 确保f => f.OrderID会序列化为字符串值,以定位Elasticsearch中用于类型和索引的字段。默认情况下,NEST骆驼在序列化时会使用属性名称
  3. 确保查询具有输入,这样就不会将其视为“无条件”输入,或者using .Verbatim()可以完全按原样对查询进行序列化
  4. Ensuring that indexed documents are available for search

答案 1 :(得分:0)

现在解决了一部分问题。

QueryContainer query = new TermQuery()
        {
            Field = "OrderId",
            Value = "1"
        };
        var searchRequest = new SearchRequest(index: "testindex")
        {
            Query = query
        };
        var searchResult = client.Search<TestLogs>(searchRequest);
  foreach (var s in orderIdArray)
        {

            Console.WriteLine($"{i}:    OrderId:" + s.OrderID + " Event: " + s.Event + " Time: " + s.TimeStamp);
            i++;
        }

现在只有当我更改

时才是问题
 QueryContainer query = new TermQuery()
        {
            Field = "OrderId",
            Value = "1"
        }; 

到...

 QueryContainer query = new TermQuery()
        {
            Field = "OrderId",
            Value = "1"
        };

不会给出任何输出是Test方法ELK_algorithmsTests.TestIndexCreation.testToMakeSameSelectionButWithSelectedEvent引发异常: System.IndexOutOfRangeException:..

任何建议应该放在哪里?因为我现在没有想法。