MiDu
MiDu

Reputation: 45

Simple NEST search not returning results

Here is my Cat class:

public class Cat
{
    public string Id { get; set; }
    public string Name { get; set; }
}

Here is my main program where I add a cat to the index and do a simple search:

static void Main(string[] args)
{
    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node);
    settings.SetDefaultIndex("mdu-test");
    var client = new ElasticClient(settings);

    var cat = new Cat
    {
        Id = "1",
        Name = "Martijn",
    };
    var index = client.Index(cat);

    var searchResults = client.Search<Cat>(s => s
        .From(0)
        .Size(10)
        .Query(q => q
           .Term(p => p.Name, "Martijn")
        )
    );

    Cat firstCat = searchResults.Documents.ToList()[0];
}

Using Curl I can see the document is added, but nothing is returned by my code. The funny thing is that I'm sure I had the same code working earlier. Any help with the solution or debugging would be greatly appreciated. Thanks in advance.

Upvotes: 1

Views: 217

Answers (1)

Greg Marzouka
Greg Marzouka

Reputation: 3325

You are using a term query, which is not analyzed- meaning it will only find exact matches (case-sensitive). I'm assuming you are using the standard analyzer (default) on your index, which is lower-casing all of your terms.

If you do want exact matches, then set Name to not_analyzed in your mapping, otherwise you probably want to use a match query instead, like so:

var searchResults = client.Search<Cat>(s => s
    .From(0)
    .Size(10)
    .Query(q => q
        .Match(m => m
            .OnField(p => p.Name)
            .Query("Martijn"))
    )
);

Upvotes: 2

Related Questions