Adrian
Adrian

Reputation: 5681

Changes b/w ElasticSearch 1.x and 2.x

Does documentation exist on how to change code written in NEST 1.x to 2.x?

I've looked at these sites and they're incomplete:
https://github.com/elastic/elasticsearch-net/blob/master/docs/2.0-breaking-changes/nest-breaking-changes.md

https://github.com/elastic/elasticsearch-net

https://www.elastic.co/blog/ga-release-of-nest-2-0-our-dot-net-client-for-elasticsearch

For example I'd like to know how to replace the following:

1)

given ISearchResponse<T>  searchResults = ... 

How to do:

searchResults.ConnectionStatus 
searchResults.RequestInformation.Request

2)

client.Get<T>(s => s.Id(id));

3)
Given QueryContainer query

new SearchDescriptor<T>()
            .From(from)     
            .Size(pageSize)   
            .Query(query); //this dosen't work anymore

4) MatchQuery doesn't accept fuziness as double and type parameters as string as it used to

5) QueryDescriptor seems gone gasp

6) client.Update is busted

 var result = client.Update<CustomerProfile>(request => request
                .Id(customer.CustomerId)
                .Doc(customer)
                .Refresh()
                );

7) client.Get is busted in a similar way to client.Update

8) In Mappings the following setup doesn't work anymore

CreateIndexDescriptor cid = ...
cid.NumberOfReplicas(numReplicas)
     .NumberOfShards(numShards)
     .Settings(s => s
         .Add("merge.policy.merge_factor", "10")
         .Add("search.slowlog.threshold.fetch.warn", "1s")
     )
     .Analysis(a => a.TokenFilters etc etc

EDIT

9) Date Ranges:
startDate and endDate are DateTime type

var qd = new QueryContainerDescriptor<EsActivity>();
        QueryContainer qc = qd.Range(r =>
                    r.Field("esactivity.timestamp")
                    .GreaterThanOrEquals(DateMath.Anchored(startDate))
                    .LessThanOrEquals(DateMath.Anchored(endDate))
                );

.GreaterThanOrEquals expects a double parameter but on the documentation page it takes DateMath.Anchored(startDate)

10) Highlighting:

highlightFields: List<string> 
Action<HighlightFieldDescriptor<T>> [] tmp = highlightFields.Select(field =>
                          new Action<HighlightFieldDescriptor<T>>(
                              highlighter => highlighter.Field(field)
                          )
                      ).ToArray();

sd:SearchDescriptor<..>..
sd.Highlight(h => h
                      .PreTags(preTag)
                      .PostTags(postTag)
                      .OnFields(tmp)
                   );

I see I can replace OnFields(tmp) with .Fields(f=>f.OnAll()) but I'd still like to specify the fields myself in some way.

And how come there is a HighlightQuery option available since we already apply highlighting on a query object.. now there are 2 query calls.

I've converted the highlighting above to

            var tmp = highlightFields.Select(field =>
                          Tuple.Create<Field, IHighlightField>(
                              Field.Create(field),
                              new HighlightField()
                          )
                       ).ToDictionary(x => x.Item1, x => x.Item2);

            sd.Highlight(h => new Highlight
                {
                    PreTags = new[] { preTag },
                    PostTags = new[] { postTag },
                    Fields = tmp
                }
            );

Upvotes: 0

Views: 114

Answers (1)

Russ Cam
Russ Cam

Reputation: 125518

1) searchResults.ApiCall replaces searchResults .ConnectionStatus.

You can get the request bytes with searchResults.ApiCall.RequestBodyInBytes and you will also need to set .DisableDirectStreaming() on ConnectionSettings in order to capture the bytes as the request is written to the request stream directly by default.

2) Use client.Get<T>(id) - The first parameter is a DocumentPath<T> type.

3) To pass a QueryContainer to a Fluent API descriptor, just return it from the Func<QueryContainerDescriptor<T>, QueryContainer>

new SearchDescriptor<T>()
    .From(from)     
    .Size(pageSize)   
    .Query(_ => query); 

4) match query fuzziness as a double mapped to a formula to calculate edit distance in Elasticsearch 1.x. Since this was removed in Elasticsearch 2.x, it is also gone from NEST. You can set fuzziness edit distance with

client.Search<Document>(s => s
    .Query(q => q
        .Match(m => m
            .Query("this is my query")
            .Fuzziness(Fuzziness.EditDistance(3))
        )
    )
);

Not sure what you're referring to with type, but I think you're referring to document type? If that's the case, document type takes a Types type which string implicitly converts to

client.Search<Document>(s => s
    .Type("other-type")
    .MatchAll()
);

5) QueryDescriptor<T> was renamed to QueryContainerDescriptor<T> to better reflect the fact that it's a descriptor for building a QueryContainer

6) Update API works

// specifying id
client.Update<Document>("document-id", u => u
    .Doc(document)
    .Refresh()
);

Since the first parameter is a DocumentPath<T>, the document instance (if you have it) can be passed as the first parameter

client.Update<Document>(document, u => u
    .Doc(document)
    .Refresh()
);

where index, type and id will be inferred from the document instance

7) See above

8) Create index settings have been revised to reflect the level at which the settings appear in the REST API json call

client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfShards(2)
        .NumberOfReplicas(2)
        .SlowLog(sl => sl
            .Search(sls => sls
                .Fetch(slsf => slsf
                    .ThresholdWarn("1s")
                )
            )
        )
        .Analysis(a => a) // etc...
    )
);

You can also use strings for settings if you prefer, although the fluent API will ensure the correct setting values are sent e.g. "search.slowlog.threshold.fetch.warn" is now "index.search.slowlog.threshold.fetch.warn"

client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfShards(2)
        .NumberOfReplicas(2)
        .Setting("index.search.slowlog.threshold.fetch.warn", "1s")
        .Analysis(a => a) // etc...
    )
);

merge.policy.merge_factor is removed in Elasticsearch 2.0

Upvotes: 1

Related Questions