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

标签 c# json elasticsearch nest

我已经成功建立了与ES的连接,然后编写了json查询。现在,我想通过Serialize方法发送该查询。
Serialize方法需要两个参数:
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使用“mapping.json”文件在创建索引时初始化映射。这是该文件的link
正如“@Russ Cam”建议的那样,我使用以下代码创建了自己的类(class)内容(就像他对“questions”索引和“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文件。
现在,当我(就像您向我展示的那样)执行以下代码:
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上搜索“服务”查询或使用Google Chrome的Sense扩展名对localhost:9200进行API调用时,我得到了2个文档。 (我正在寻找的文件)
简而言之,我想要做的就是:
  • 获取所有文档(无条件)
  • 获取一个时间范围内并基于关键字的所有文档。例如“service”

  • 我究竟做错了什么?如果需要,我可以提供更多信息。
    谢谢大家的详细回答。

    最佳答案

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

    关于c# - 如何在C#中使用NEST客户端的ElasticClient类中的Serialize方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41122656/

    相关文章:

    c# - 从 SQL Server 2008 到 C# 的地理函数(多边形/ map 区域内部或外部的纬度和经度点)

    java - java中如何对JSON数组进行排序

    elasticsearch - 在 Elasticsearch 查询中使用 distinct 和 where 子句

    ruby - 在Travis上将Elasticsearch版本指定为2.3.1

    c# - 将列表类型转换为 IEnumerable 接口(interface)类型

    c# - 使用 C# 代码在 MS SQL 数据库中存储 key 时出现问题

    c# - OneDrive SDK & 后台任务 WP8.1

    php - 脚本在添加数据类型 : 'json' 时停止返回值

    javascript - 如何在 Javascript 中将 JSON 对象解析为指定的类型?

    elasticsearch - 获得满足条件的集合(不同值的列表)