Mystic Force
Mystic Force

Reputation: 7

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

I am currently using PlainElastic as my .NET ElasticSearch client. I am consider moving to the official .NET client,NEST Questions?

  1. Does NEST support SSL?
  2. can we do common queries?
  3. How to do common aggregations? does it take long time to move to nest.

Upvotes: 1

Views: 255

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

1.NEST supports SSL/TLS. Just specify your url in ConnectionSettings

var pool = new SingleNodeConnectionPool(new Uri("https://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);

var client = new ElasticClient(connectionSettings);

2.NEST supports all of the APIs that Elasticsearch exposes, including the entire query DSL. An example

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 has features such as inference and operator overloading to make constructing queries even easier. Here's the previous query with 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 supports all aggregations. Here's a fairly involved example of a query against the stackoverflow data set, looking at questions tagged "dnx" or ".net-core" and created since 29 June 2015.

On these questions, a 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 performed on the question number.

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

Upvotes: 1

Related Questions