Search code examples
asp.net-mvc-3elasticsearchnestelasticsearch-marvel

Why do I get 0 match from ElasticSearch query (C#)?


I have some code which should return 5 matches from the search.

If I try the query in browser, I get 5 results:

http://localhost:9200/_search?q=Testing

If I user SENSE editor it also shows my 5 results:

Server=localhost:9200
POST _search
{
    "query": {
        "query_string": {
            "query": "Testing"
        }
    }
}

But my C# code in controller fails to get any match. What am I missing?

Uri localhost = new Uri("http://localhost:9200");
            var setting = new ConnectionSettings(localhost);
            setting.SetDefaultIndex("videos");
            var client = new ElasticClient(setting);

            var result = client.Search<SearchHint>(
                body => body.Query(
                    query => query.QueryString(
                        qs => qs.Query(keys))));

            var results = new SearchResults()
            {
                Results = result.Documents.ToList() <-- this has 0 items
            };

EDIT 1:

public class SearchHint
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public int NumItems { get; set; }
        public bool IsList { get; set; }

        public SearchHint(string id, string title, int numItems, bool isList)
        {
            Id = id;
            Title = title;
            NumItems = numItems;
            IsList = isList;
        }
    }

EDIT 2: There are 4 types in the index (videos\list, videos\video, videos\author, videos\category). Any search should search all types not any particular type.


Solution

  • I think the issue is related to the way that NEST is defaulting the types for your search. Unless you have specified and [ElasticType] attribute on your SearchHint class it will query Elasticsearch with the following url:

     /_all/searchhint/_search
    

    Try adding the typename that corresponds to the type you are using in your index, to your class definition, like the following (replacing mytype with the appropriate value for your index. Also, if you fields on the indexed items do not match the default mapping conventions (camel cased) you will not get data populated.

     [ElasticType(Name = "mytype"]
     public class SearchHint
     {
           // Will map to field with name title in your index.
           //Use the following property to specify an alternate name
           //[ElasticProperty(Name="Title")]
           public string Title { get; set;}
     }
    

    Please see NEST Documentation on Inference for an overview of how this all works.

    Update: The previous only applies to searching within a single type. If you want to search across multiple types, then you will need to specify .AllTypes() on your search query and there is not a need to specify the [ElasticType] attribute on your class.

                var result = client.Search<SearchHint>(
                    body => body
                      .AllTypes()
                      .Query(
                        query => query.QueryString(
                            qs => qs.Query(keys))));