Case-insensitive fields in Elasticsearch

I am using NEST with ElasticSearch and I am trying to perform a search by allowing users to enter search phrases in the search field. Everything works fine no matter when the user enters the search phrase they need to make sure that the field name matches the field name in Elastic search.

For example, one of my fields is called bookTitle. If they search below then it works

bookTitle: "A Tale of Two Cities"

If they search, as an example below, it does not work

booktitle: "The Tale of Two Cities" BookTitle: "The Tale of Two Cities"

The code I'm using to search is below. Anyone have any ideas on how I can fix this. I was hoping there was an ElasticSearch / NEST parameter that allows me to do this and not do something ugly with the search text, for example, find “BookTitle” and replace “bookTitle”.

   public List<ElasticSearchRecord> Search(string searchterm) {

        var results = _client.Search<ElasticSearchRecord>(s => s
                        .Query(q => q
                            .QueryString(qs => qs
                                .DefaultField("content")
                                .Query(searchterm)
                            )
                        ));


        return results.Documents.ToList();
    }

Any help is greatly appreciated.

+4
source share
3 answers

The way you want it is not possible with Elasticsearch. You control the display, you define the field names, it is the one who controls the requests.

In accordance with this decision, you need to ensure that your users enter in the search field, Elasticsearch out of the box will not help you with the names of a smaller field or something like that.

, , , .

, . - :

, _all. QueryString , ES query_string . , ES _all, _all, , .

, , , - , , script, , , Groovy. , Elasticsearch.

, , .

+3

# , . , . , .

" " .

+1

, .ToLower() , .

:

public class LowercaseNamingStrategy : Newtonsoft.Json.Serialization.NamingStrategy
{
    protected override string ResolvePropertyName(string name)
    {
        return name.ToLower();
    }
}

:

    public class ElasticSerializer : JsonNetSerializer
    {
        public ElasticSerializer(IConnectionSettingsValues settings)
            : base(settings)
        {
            this.ContractResolver.NamingStrategy = new LowercaseNamingStrategy();
        }
    }

NEST:

        var pool = new StaticConnectionPool([your nodes]);
        var settings = new ConnectionSettings(pool, s => new ElasticSerializer(s));
        var client = new ElasticClient(settings);

This will keep your fields as lowercase. Then, when you request, just force all the user-provided fields to be lowercase, and everything should match.

If you are not starting from scratch, you will have to re-populate your details so that the naming strategy is unified.

0
source

Source: https://habr.com/ru/post/1610065/


All Articles