我正在尝试测试创建索引,添加文档并使用过滤器进行搜索。我遇到了似乎无法解决的编译器错误。谢谢你的帮助!
编译器错误(VS 2013,控制台应用程序):
“表达式树可能不包含动态操作。”
当我双击该错误时,它将在代码的.Filter行中突出显示“t.User”。
这是完整的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading.Tasks;
using Nest;
namespace elastic_search_client_app
{
class Post
{
public int Id;
public string User;
public DateTime PostDate;
public String Message;
}
class Program
{
static void Main(string[] args)
{
var uri = new Uri("http://es-server-name");
var settings = new ConnectionSettings(uri).SetDefaultIndex("stack-overflow-posts");
var client = new ElasticClient(settings);
var post = new Post() { Id = 2, User = "mk@microsoft.com", PostDate = DateTime.Now, Message = "This is pretty cool and lot of text stuff here from catalog summary collection" };
client.Index(post);
client.Refresh();
var result = client.Search<dynamic>(s => s
.Index("stack-overflow-posts")
.AllTypes()
.Query(q =>
q.QueryString(qs =>
qs.Query("cool")))
.Filter(f => f.Term(t => t.User, "mk@microsoft.com")));
}
}
}
最佳答案
An expression tree may not contain a dynamic operation.
该错误非常简单。
编译后的过滤器查询表达式
t => t.User, "mk@microsoft.com"
具有Expression<Func<T, object>> fieldDescriptor, object term)
的签名。编译器无法推断
T
,因为T
的类型为dynamic
。您可以将查询的类型更改为
client.Search<Post>
或使用string
重载。var result = client.Search<dynamic>(s => s
.Index("stack-overflow-posts")
.AllTypes()
.Query(q =>
q.QueryString(qs =>
qs.Query("cool")))
.Filter(f => f.Term("user", "mk@microsoft.com")));
关于c# - Elasticsearch Nest C#编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597850/