# ElasticSearch Result using NEST

**URL:** <https://discuss.elastic.co/t/elasticsearch-result-using-nest/47847>\
**Category:** Elasticsearch\
**Created:** [April 20, 2016, 2:37am UTC](https://discuss.elastic.co/t/elasticsearch-result-using-nest/47847 "2016-04-20T02:37:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ASN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asn/32/10535_2.png) [@ASN](https://discuss.elastic.co/u/ASN)\
**Post date:** [April 20, 2016, 2:37am UTC](https://discuss.elastic.co/t/elasticsearch-result-using-nest/47847/1 "2016-04-20T02:37:08Z")

</div>

Hi all. I'm using NEST for ES in .net

```
 string wordInfoQuery = @"{
                ""query"": {
                    ""match_phrase"":{
                        ""Answer"":{
                                      ""query"": ""brown dog"",
                                      ""slop"": "3"
                                    }
                                }
                            }
                        }";

           

           try
                {
       
                    if (client != null)
                    {
                        var callResult = client.LowLevel.Search<string>(wordInfoQuery);
                        Response.Write(client.LowLevel);
                        Response.Write("query=" + wordInfoQuery);
                        Response.Write(callResult.Body);
                    }
                    
                }

```

In the above code, when i run the code in debug mode, I'm getting a successful post message with query result(for eg. max\_Score = 0.28) where as if i run the code in browser mode, it is still making a call to ES , but the result is empty(max\_score =""). I dont know why is this happening.  
someone please help in solving this.

thanks in advance

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 21, 2016, 7:07am UTC](https://discuss.elastic.co/t/elasticsearch-result-using-nest/47847/2 "2016-04-21T07:07:21Z")

</div>

A few observations

1. You use `"Answer"` as the field name for the `match_phrase` query in your json but NEST by default will camel case all CLR property names when mapping, indexing, searching, etc. This should be `"answer"` or [change the default field name inference on Connection Settings](https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/field-inference.html#camel-casing)
2. When there are no results for a `match_phrase` query, `max_score` will be `null` so you will need to handle this case. In your example, a `slop` of 3 produces no hits for the query so `max_score` is `null`. If you change `slop` to 5, you get the document back and a value for `max_score`.
3. I'd recommend using the [Fluent API](https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/match-phrase-usage.html#_fluent_dsl_example_32) or the [Object Initializer Syntax](https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/match-phrase-usage.html#_object_initializer_syntax_example_32) in NEST unless you have good reason not to (and if you do have a reason, we'd love to know!). There are **[usage examples of all of the Query DSL for NEST 2.x](https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/query-dsl.html)** that should hopefully help 🙂

An example

```auto
void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "zzz";

    var connectionSettings = new ConnectionSettings(pool)
            .DefaultIndex(defaultIndex)
            .MapDefaultTypeNames(m => m.Add(typeof(Class1), "omg"))
            .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}");
                    }

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

    if (client.IndexExists(defaultIndex).Exists)
    {
        client.DeleteIndex(defaultIndex);
    }

    client.CreateIndex(defaultIndex);

    client.Index(new Class1
    {
        Id = 1,
        Answer = "The quick brown fox jumps over the lazy dog"
    }, i => i.Refresh());

    var searchResponse = client.Search<Class1>(s => s
        .Query(q => q
            .MatchPhrase(mp => mp
                .Field(f => f.Answer)
                .Query("brown dog")
                .Slop(5)
            )
        )
    );

    Console.WriteLine(searchResponse.MaxScore);
}

public class Class1
{
    public int Id { get; set; }
    public string Answer { get; set; }
}

```

Writes out the following to the console

```auto
HEAD http://localhost:9200/zzz?pretty=true
Status: 200

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

DELETE http://localhost:9200/zzz?pretty=true
Status: 200
{
  "acknowledged" : true
}

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

PUT http://localhost:9200/zzz?pretty=true 
{}
Status: 200
{
  "acknowledged" : true
}

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

PUT http://localhost:9200/zzz/omg/1?pretty=true&refresh=true 
{
  "id": 1,
  "answer": "The quick brown fox jumps over the lazy dog"
}
Status: 201
{
  "_index" : "zzz",
  "_type" : "omg",
  "_id" : "1",
  "_version" : 1,
  "_shards" : {
	"total" : 2,
	"successful" : 1,
	"failed" : 0
  },
  "created" : true
}

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

POST http://localhost:9200/zzz/omg/_search?pretty=true 
{
  "query": {
	"match": {
	  "answer": {
		"type": "phrase",
		"query": "brown dog",
		"slop": 5
	  }
	}
  }
}
Status: 200
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
	"total" : 5,
	"successful" : 5,
	"failed" : 0
  },
  "hits" : {
	"total" : 1,
	"max_score" : 0.07829509,
	"hits" : [ {
	  "_index" : "zzz",
	  "_type" : "omg",
	  "_id" : "1",
	  "_score" : 0.07829509,
	  "_source" : {
		"id" : 1,
		"answer" : "The quick brown fox jumps over the lazy dog"
	  }
	} ]
  }
}

------------------------------ 
0.07829509

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:55pm UTC](https://discuss.elastic.co/t/elasticsearch-result-using-nest/47847/4 "2017-07-05T22:55:41Z")

</div>


