Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

.net - How to make a Nest Elasticsearch case insensitive

I'm using the Nest.ElasticSearch library for .Net

And i have a model that i use for the index, which has a field i want to sort alphabetically (A-Z)

    [Text(Fielddata = true, Analyzer = "keyword")]
    public string FieldToSort { get; set; }

Then i have an endpoint i call which will make use of Nest.ElasticClient

        if (sortProperty != null)
        {
            SortOrder sortOrder = sortValue != null && sortValue.ToLower() == "asc" ? SortOrder.Ascending : SortOrder.Descending;

            switch (sortProperty.ToLower())
            {
                case "fieldToSort":
                    sortDescriptor.Field(s => s.FieldToSort , sortOrder);
                    break;
            }
        }
        var response = _client.Search<FieldModel>
           (
               s => s.Index("indexName")
               .Query(q => q
                   .Bool(b => b
                       .Filter(filterDescriptor)
                       .Must(sh => string.IsNullOrWhiteSpace(query) ? sh.MatchAll() : sh
                           .MultiMatch(mm => mm
                               .Fields(f => f
                                   .Field(c => c.something, 1)
                                   .Field(c => c.somehingelse, 3)
                               .Query(query)
                               .Operator(Operator.And)
                           )
                       )
                   )
               )
               .TrackTotalHits(true)
               .MinScore(Config.ElasticSearch.MinScore)
               .Sort(so => sortDescriptor)
               .Skip(offset)
               .Take(pageSize)
           );

The problem is that when i get the list it seems to order A-Za-z, what if i want this to be case insensitive, so it doesn't take in account if it's Capital or lowercase?

question from:https://stackoverflow.com/questions/65599938/how-to-make-a-nest-elasticsearch-case-insensitive

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be doing this at index time for best performance. You can define a lowercased normalizer in your index settings:

.Normalizers(norm => norm.Custom("lowercase", cn => cn.Filters("lowercase"));

Then define this in your mapping. If you are using attribute based mapping it would look like this:

[Keyword(Index = true, Normalizer = "lowercase")]

Syntax might differ slightly depending on your ES and NEST version, but this would be the general approach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...