14

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?

Matt Millican
  • 4,044
  • 4
  • 38
  • 55
  • I would suggest .Size(10) instead of .Take(20) just to remove that as a variable for error, although the functionality should be the same. Also I believe you can remove .DefaultField and all fields will be searched. Could you try that? I'm curious about this – Daniel Hoffmann-Mitscherling Jun 26 '15 at 01:11
  • 1
    @DanielHoffmann-Mitscherling i updated this and still get 0 results, when I should get 1. – Matt Millican Jun 26 '15 at 01:14
  • 1
    So weird! Could you add back DefaultField("_all") and also add .AllTypes()? Manually forcing NEST to create a query with all types might give us more info – Daniel Hoffmann-Mitscherling Jun 26 '15 at 01:24
  • 2
    @DanielHoffmann-Mitscherling that worked! After testing that, I changed it to `.AllTypes()` and that also works. Now I wonder if I can simplify this query? – Matt Millican Jun 26 '15 at 01:31
  • Great! Do you mind if I post that as an answer for future people reading this thread? I believe that if you use a simple query or term query it should also function but be slightly more elegant. – Daniel Hoffmann-Mitscherling Jun 26 '15 at 01:42
  • 1
    @DanielHoffmann-Mitscherling absolutely! thanks! – Matt Millican Jun 26 '15 at 01:45

1 Answers1

18

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))))));
Ben Aubin
  • 5,542
  • 2
  • 34
  • 54
  • Can we use type based Term filter here. Example: Two types: "type1", "type2" type1 having: name, locationid, type2 having: countryid and stateid using this how can we achive type based term filter here? – Mohan Gopi Feb 02 '16 at 11:48
  • @MohanGopi -- Yes: `.Type().Type()` – Josh M. Sep 29 '17 at 20:45