Elasticsearch.net客户端无法做基本的搜索 [英] Elasticsearch.net client can't do basic search
问题描述
我有一个基本的Elasticsearch查询,看起来像这样
POST /水果/ _search
{查询 :{术语:{姓名:香蕉}}}
我得到的结果返回当我在某种意义上运行没有任何问题。
于是,我试着做这elasticsearch.net
VAR requestBody =新的{查询= {新学期新= {名称=香蕉}}};
VAR的结果= client.Search<串GT(果,requestBody);
和我没有得到任何结果回来。如果我只是用新的{}然后我得到的点击,而不是过滤。
我在做什么错了?
<搜索体DIV CLASS =h2_lin>解决方案
如果您使用低级别的客户端(elasticsearch.net)直接也不会做任何正常化和逐字连载对象:
VAR的查询=新的{查询= {新学期新= {名称=香蕉}}};
变种JSON =新ElasticsearchClient()Serializer.Serialize(查询).Utf8String();
这将导致以下JSON:
{
查询:{
术语:{
名:香蕉
}
}
}
如果您使用NEST的默认行为是驼峰属性名称(窝自以为是):
{
查询:{
术语:{
名:香蕉
}
}
}
如果您通过高层次的客户端使用低水平的客户端( client.Raw
),它会使用完全一样的序列化设置为高电平客户端。
您可以控制通过高水平的客户端上的这种行为:
变种connectionSettings =新ConnectionSettings()
.SetDefaultPropertyNameInferrer(p值=指p);
VAR的客户=新ElasticClient(connectionSettings);
I have a basic Elasticsearch query that looks like this
POST /fruit/_search
{"query":{"term":{"Name":"banana"}}}
I get result back, no problems when I run in sense.
So I try to do this in elasticsearch.net
var requestBody = new { query = new { term = new { Name = "banana" } } };
var result = client.Search<string>("fruit", requestBody);
And I get no results back. If I just have a search body with new {} then I get hits, but not filtered.
What am I doing wrong?
If you use the low level client (elasticsearch.net) directly it will not do any normalisation and serialise the object verbatim:
var query = new { query = new { term = new { Name = "banana" } } };
var json = new ElasticsearchClient().Serializer.Serialize(query).Utf8String();
this will result to the following json:
{
"query": {
"term": {
"Name": "banana"
}
}
}
If you use NEST the default behaviour is to camelCase property names (NEST is opinionated):
{
"query": {
"term": {
"name": "banana"
}
}
}
If you use the low level client through the high level client (client.Raw
) it will use the exact same serialisation settings as the high level client.
You can control this behaviour on the high level client through:
var connectionSettings = new ConnectionSettings()
.SetDefaultPropertyNameInferrer(p=>p);
var client = new ElasticClient(connectionSettings);
这篇关于Elasticsearch.net客户端无法做基本的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!