简体   繁体   English

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

[英]Serialising an object to JSON, and then using that to send a query in elastic search using NEST

I get a bit confused and frustrated when it comes to using NEST to querying, as it seems very hit and miss. 在使用NEST进行查询时,我感到有些困惑和沮丧,因为它似乎很受欢迎。 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 使用标准JSON时查询没有问题,所以我想知道是否存在使用JSON对象进行查询的方法,下面有代码

var query = "bkala";

var q = new
{
    query = new
    {
        text = new
        {
            _all = "jane"
        }
    }
};

var qJson = JsonConvert.SerializeObject(q);
var hits = client.Search<Users>(qJson);

However, I get the error "Cannot convert from type string to System.Func, Nest.ISearchRequest" 但是,我收到错误消息“无法从类型字符串转换为System.Func,Nest.ISearchRequest”

If anyone knows how I can simply query using a JSON object, that would be fantastic, cheers in advance. 如果有人知道我如何可以简单地使用JSON对象进行查询,那就太好了,先加油打气。

NEST and Elasticsearch.Net, the low level client that NEST uses under the covers, are flexible in how you wish to query. NEST和Elasticsearch.Net是NEST在后台使用的低级客户端,可以灵活地查询。 With NEST you have a couple of different ways: 使用NEST,您有两种不同的方式:

NEST - High level client NEST-高级客户

1.Fluent API 1.Fluent API

var query = "bkala";

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

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. 如上所述,此API使用lambda表达式来定义一个流畅的接口,该接口模仿Elasticsearch json API和查询DSL的结构。

2.Object Initializer Syntax 2.对象初始化语法

var query = "bkala";

var request = new SearchRequest<MyDocument>
{
    Query = new MatchQuery
    {   
        Field = "_all",
        Query = query
    }
};

var searchResult = client.Search<MyDocument>(request);

If lambda expressions are not your thing, then you can always define your searches using specific search types. 如果不是lambda表达式,那么您始终可以使用特定的搜索类型定义搜索。

Elasticsearch.Net - Low level client Elasticsearch.Net-低级客户端

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. 如果您想使用匿名类型(根据您的问题),json字符串或查询的字节表示形式进行查询,则可以使用低级客户端Elasticsearch.Net来实现。 The low level client is exposed on the high level client through the .LowLevel property 通过.LowLevel属性在低级别客户端上公开低级别客户端

1.Anonymous types 1,匿名类型

var query = new
{
    query = new
    {
        match = new
        {
            _all = new
            {
                query = "bkala"
            }
        }
    }
};

var searchResult = client.LowLevel.Search<SearchResponse<MyDocument>>(query);

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; 在高级客户端上使用低级客户端意味着您仍然可以利用Json.NET对搜索结果进行反序列化; in this example, the search response can be accessed through searchResult.Body 在此示例中,可以通过searchResult.Body访问搜索响应。

2.Json string 2.杰森弦

var query = @"
{
  ""query"": {
    ""match"": {
      ""_all"": {
        ""query"": ""bkala""
      }
    }
  }
}";

var searchResult = client.LowLevel.Search<SearchResponse<MyDocument>>(query);

3.Byte array 3.字节数组

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"
      }
    }
  }
}

Check out the Getting started guide on the github repo as well as the documentation on the Elastic website . 查阅github仓库上入门指南以及Elastic网站上的文档 We are continually working to improve documentation and PRs are more than welcome for areas where you feel we are lacking :) 我们一直在努力改善文档,在您认为我们缺乏的领域,PR受到了欢迎:)

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

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