ElasticSearch NEST alternative for C# .Contains()

7.6k views Asked by At

Having moved from Redis to ElasticSearch for my personal project, I need some help from the masters. The basic requirement is as below:

  1. Indexes contain POCO of type Album which has fields such as Artist, Title, Year
  2. When user enters a search-term, for ex "2", I should get the Albums in which the above fields contain the search-term
  3. It should match Album Title like "2 States", Artists such as "2 Pac" and Year in "2014,1992..etc"

I have got the code working just as expected, but I'm using wildchars which I believe will impact the performance. The code is below:

var results = Client.Search<Album>(body =>
    body.Query(query =>
        query.QueryString(qs =>
            qs.OnFieldsWithBoost(d => d
                .Add(f => f.AlbumName.ToLowerInvariant(), 5.0)
                .Add(f => f.AlbumTitle.ToLowerInvariant(), 2.0)
                )
                .Query(String.Format("{0}*", searchText))
            )
        )
        .Take(100)
    );

Any suggestions to improve the query?

1

There are 1 answers

0
vanderkorn On BEST ANSWER

You need to create an index using the Ngram tokenizer. And then use the search string without wildcard.

Example: How to search for a part of a word with ElasticSearch

Ngram tokenizer: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html