C# 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"

  }

}


手掌心
浏览 331回答 2
2回答

小唯快跑啊

问题的一部分现在已经解决了。QueryContainer query = new TermQuery()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field = "OrderId",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "1"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var searchRequest = new SearchRequest(index: "testindex")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query = query&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; var searchResult = client.Search<TestLogs>(searchRequest);&nbsp; foreach (var s in orderIdArray)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{i}:&nbsp; &nbsp; OrderId:" + s.OrderID + " Event: " + s.Event + " Time: " + s.TimeStamp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; }现在的问题只是如果我改变&nbsp;QueryContainer query = new TermQuery()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field = "OrderId",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "1"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp;到...&nbsp;QueryContainer query = new TermQuery()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field = "OrderId",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "1"&nbsp; &nbsp; &nbsp; &nbsp; };它不会给出任何输出是测试方法 ELK_algorithmsTests.TestIndexCreation.testToMakeSameSelectionButWithSelectedEvent 抛出异常:System.IndexOutOfRangeException: ..有什么建议应该在哪里看?因为我现在没有想法了..

翻翻过去那场雪

使用 type 时TestLogs,客户端如何知道目标testconnectiones索引?有几件事情需要考虑:确保testconnectiones查询索引确保f => f.OrderID将序列化为字符串值以针对 Elasticsearch 中存在的类型和索引的字段。默认情况下,序列化时 NEST 骆驼案例属性名称确保查询有一个输入,使其不被视为“conditionless”,或者使用.Verbatim()序列化查询完全一样的确保索引文档可用于搜索
打开App,查看更多内容
随时随地看视频慕课网APP