简体   繁体   English

使用NEST的Elasticsearch-无结果

[英]Elasticsearch using NEST - No Results

I am trying to do a simple search in Elasticsearch. 我正在尝试在Elasticsearch中进行简单的搜索。

in marvel, I am getting the results back using the following query: 令人惊奇的是,我使用以下查询返回结果:

    GET /gl_search/poc/_search
{
  "query": {

    "term": {
      "Account_No": "056109"
    }
  }
}

The type is poc and index is gl_search. 类型是poc,索引是gl_search。 See below 见下文

{
   "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"
        );

When using NEST, I am not getting any results back. 使用NEST时,没有得到任何结果。 I tried the following: 我尝试了以下方法:

        var r = client.Search<poc>(s => s
                    .From(0)
                    .Size(10)
                    .Query(q => q
                        .Term(p => p.Account_No, "056109")


            )

and

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

Settings are: 设置为:

        var node = new Uri("http://localhost:9200");

        var settings = new ConnectionSettings(
            node,
            defaultIndex: "gl_search"
        );

Update 更新资料

I dont see anything in Fiddler. 我没有在Fiddler中看到任何东西。 I assume I would see something there. 我想我在那里会看到一些东西。 If so, is it the way I set it up or something kind of virus protection blocking it 如果是这样,是我设置的方式还是某种病毒防护阻止了它

As pointed out in the comments, NEST by default will camelcase .NET object property names when serializing the SearchDescriptor<T> into the query DSL. 正如评论中指出的那样,默认情况下,将SearchDescriptor<T>序列化到查询DSL时,NEST默认将使用驼峰式.NET对象属性名称。 So, in your example above, 因此,在上面的示例中,

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

produces the following query DSL ( notice the property is account_No ) 产生以下查询DSL( 注意,属性为account_No

{
  "from": 0,
  "size": 10,
  "query": {
    "term": {
      "account_No": {
        "value": "056109"
      }
    }
  }
}

Now, there are two real options for addressing this 现在,有两个真正的解决方案

1.Use a string for the property name, bypassing NEST's camelcasing convention but at the expense of not working with lambda expressions and all of the type safety that they provide 1.使用字符串作为属性名称,绕过NEST的驼峰约定,但是以不使用lambda表达式及其提供的所有类型安全为代价

var r = client.Search<poc>(s => s
            .Index("gl_search")
            .From(0)
            .Size(10)
            .Query(q => q
               .Term("Account_No", "056109")
            )
        );

2.Change the default serialization options for NEST; 2.更改NEST的默认序列化选项; useful if all of you properties are cased as per your examples 如果按照示例对所有属性进行了大小写,则很有用

    // 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")
                )
            );

A Func<string, string> is passed to .SetDefaultPropertyNameInferrer() that will be called for each property that will be serialized to form the json query DSL that is sent to Elasticsearch. Func<string, string>传递给.SetDefaultPropertyNameInferrer() ,它将为将被序列化以形成发送到Elasticsearch的json查询DSL的每个属性调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM