如何在c#中使用NEST客户端的ElasticClient类中的Serialize方法?

时间:2016-12-13 13:44:12

标签: c# json elasticsearch nest

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

1。对象和  2.流writeableStream

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

Stream wstream;

使用它来使用以下代码初始化我的json2变量:

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

我在wstream变量上遇到以下错误:

Use of unassigned local variable 'wstream'.

我错过了什么吗?这是我创建错误的wstream变量的方式吗?谢谢。

/ * \\\ 编辑: ///// * / 现在还有另一个问题,我使用Searchblox索引和搜索我的文件,它本身调用ES 2.x来完成这项工作。 Searchblox使用“ mapping.json ”文件在创建索引时初始化映射。这是该文件的link。 正如“@Russ Cam”建议的那样,我使用以下代码创建了自己的类内容(就像他使用“问题”索引和“问题”类一样):

 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 类(类型,商店等)中的这些字段来自上面附带的 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 中的文档。 相反,当我在Searchblox上搜索“service”查询或使用Google Chrome的Sense扩展程序对localhost:9200进行API调用时,我会收到2个文档。 (我正在寻找的文件)

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

  1. 获取所有文件(无标准)
  2. 获取时间范围内的所有文档并基于关键字...例如“service”
  3. 我做错了什么?如果需要,我可以提供更多信息.. 谢谢大家的详细解答。

3 个答案:

答案 0 :(得分:1)

这一行

  

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

     

Stream wstream;

不会创建na对象。它几乎宣布它。你需要new - 编辑它。

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

答案 1 :(得分:1)

它实际上比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);
}

答案 2 :(得分:0)

您刚刚声明wstream但从未为其分配过实际的流。取决于Serialize方法的工作原理,可能是:

  • 您需要创建一个流并将其传递给Serialize方法
  • 或者您需要使用out前缀
  • 传递stream参数
相关问题