是否有可能从普通弹性搜索转移到官方 .net 客户端,NEST

Is it possible to move from plain elastic search to official .net client, NEST

我目前使用 PlainElastic 作为我的 .NET ElasticSearch 客户端。 我正在考虑迁移到官方 .NET 客户端 NEST 有问题吗?

  1. NEST 支持 SSL 吗?
  2. 我们可以做常见的查询吗?
  3. 常见的聚合怎么做? 搬家需要很长时间吗

1.NEST 支持 SSL/TLS。只需在 ConnectionSettings

中指定您的 url
var pool = new SingleNodeConnectionPool(new Uri("https://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);

var client = new ElasticClient(connectionSettings);

2.NEST 支持 Elasticsearch 公开的 所有 API,包括整个查询 DSL。一个例子

client.Search<Conference>(s => s
    .Query(q => q
        .Bool(b => b
            .Should(sh => sh
                .Match(m => m
                    .Field(f => f.Location)
                      .Query("Sydney")
                ), sh => sh
                .Match(m => m
                      .Field(f => f.Location)
                      .Query("Spektrum")
                      .Boost(2)
                )
            )
            .Filter(fi => fi
                .Term(t => t
                    .Field(f => f.Name.Suffix("raw"))
                    .Value("NDC")
                )
            )
        )
    )
);

NEST 具有 inference and operator overloading 等功能,可使构建查询变得更加容易。这是上一个带有运算符重载的查询

client.Search<Conference>(s => s
    .Query(q => (q
        .Match(m => m
            .Field(f => f.Location)
              .Query("Sydney")
        ) || q
        .Match(m => m
              .Field(f => f.Location)
              .Query("Spektrum")
              .Boost(2)
        )) && +q
        .Term(t => t
            .Field(f => f.Name.Suffix("raw"))
            .Value("NDC")
        )
    )
);

3.NEST 支持 all 聚合。这是一个针对 Whosebug 数据集的查询的相当复杂的示例,查看标记为 "dnx" 或“.net-core”并自 2015 年 6 月 29 日创建的问题。

在这些问题上,对问题编号进行了 terms aggregation is performed on the tags field, but only looking at the "dnx" and ".net-core" tags. On each term bucket, a date histogram aggregation is performed to bucket questions into 1 week intervals, with a count performed on the number of questions in each bucket and a Holt-Winters moving average aggregation

var response = client.Search<Question>(s => s
    .Size(0)
    .Query(q => +q
        .Terms(m => m
            .Field(f => f.Tags)
            .Terms("dnx", ".net-core")
        ) && +q
        .DateRange(r => r
            .Field(f => f.CreationDate)
            .GreaterThan(new DateTime(2015, 06, 29))
        )
    )
    .Aggregations(a => a
        .Terms("tags", t => t
            .Field(f => f.Tags)
            .Include(@"dnx|\.net\-core")
            .Aggregations(sub => sub
                .DateHistogram("weekly_questions", dh => dh
                    .Field(f => f.CreationDate)
                    .Interval("1w")
                    .Aggregations(sa => sa
                        .ValueCount("count_questions", vc => vc
                            .Field(f => f.Id)
                        )
                        .MovingAverage("questions", ma => ma
                            .BucketsPath("count_questions.value")    
                            .Window(12)
                            .Model(mo => mo
                                .HoltWinters(hw => hw
                                    .Type(HoltWintersType.Additive)                                     
                                )
                            )
                        )
                    )
                )
            )
        )
    )
);