0

Given

string author = "John Smith";

The following lambda query using NEST works:

string firstname = author.Split().First().ToLower();
var searchResults = client.Search<Magazine>(s => s.From(0).Size(5000).Query(q => q.Term(p => p.Author, author)));

However, the same query using OIS (Object Initializer Syntax) does not work:

author = author.Split().First().ToLower();

QueryContainer query = new TermQuery
{
   Field = "Author",
   Value = author
};

var searchRequest = new SearchRequest
{
   From = 0,
   Size = 10,
   Query = query
};

var searchResults = client.Search<Magazine>(searchRequest);

What is wrong?

user1052610
  • 4,440
  • 13
  • 50
  • 101
  • This should help you http://stackoverflow.com/questions/28312465/elasticsearch-nest-library-wired-behavior/28333375 – Rob Apr 02 '15 at 18:21

2 Answers2

0

Try camel casing the field name:- I think this is needed when using the OIS syntax or raw query.

author = author.Split().First().ToLower();

QueryContainer query = new TermQuery
{
   Field = "author",
   Value = author
};

var searchRequest = new SearchRequest
{
   From = 0,
   Size = 10,
   Query = query
};

var searchResults = client.Search<Magazine>(searchRequest); 
Vineet
  • 401
  • 5
  • 15
0

Add "keyword" suffix to QueryContainer:

QueryContainer query = new TermQuery
{
   Field = "Author",
   Value = author
};
query.Suffix("keyword");
var searchRequest = new SearchRequest
{
   From = 0,
   Size = 10,
   Query = query
};

var searchResults = client.Search<Magazine>(searchRequest); 

try it it will work