C# Elasticsearch NEST cannot convert lambda expression

柔情痞子 提交于 2020-01-24 05:38:25

问题


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.


回答1:


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.




回答2:


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())
               );


来源:https://stackoverflow.com/questions/31196256/c-sharp-elasticsearch-nest-cannot-convert-lambda-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!