1. <i id='K7r4a'><tr id='K7r4a'><dt id='K7r4a'><q id='K7r4a'><span id='K7r4a'><b id='K7r4a'><form id='K7r4a'><ins id='K7r4a'></ins><ul id='K7r4a'></ul><sub id='K7r4a'></sub></form><legend id='K7r4a'></legend><bdo id='K7r4a'><pre id='K7r4a'><center id='K7r4a'></center></pre></bdo></b><th id='K7r4a'></th></span></q></dt></tr></i><div id='K7r4a'><tfoot id='K7r4a'></tfoot><dl id='K7r4a'><fieldset id='K7r4a'></fieldset></dl></div>

        <bdo id='K7r4a'></bdo><ul id='K7r4a'></ul>

      <legend id='K7r4a'><style id='K7r4a'><dir id='K7r4a'><q id='K7r4a'></q></dir></style></legend>

    2. <small id='K7r4a'></small><noframes id='K7r4a'>

      <tfoot id='K7r4a'></tfoot>

    3. 将对象序列化为 JSON,然后使用该对象在使用 NEST 的弹性搜索中发送查询

      时间:2023-07-10

          <tbody id='ICGnd'></tbody>

        • <legend id='ICGnd'><style id='ICGnd'><dir id='ICGnd'><q id='ICGnd'></q></dir></style></legend>
          • <i id='ICGnd'><tr id='ICGnd'><dt id='ICGnd'><q id='ICGnd'><span id='ICGnd'><b id='ICGnd'><form id='ICGnd'><ins id='ICGnd'></ins><ul id='ICGnd'></ul><sub id='ICGnd'></sub></form><legend id='ICGnd'></legend><bdo id='ICGnd'><pre id='ICGnd'><center id='ICGnd'></center></pre></bdo></b><th id='ICGnd'></th></span></q></dt></tr></i><div id='ICGnd'><tfoot id='ICGnd'></tfoot><dl id='ICGnd'><fieldset id='ICGnd'></fieldset></dl></div>
          • <tfoot id='ICGnd'></tfoot>
              • <bdo id='ICGnd'></bdo><ul id='ICGnd'></ul>
              • <small id='ICGnd'></small><noframes id='ICGnd'>

                本文介绍了将对象序列化为 JSON,然后使用该对象在使用 NEST 的弹性搜索中发送查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在使用 NEST 进行查询时,我感到有些困惑和沮丧,因为它似乎很受欢迎.使用标准 JSON 时查询没有问题,所以我想知道是否有某种方法可以使用 JSON 对象进行查询,我有以下代码

                I get a bit confused and frustrated when it comes to using NEST to querying, as it seems very hit and miss. I have no trouble querying when using standard JSON, so I was wondering if there was some way to query using a JSON object, I have code below

                var query = "bkala";
                
                var q = new
                {
                    query = new
                    {
                        text = new
                        {
                            _all = "jane"
                        }
                    }
                };
                
                var qJson = JsonConvert.SerializeObject(q);
                var hits = client.Search<Users>(qJson);
                

                但是,我收到错误无法从类型字符串转换为 System.Func、Nest.ISearchRequest"

                However, I get the error "Cannot convert from type string to System.Func, Nest.ISearchRequest"

                如果有人知道我如何简单地使用 JSON 对象进行查询,那就太好了,提前欢呼.

                If anyone knows how I can simply query using a JSON object, that would be fantastic, cheers in advance.

                推荐答案

                NEST 和 Elasticsearch.Net 是 NEST 在后台使用的低级客户端,可以灵活地查询您希望的查询方式.使用 NEST,您有几种不同的方式:

                NEST and Elasticsearch.Net, the low level client that NEST uses under the covers, are flexible in how you wish to query. With NEST you have a couple of different ways:

                1.Fluent API

                var query = "bkala";
                
                var searchResult = client.Search<MyDocument>(s => s
                    .Query(q => q
                        .Match(m => m
                            .Field("_all")
                            .Query(query)
                        )
                    )
                );
                

                如上所示,此 API 使用 lambda 表达式来定义一个流畅的接口,模仿 Elasticsearch json API 的结构和查询 DSL.

                Laid out as above, this API uses lambda expressions to define a fluent interface that mimics the structure of the Elasticsearch json API and query DSL.

                2.对象初始化语法

                var query = "bkala";
                
                var request = new SearchRequest<MyDocument>
                {
                    Query = new MatchQuery
                    {   
                        Field = "_all",
                        Query = query
                    }
                };
                
                var searchResult = client.Search<MyDocument>(request);
                

                如果您不喜欢 lambda 表达式,那么您始终可以使用特定的搜索类型来定义您的搜索.

                If lambda expressions are not your thing, then you can always define your searches using specific search types.

                如果您想使用匿名类型(根据您的问题)、json 字符串或查询的字节表示进行查询,则可以使用低级客户端 Elasticsearch.Net 来实现此目的.低级客户端通过 .LowLevel 属性暴露在高级客户端上

                In cases where you would like to query with anonymous types (as per your question), json strings or a byte representation of a query, then you can use the low level client, Elasticsearch.Net, to achieve this. The low level client is exposed on the high level client through the .LowLevel property

                1.匿名类型

                var query = new
                {
                    query = new
                    {
                        match = new
                        {
                            _all = new
                            {
                                query = "bkala"
                            }
                        }
                    }
                };
                
                var searchResult = client.LowLevel.Search<SearchResponse<MyDocument>>(query);
                

                在高级客户端上使用低级客户端意味着您仍然可以利用使用 Json.NET 来反序列化搜索结果;在本例中,可以通过 searchResult.Body

                Using the low level client on the high level client means that you can still take advantage of using Json.NET to deserialize search results; in this example, the search response can be accessed through searchResult.Body

                2.Json字符串

                var query = @"
                {
                  ""query"": {
                    ""match"": {
                      ""_all"": {
                        ""query"": ""bkala""
                      }
                    }
                  }
                }";
                
                var searchResult = client.LowLevel.Search<SearchResponse<MyDocument>>(query);
                

                3.Byte 数组

                var bytes = new byte[] { 123, 13, 10, 32, 32, 34, 113, 117, 101, 114, 121, 34, 58, 32, 123, 13, 10, 32, 32, 32, 32, 34, 109, 97, 116, 99, 104, 34, 58, 32, 123, 13, 10, 32, 32, 32, 32, 32, 32, 34, 95, 97, 108, 108, 34, 58, 32, 123, 13, 10, 32, 32, 32, 32, 32, 32, 32, 32, 34, 113, 117, 101, 114, 121, 34, 58, 32, 34, 98, 107, 97, 108, 97, 34, 13, 10, 32, 32, 32, 32, 32, 32, 125, 13, 10, 32, 32, 32, 32, 125, 13, 10, 32, 32, 125, 13, 10, 125 };
                
                var searchResult = client.LowLevel.Search<SearchResponse<MyDocument>>(bytes);
                

                以上所有方法都会产生以下查询

                All of the above methods produce the following query

                {
                  "query": {
                    "match": {
                      "_all": {
                        "query": "bkala"
                      }
                    }
                  }
                }
                

                查看 github repo 上的入门指南 以及 Elastic 网站上的文档.我们一直在努力改进文档,对于您认为我们缺乏的领域,我们非常欢迎 PR :)

                Check out the Getting started guide on the github repo as well as the documentation on the Elastic website. We are continually working to improve documentation and PRs are more than welcome for areas where you feel we are lacking :)

                这篇关于将对象序列化为 JSON,然后使用该对象在使用 NEST 的弹性搜索中发送查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:MongoDB.Driver.Builders 如何分组并取平均值 下一篇:支持和反对从 SQL Server 迁移到 MongoDB 的原因

                相关文章

                <legend id='8SNMO'><style id='8SNMO'><dir id='8SNMO'><q id='8SNMO'></q></dir></style></legend>
                1. <i id='8SNMO'><tr id='8SNMO'><dt id='8SNMO'><q id='8SNMO'><span id='8SNMO'><b id='8SNMO'><form id='8SNMO'><ins id='8SNMO'></ins><ul id='8SNMO'></ul><sub id='8SNMO'></sub></form><legend id='8SNMO'></legend><bdo id='8SNMO'><pre id='8SNMO'><center id='8SNMO'></center></pre></bdo></b><th id='8SNMO'></th></span></q></dt></tr></i><div id='8SNMO'><tfoot id='8SNMO'></tfoot><dl id='8SNMO'><fieldset id='8SNMO'></fieldset></dl></div>

                2. <small id='8SNMO'></small><noframes id='8SNMO'>

                  • <bdo id='8SNMO'></bdo><ul id='8SNMO'></ul>

                    <tfoot id='8SNMO'></tfoot>