简体   繁体   中英

C# NEST query does not print out data correctly

I'm stuck at a problem, apparently I might have misunderstood something, but here is the problem.

I've got some testdata in a database, but when I run this:

Connection to elastic

  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);
    }

Then

 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);
 }

the output is only:

searchResponse.Documents.Count: 0.

There should be 10 orders in the search response.

I just wonder if I've misunderstood something.

The data in the database looks for example like this

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

When using the type TestLogs , how does the client know to target testconnectiones index?

There are a few things to consider:

  1. Ensuring that testconnectiones index is queried
  2. Ensuring that f => f.OrderID will serialize to a string value to target a field that exists for the type and index in Elasticsearch. By default, NEST camel cases property names when serializing
  3. Ensuring that the query has an input so that it is not considered "conditionless", or using .Verbatim() to serialize the query exactly as is
  4. Ensuring that indexed documents are available for search

A part of the problem was now solved went by.

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++;
        }

Now is the problem only that if i change

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

to...

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

It wont give any output is Test method ELK_algorithmsTests.TestIndexCreation.testToMakeSameSelectionButWithSelectedEvent threw exception: System.IndexOutOfRangeException: ..

any suggestion where is should look? because im out of ideas right now..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM