4

I'm running into exactly the same problem described (and not answered) here ElasticSearch NEST Search

I use:

  1. .NET Framework 4.5;

  2. ASP.NET MVC 5;

  3. Elasticsearch 1.6.0 (on a server);

  4. Elasticsearch.NET 1.6.1

  5. NEST 1.6.1

I have an MVC controller which has two actions:

  1. Index - which contains HTML UI form

  2. Search - which contains Elasticsearch.NET client and a query.

    public ActionResult Search(SearchCreteria sc)
    {
      Settings settings = new Settings();
      Client client = new Client(settings);
      ElasticsearchClient esClient = client.Get();
    
      var test = esClient.Search<Contract>(body => body.Query(query => query.QueryString(qs => qs.Query("test"))));
    
      return View(test);
    }
    

Entire "body => body.Query(query => query.QueryString(qs => qs.Query("test")))" lambda expression in the code above has squiggly red underline with the following tooltip:

(Parameter) ? body

Error:

Cannot convert lambda expression to type 'object' because it is not a delegate type

I googled the problem and found out that in 99% of cases folks forgot to include an assembly, typically System.Linq.

Well.. I definitely didn't forget to add that one, but I though maybe I have to include a NEST specific assembly or something like that (which I'm sure is not true, except for NEST itself), so I decided to add everything I though could be somewhat relevant and I ended up with this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Web.Mvc;
using WebUI.Models.Concrete;

using Domain.Concrete.Entities;
using Domain.Concrete.Connectivity.Elastic;
using Domain.Concrete.Processors.Elastic;

using Elasticsearch;
using Elasticsearch.Net;
using Elasticsearch.Net.Connection.Configuration;
using Elasticsearch.Net.Connection.RequestState;
using Elasticsearch.Net.Connection.Security;
using Elasticsearch.Net.ConnectionPool;
using Elasticsearch.Net.Exceptions;
using Elasticsearch.Net.Providers;
using Elasticsearch.Net.Serialization;

using Nest;
using Nest.Domain;
using Nest.DSL.Descriptors;
using Nest.DSL.Query;
using Nest.DSL.Query.Behaviour;
using Nest.DSL.Visitor;
using Nest.Resolvers.Converters.Aggregations;
using Nest.Resolvers.Converters.Filters;
using Nest.Resolvers.Converters.Queries;
using Nest.Resolvers.Writers;

It didn't help as expected, but was worth a try. So now, I'm not sure where is the problem and any help would be highly appreciated.

Community
  • 1
  • 1
Alex Purice
  • 43
  • 1
  • 6
  • your lambda is unknown. Try casting the lambda to Func. What ever the type of body is. `(Func)(body => {...})`... And you dont need to refrence any thing to solve that problem. because it irrelevant. Look at [here](http://stackoverflow.com/questions/23247125/cannot-convert-lambda-expression-to-type-object-because-it-is-not-a-delegate-t) – M.kazem Akhgary Jul 02 '15 at 23:49
  • (Parameter) ? body. What is the type of body? – M.kazem Akhgary Jul 02 '15 at 23:55
  • I can not reproduce your issue. Try adding instead of Contract and specifying the type in the search via .Type("yourType") or using .AllTypes() The latter will not be specific to contracts but perhaps will illuminate the actual problem – Daniel Hoffmann-Mitscherling Jul 03 '15 at 00:03
  • Daniel. That didn't work. – Alex Purice Jul 03 '15 at 00:18
  • Afraid I can not help without the ability to reproduce your issue. – Daniel Hoffmann-Mitscherling Jul 03 '15 at 01:04
  • Try to use ElasticClient instead of ElasticsearchClient. – Rob Jul 03 '15 at 08:15
  • Rob. That did work. Thanks for the tip. I didn't know that NEST has its own client. Everywhere in documentation you can find ElasticsearchClient only. – Alex Purice Jul 04 '15 at 18:07
  • Some posted a tip that actually worked for me and then deleted that tip. I don't post the tip as answer because it isn't an answer, but a helpful band-aid. So the tip was to use NEST's client instead of Elasticsearch.NET's client. I added some do to use that client and started to pull data from ES. Better than nothing, I guess. – Alex Purice Jul 07 '15 at 04:22

2 Answers2

5

Answer was already provided in comments on the question itself but adding this for future googlers.

Elasticsearch.NET

Is a barebones lowlevel ElasticsearchClient client, it only accepts strings, anonymous/dynamic objects, or byte[]. Similarly it has no return types for the responses either. This client supports connection pooling and node failover when so configured. 80% of this client is automatically generated.

This client is only useful if you are doing an integration with Elasticsearch that only exists of a handful of calls and you do not want to introduce a dependency on e.g Json.NET. An example of a library that only uses this is Serilog.Sinks.Elasticsearch

NEST

The high level ElasticClient client, has types for 99.9% of all the requests and responses. 99/100 times this is the client that you want to use. NEST uses Elasticsearch.NET under the hood to dispatch requests to to correct elasticsearch API endpoints and to use the same connection pooling and failover infrastructure.

Martijn Laarman
  • 13,476
  • 44
  • 63
  • It is correct in its nature. However, I would like to point out that information about ElasticClient is extremely limited. In fact, I didn't know that it existed before Rob (Rob Jul 3 at 8:15) told me about it. Google spits out this page http://nest.azurewebsites.net/nest/quick-start.html only if you search for ElasticClient, and the info is in 2 paragraphs on that page. If you missed it you are in trouble. So - yes, correct answer is always use ElasticClient, instead of ElasticserachClient. – Alex Purice Jul 15 '15 at 16:30
  • Thanks for the feedback Alex, we will update our quick start to explicitly mention this. – Martijn Laarman Jul 16 '15 at 10:17
-1

i can give you one example of how to use the NEST.

var node = new Uri(elasticSearchURI);
var connectionPool = new SniffingConnectionPool(new[] { node });

var config = new ConnectionSettings(connectionPool)
                        .SniffOnConnectionFault(false)
                        .SniffOnStartup(false)
                        .SetTimeout(600000)
                        .DisablePing();

_Instance = new ElasticClient(config);

var result = _Instance.Search<Location>(s => s
              .Index("index")
              .Type("type")
              .Query(q =>
              {
                QueryContainer locationQuery = null;
                locationQuery |= q.QueryString(qs=>qs.OnFields(f => f.RecordValue).Query(term).MinimumShouldMatchPercentage(100));
                return locationQuery;
              })
             .Take(1)
             .Sort(sort => sort.OnField("_score").Descending())
           );

Or if you don't want to use QueryContainers

   var result = _Instance.Search<Location>(s => s
                  .Index("index")
                  .Type("type")
                  .Query(q => q.QueryString(qs=>qs.OnFields(f => f.RecordValue).Query(term).MinimumShouldMatchPercentage(100))
                  .Take(10)
                  .Sort(sort => sort.OnField("_score").Descending())
               );
danvasiloiu
  • 751
  • 7
  • 24