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

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

问题描述

对于使用NEST来查询,我感到有点困惑和沮丧,因为它似乎很受打击和怀念。在使用标准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);

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



如果有人知道如何使用JSON对象简单地查询,那将是非常棒的,提前欢呼。

解决方案

NEST和Elasticsearch.Net是NEST在封面下使用的低级客户端,对您希望查询的灵活性。 NEST你有几种不同的方式:



NEST - 高级客户端



1。流畅的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。



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表达式不是你的东西,那么你可以随时使用特定的搜索类型定义你的搜索。 / p>

Elasticsearch.Net - 低级客户端



如果您想使用匿名类型进行查询(按照你的问题),json字符串或一个字节表示的查询,那么你可以使用低级别的客户端Elasticsearch.Net来实现这一点。低级客户端通过 .LowLevel 属性



匿名显示在高级客户端上。类型

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

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

使用高级别客户端上的低级客户端意味着您仍然可以利用使用Json。 NET反序列化搜索结果;在这个例子中,搜索响应可以通过 searchResult.Body



2.Json string

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

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

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

查看 github repo 入门指南以及 Elastic网站上的文档。我们不断努力改进文件,对于您感到缺乏的领域,公共关系更受欢迎:)


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

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

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

解决方案

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:

NEST - High level client

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.

2.Object Initializer Syntax

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.

Elasticsearch.Net - Low level client

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.Anonymous types

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; in this example, the search response can be accessed through searchResult.Body

2.Json string

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

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

3.Byte array

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. 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天全站免登陆