本文介绍了如何使用C#中的NEST客户端的ElasticClient类中的Serialize方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a successful connection to ES, and then written my json query. Now, I would like to send that query via the Serialize method. The Serialize method requires two parameters:

1。对象和
2.流writableStream

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

My question is, with the second one. When I create a stream with the following code line:

Stream wstream;

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

And use it to initialize my json2 variable with the following code:

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

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

I get the following error on the wstream variable:

Use of unassigned local variable 'wstream'.

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

Am I missing something? Is it the way I create the wstream variable that is wrong? thank you.

/ * \\\ 编辑: //// / * /
现在还有一个问题,我使用Searchblox来索引和搜索我的文件,这个文件本身叫做ES 2.x来完成这个工作。 Searchblox使用 mapping.json文件在创建索引时初始化映射。以下是该文件的。
作为@Russ Cam建议,我使用以下代码创建了自己的课程内容(就像他用问题索引和问题 class):

/* \\\ edit: ///// */ There is another issue now, I use Searchblox to index and search my files, which itself calls ES 2.x to do the job. Searchblox uses a "mapping.json" file to initialize a mapping upon the creation of an index. Here's the link to that file. As "@Russ Cam" suggested, I created my own class content with the following code (just like he did with the "questions" index and "Question" 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

这些字段来自内容类(类型,存储等)来自上面附带的 mapping.json 文件。
现在,当我(就像你显示我)执行以下代码:

These fields from the content class (type,store etc.) come from the mapping.json file attached above.Now, when I (just like you showed me) execute the following code:

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

所有我作为响应在 searchResponse 变量是:

All I get as a response on the searchResponse variable is:

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":[]}}

no 文档在 searchResponse.Documents
矛盾的是,当我搜索对于服务查询Searchblox或使用Google Chrome的Sense扩展程序对localhost:9200进行API调用,我得到2个文档。 (我正在寻找的文件)

And no documents in searchResponse.Documents.Contradictorily, when I search for the "service" query on Searchblox or make an API call to localhost:9200 with the Sense extension of Google Chrome, I get 2 documents. (the documents that I was looking for)

简而言之,我想要的是能够:

In brief, all I want is to be able to :


  1. 获取所有文档(无条件)

  2. 在一个时间范围内获取所有文档,并基于关键字,如service

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

What am I doing wrong? I can provide with more information if needed..Thank you all for your detailed answers.

推荐答案

比NEST更简单:)客户端将序列化您的请求并将其发送到Elasticsearch,您不需要采取步骤自己序列化,然后将其传递给客户端以发送到Elasticsearch。

It's actually much simpler than this with NEST :) The client will serialize your requests for you and send them to Elasticsearch, you don't need to take the step to serialize them yourself and then pass them to the client to send to Elasticsearch.

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

Take a search request for example. Given the following POCO

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

我们可以搜索包含短语em的问题

We can search for questions that contain the phrase "this should never happen" in the body with

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

这篇关于如何使用C#中的NEST客户端的ElasticClient类中的Serialize方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:44