将对象序列化为 JSON,然后使用它在弹性搜索中使用 NEST 发送查询 [英] Serialising an object to JSON, and then using that to send a query in elastic search using NEST

查看:33
本文介绍了将对象序列化为 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.Object Initializer 语法

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 存储库的入门指南 以及 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 发送查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆