如何在C#中的Nest Client中使用弹性class类中的序列化方法



我已经创建了与ES的成功连接,然后写了我的JSON查询。现在,我想通过序列化方法发送该查询。序列化方法需要两个参数:

1。对象和2.流writableStream

我的问题是,第二个问题。当我使用以下代码行创建流时:

Stream wstream;

并使用以下代码来初始化我的JSON2变量:

var json2 = highLevelclient.Serializer.Serialize(query, wstream).Utf8String();

我在WSTREAM变量上获取以下错误:

Use of unassigned local variable 'wstream'.

我想念什么吗?是我创建错误的WSTREAM变量的方式吗?谢谢。


/* \ 编辑:///////////现在还有另一个问题,我使用SearchBlox索引并搜索文件,该文件本身称为ES 2.X来完成这项工作。searchblox使用a'映射。文件以在创建索引时初始化映射。这是该文件的链接。AS" @Russ Cam"建议,我使用以下代码创建了自己的班级内容(就像他对" 问题" "索引"one_answers" 问题" 'class "类):

 public class Content
    {
        public string type { get; set; }
        public Fields fields { get; set; }
    }
    public class Fields
    {
        public Content1 content { get; set; }
        public Autocomplete autocomplete { get; set; }
    }
    public class Content1
    {
        public string type { get; set; }
        public string store { get; set; }
        public string index { get; set; }
        public string analyzer { get; set; }
        public string include_in_all { get; set; }
        public string boost { get; set; }
    } //got this with paste special->json class

这些字段来自 content class( type,store etc。)来自上面附加的 mapping.json 文件。现在,当我(就像您向我展示)执行以下代码时:

var searchResponse = highLevelclient.Search<Content>(s => s.Query(q => q
         .Match(m => m.Field(f => f.fields.content)
        .Query("service")

我在 searchResponse 变量上得到的响应是:

Valid NEST response built from a successful low level call on POST: /idx014/content/_search
Audit trail of this API call:
 -HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.7180404
Request:
{"query":{"match":{"fields.content":{"query":"service"}}}}
Response:
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

searchResponse.documents 中的文档。矛盾的是,当我在搜索blox上搜索" 'service"服务" 查询或对Localhost进行API调用:9200借助Google Chrome的含义扩展,我会得到2个文档。(我正在寻找的文档)

简而言之,我想要的就是:

  1. 获取所有文件(无标准)
  2. 将所有文档在一个时间范围内并基于关键字。

我在做什么错?如果需要,我可以提供更多信息。谢谢大家的详细答案。

实际上比nest要简单得多:)客户端将为您序列化您的请求并将其发送到Elasticsearch,您无需采取步骤即可自己序列化,然后将它们传递给客户发送到Elasticsearch。

以搜索请求为例。给定以下poco

public class Question
{
    public string Body { get; set; }
}

我们可以搜索包含短语的问题"这永远不应该发生"

var settings = new ConnectionSettings(new Uri("http://localhost:9200"))
    .InferMappingFor<Question>(m => m
        .IndexName("questions")
    );

var client = new ElasticClient(settings);
var searchResponse = client.Search<Question>(s => s
    .Query(q => q
        .MatchPhrase(m => m
            .Field(f => f.Body)
            .Query("this should never happen")
        )
    )
);
// do something with the response
foreach (var question in searchResponse.Documents)
{
    Console.WriteLine(question.Body);
}

此行

我的问题是,第二个问题。当我使用以下代码行创建流时:

流wstream;

不创建NA对象。它几乎宣布了。您需要new -ED。

Stream wstream = new MemoryStream(); //doesn't have to be MemoryStream here - check what does Serialize expects

只记得以后关闭它或使用using语句。

using(Stream stream = new MemoryStream())
{
  
   //do operations on wstream
  
} //closes automatically here

您刚刚声明为wstream,但从未分配实际流。根据Serialize方法的工作方式,可能是:

  • 您需要创建流并将其传递到Serialize方法
  • 或您需要使用out前缀传递流参数

最新更新