1

I am trying to write a simple console app using C# and NEST to learn more about Elasticsearch.

I can run the following query in Sense (Kibana)

GET /companies/company/_search
{
    "query": {
          "match": {
              "dbaName": "STEAK"
         }
     }
}

and I will get back the following result:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.15342641,
    "hits": [
      {
        "_index": "companies",
        "_type": "company",
        "_id": "1",
        "_score": 0.15342641,
        "_source": {
          "dbaName": "We do steak",
          "primaryLicenseStatus": "T",
          "primaryLicenseDescription": "Restuarant",
          "lastActivityDate": "2016-06-06T08:25:23.4136549-04:00"
        }
      }
    ]
  }
}

However, when I try to do this query using NEST, I get no results:

var response = client.Search<Company>(s => s
    .Index(theIndex)
    .Query(q =>
        q.Match(m => m.Field(f => f.DbaName).Query("steak"))
    )
);

UPDATE - I figured it out!
So I feel like I should take this post down since the question has no relevance to the solution.

Spoiler: My query in NEST is fine.

I had created a console app that would start by deleting the index, re-creating it, insert the Company and then search for it.
The server was not keeping up with my console app and only when I stopped deleting/recreating the index was I able to get the search to work. (facepalm)
If any feel that I should simply delete this thread, let me know in the comments below and I will remove it, but someone may find it helpful.

Airn5475
  • 2,452
  • 29
  • 51
  • What does the serialized form of the NEST query look like? What analyzer (if any) is set on the `dbaName` field? – Russ Cam Jul 06 '16 at 13:06
  • Thanks Russ for your response. I apologize, I am a bit of a newb to Elasticsearch and am not 100% sure how to get answers for your questions. I have not specified an analyzer, so I am assuming the default is being used. I've poked around how to find out and am not coming up with a solid article to explain. – Airn5475 Jul 06 '16 at 13:35
  • 1
    No worries :) You can log requests/responses with something like Fiddler (`targeting `ipv4.fiddler:9200` instead of `localhost:9200`), or you can use `OnRequestCompleted` as per - http://stackoverflow.com/a/38215585/1831. Documents that are indexed are not immediately available in search, you need to wait for the refresh interval (default is 1 second) before they will appear in search documents. You can manually refresh which is useful for testing but should try to avoid calling it frequently in production. – Russ Cam Jul 07 '16 at 00:31

1 Answers1

0

The first step in your debugging process should be to view the request that is being made to ES and compare it with what you're expecting.

Here is a great extension method to get the raw json that was sent to ES:

    /// <summary>
    /// Debugging method to get the json that was sent to ES
    /// </summary>
    public static string GetRequestString(this IResponse response)
    {
        var request = response.RequestInformation.Request;
        if (request != null)
        {
            return Encoding.Default.GetString(request);
        }
        return response.RequestInformation.RequestUrl;
    }
jhilden
  • 12,207
  • 5
  • 53
  • 76
  • Thanks for the response. Two things: First: `response.RequestInformation` is throwing an error. I'm using NEST 2.3.3 if that matters. Second: my `var request = response.ApiCall.RequestBodyInBytes;` is returning null. Thoughts? – Airn5475 Jul 06 '16 at 15:41
  • Okay I had to change a setting to get the Request Body back. The body is this: `{"query":{"match":{"dbaName":{"query":"steak"}}}}` Which when I run in Sense, it works. Thoughts? – Airn5475 Jul 06 '16 at 15:51