Marcus
Marcus

Reputation: 128

Error with NEST when selecting by type

I am using NEST to get the results from ElasticSearch by type.

So far I've tried this, but I get results with the all types.

    [HttpGet]
    public ActionResult List(string poiType, int? page, string sortName)
    {
        List<POIModel> resultsList = new List<POIModel>();
        var node = new Uri(WebConfigurationManager.AppSettings["ElasticSearchLocation"]);
        var settings = new ConnectionSettings(node);

        settings.DefaultIndex("pois");
        var client = new ElasticClient(settings);
        var result = client.Search<POIModel>(s => s.Type(poiType).MatchAll());
        resultsList = result.Hits.Select(t =>
        {
            t.Source.id = int.Parse(t.Id);
            return t.Source;
        }).ToList<POIModel>();

        return View(resultList);
    }

My index is "pois" and I try to select the results by poiType and map them into a List, but I get results from all types.

What can I do or try to get only one type ?

Upvotes: 0

Views: 38

Answers (1)

Russ Cam
Russ Cam

Reputation: 125498

Your code looks fine and results in the following request, where poiType is "poi-type"

POST http://localhost:9200/pois/poi-type/_search 
{
  "query": {
    "match_all": {}
  }
}

If you inspect the request (using a web debugging proxy like Fiddler, or using .OnRequestCompleted() along with DisableDirectStreaming() on ConnectionSettings), what do you see?

One recommendation I would make would be to create one ElasticClient as a singleton and pass this as a dependency to your controller; there are caches used per ConnectionSettings that can be relatively expensive to construct so it's a good idea to share a single instance and ElasticClient is thread-safe too so you can share a single instance of this too.

Upvotes: 1

Related Questions