1
votes

I have been pulling my hair out trying to configure and partial search ElasticSearch indexed data using Nest library version 5.3.1 (same version applies to its one of its dependencies; Elasticsearch.Net).

As per suggestions found online I used data attributes to specify analyzer type on some of the indexed properties as shown below:

public class Article
    {
        public int Id { get; set; }
        [Completion(Analyzer = "standard", PreservePositionIncrements = true, PreserveSeparators = true)]
        public string Title { get; set; }
        public string Url { get; set; }
    }

I have at least one record in the search index for type "Article" having title starting with "The greatest ....". Whenever I perform a partial search for a keyword "greatest" using code below, it works just fine returning matching search results.

MultiTermQueryRewrite multiqueryRewrite = null;
var searchQuery = Query<Article>.Wildcard(f => f.Title, "*greatest*", rewrite: multiqueryRewrite);

var client = ElasticsearchClient.GetClient<Article>();
return client.Search<Article>(s => s.Query(searchQuery));

But... if I try searching for "the greatest" keywords with any variation listed below, I don't get any results back.

var searchQuery = Query<Article>.Wildcard(f => f.Title, "*the greatest*", rewrite: multiqueryRewrite);

or

var searchQuery = Query<Article>.Wildcard(f => f.Title, "*the*greatest*", rewrite: multiqueryRewrite);

or even

var searchQuery = Query<Article>.Wildcard(f => f.Title, "*the?greatest*", rewrite: multiqueryRewrite);

I am new to the ElasticSearch product, so any help would be greatly appreciated.

Thanks in advance for your help.

2

2 Answers

2
votes

As per documentation

Wild card Matches documents that have fields matching a wildcard expression (not analyzed).

Since title field is Analyzed, it gets tokenized before getting indexed. Some text say The Greatest will get tokenized and then converted into lower case (Behaviour Of Standard Analyzer). So it will be stored in reverse index as two tokens the and greatest.

When you search for *greatest*. It is searched as there is a token corresponding to that.

But when you search for * the greatest * , it is not found as there is no token which contains this text.

You can use Query String

 var searchQuery = Query<Article>.QueryString(c => c
.Query("*the greatest*")
.DefaultField(p=>p.Title))

Hope this helps!!

0
votes

The standard analyzer applied to the Title field produces lower case terms of your "The Greatest" Title in the following format [the, greatest]. You could consider using the Keyword Analyzer but please note you will have to deal with word casing.