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

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

本文介绍了将对象序列化为 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 的弹性搜索中发送查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将对象序列化为 JSON,然后使用该对象在使用 NEST 的弹性搜索中发送查询

基础教程推荐