Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch NEST Search Multiple Types & All Fields

Using ElasticSearch NEST, I am having trouble getting expected results back from my queries. My index/type layout is as follows:

  • theatres (index)
    • event (types)
    • theatre
    • promotion
    • generic content

Each of those types have their own fields, and I am using NEST's Index() method to index the data. I can verify that it's being indexed properly by:

  • Looking at http://localhost:9200/theatres/_mapping
  • Using the Head plugin to view data

For reference, here is my client configuration:

// TODO: Put settings in config
var node = new Uri("http://localhost:9200");
var connSettings = new ConnectionSettings(node);
connSettings.SetDefaultIndex("theatres");
connSettings.ThrowOnElasticsearchServerExceptions();

var client = new ElasticClient(connSettings);

The Query

Now, for the query, I want to search all types and all fields within the index. Using the Head plugin, I am able to generate the query and get the expected results: enter image description here

Using that query that it generated, I tried the following NEST query:

var query = "waukesha"; // This would be passed in

var resp = client.Search<dynamic>(s => s
   .From(0)
   .Take(10)
   .Query(qry => qry
       .Bool(b => b
       .Must(m => m
           .QueryString(qs => qs
               .DefaultField("_all")
               .Query(query))))));

However, this gives me a different result. Is NEST doing something behind the scenes that I'm not aware of? Or is this not supported?

like image 517
Matt Millican Avatar asked Jun 26 '15 00:06

Matt Millican


1 Answers

Your query is missing .AllTypes()

You can also specify multiple types using .Types("type1", "type1")

So:

var query = "waukesha"; // This would be passed in

var resp = client.Search<dynamic>(s => s
   .AllTypes()
   .From(0)
   .Take(10)
   .Query(qry => qry
       .Bool(b => b
       .Must(m => m
           .QueryString(qs => qs
               .DefaultField("_all")
               .Query(query))))));
like image 185
Daniel Hoffmann-Mitscherling Avatar answered Oct 18 '22 23:10

Daniel Hoffmann-Mitscherling