Grigoris Loukidis
Grigoris Loukidis

Reputation: 403

Elastic-QueryString doesn't work correctly

I am new to elastic. I am trying to search with the following code, but when I want to get back the word "house", if I type "hou" I can find it , but if I type "ouse" it doesn't work.Also, the analyzer doesn't work.Is this the right place to add it?

var response = client.Search<Homes>(n => n
    .Index(index)
    .Type(type)
    .Size(searchSize)
    .From(0)
    //.Analyzer(analyzername)
    .TrackScores(true)
   .Query(q=>q.QueryString(qs=>qs.Query("*"+searchWord+"*")
                                 .Fields(f=>f.Field(fieldsForSearchList[0]))
                                 .Analyzer(analyzername)))
);

Upvotes: 1

Views: 135

Answers (2)

Grigoris Loukidis
Grigoris Loukidis

Reputation: 403

On index time, it needs Ngram filter, not only EdgeNGram because we need to create sub words not only from the left edge but from different positions of the word.For example the word house will be cutted to [ho,ou,us,se,hou,ous,use,hous,ouse,house]. Allthough it gets much more time on indexing because it creates many more words than the EdgeNGram. With EdgeNGram we get [ho,hou,hous,house].

Upvotes: 0

Russ Cam
Russ Cam

Reputation: 125538

We don't know if the analyzer works or not as you haven't provided the name of it nor shown what it the analyzer is composed of. Without this information, it's impossible to answer your question :)

There's a couple of points that may help you move in the right direction

  1. You're using wildcards in the query string query. By default, these are not enabled as they can be expensive in terms of performance (there are usually better ways to index your data such as using custom analyzers). You can have wildcards evaulated by using .AnalyzeWildcard() inside the body of .QueryString(), but I expect you are already analyzing this field at index time with an analyzer that incorporates an edgengram token filter, because you get results back for the partial term "hou".
  2. You can also apply an edgengram token filter to produce ngrams from the end of terms (as opposed to the start), by building a custom analyzer that applies a reverse token filter, then an edgengram filter, followed by another reverse token filter. You probably want to apply this analyzer and index into a separate multi-field sub field (maybe call it "reverse"?) then when searching, search across both the field and sub field, applying a boost to the "forward" field (since people tend to prefix search, but this may be different in your domain).

Upvotes: 1

Related Questions