我正在尝试在Elasticsearch中进行简单的搜索。
在奇迹中,我使用以下查询获得结果:
GET /gl_search/poc/_search
{
"query": {
"term": {
"Account_No": "056109"
}
}
}
类型是poc,index是gl_search。见下文
{
"took": 28,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 23586,
"max_score": 4.7722025,
"hits": [
{
"_index": "gl_search",
"_type": "poc",
"_id": "AU7fza_MU2-26YcvKIeM",
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(
node,
defaultIndex: "gl_search"
);
使用NEST时,我没有得到任何结果。我尝试了以下方法:
var r = client.Search<poc>(s => s
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Account_No, "056109")
)
和
var r = client.Search<poc>(query => query.Index("gl_search")
.Type("poc")
.From(0)
.Size(100)
.Filter(x => x.Term(p=> p.Journal_ID, "056109")));
设置为:
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(
node,
defaultIndex: "gl_search"
);
更新
我在Fiddler看不到任何东西。我想我会在那里看到一些东西。如果是这样,是我设置它的方式还是阻止它的某种病毒保护
答案 0 :(得分:1)
正如评论中所指出的,在将SearchDescriptor<T>
序列化到查询DSL中时,NEST默认会使用camelcase .NET对象属性名称。所以,在上面的例子中,
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var connection = new InMemoryConnection(settings);
var client = new ElasticClient(connection: connection);
var r = client.Search<poc>(s => s
.Index("gl_search")
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Account_No, "056109")
)
);
Console.WriteLine(Encoding.UTF8.GetString(r.RequestInformation.Request));
}
生成以下查询DSL(注意属性为account_No
)
{
"from": 0,
"size": 10,
"query": {
"term": {
"account_No": {
"value": "056109"
}
}
}
}
现在,有两个真正的选项可以解决这个问题
1.使用属性名称的字符串,绕过NEST的camelcasing约定,但代价是不使用lambda表达式和它们提供的所有类型安全性
var r = client.Search<poc>(s => s
.Index("gl_search")
.From(0)
.Size(10)
.Query(q => q
.Term("Account_No", "056109")
)
);
2.更改NEST的默认序列化选项;如果所有属性都按照您的示例加入
,则非常有用 // change the default camelcase property name serialization
// behaviour with .SetDefaultPropertyNameInferrer
var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
.SetDefaultPropertyNameInferrer(s => s);
var client = new ElasticClient(settings);
var r = client.Search<poc>(s => s
.Index("gl_search")
.From(0)
.Size(10)
.Query(q => q
.Term(p => p.Account_No, "056109")
)
);
Func<string, string>
传递给.SetDefaultPropertyNameInferrer()
,将为每个将被序列化的属性调用,以形成发送到Elasticsearch的json查询DSL。