Boost PDF files if name contains query



I have a request from a client asking to boost PDF files if the name of the PDF contains the query, but I haven´t found a good way to accomplish that.

Basically what I want to do is this:

var searchResult = SearchClient.Instance.UnifiedSearchFor(searchString, findLanguage)
                                          .BoostMatching(p => p.MatchTypeHierarchy(typeof(PdfFile)) & ((PdfFile)p).Name.Contains(searchString), 4)
                                          .Skip((page - 1) * hitsPerPage)

Of course .BoostMatching(p => p.MatchTypeHierarchy(typeof(PdfFile)) & ((PdfFile)p).Name.Contains(searchString), 4) doesn´t work because that won´t be compiled to a filter, but it´s the essence of what I want. I can´t use AnyWordBeginsWith() since the requirement isn´t that the word in the name should start with the query. The MatchFuzzy() won´t work either because as I understand it, it will match on like similiar words which isn´t the requirement either.

Have I missed something or is this just not possible? Seems quite simple so I thought that would be possible. Hopefully I´ve just missed something :)


Feb 15, 2018 20:04

Try changing

.BoostMatching(p => p.MatchTypeHierarchy(typeof(PdfFile)) & ((PdfFile)p).Name.Contains(searchString), 4)

to this

.BoostMatching(p => p.MatchTypeHierarchy(typeof(PdfFile)) & ((PdfFile)p).Name.Match(searchString), 4)
Feb 16, 2018 12:15

But that would only match PDF files where property Name matches the search query exactly, right? At least that was how I was taught Match works. I want to have PDF files where Name fully or partly contains the search query so if the query is "brev" then I want to boost PDF files with name "Skicka brev" etc.

Feb 16, 2018 12:45

Ah, you are right. Then I guess you have to add some kind of wildcard search extention.

Something similar to the FuzzyFilter method specified here:

I did a quick test: Document with Name "Sending letters" got match when using FuzzyFilter with "letters".

Feb 16, 2018 13:27

Hmm, do you mind showing how you use that filter? I saw that post but didn´t really think it would work together with the boosting, since it's not only that I want to use a fuzzy search in general, but actually boost those items.

Feb 16, 2018 14:03

I just did a quick test to verify that filter actually would match. I altered the extension to this:

public static ITypeSearch<T> FuzzyFilter<T>(
            this ITypeSearch<T> search,
            Expression<Func<T, string>> fieldSelector,
            string almost,
            double? minSimilarity = null)
            var fieldName = search.Client.Conventions

            var wildcardQuery = new FuzzyQuery(fieldName, almost.ToLowerInvariant())
                MinSimilarity = minSimilarity

            //Add it to the search request body
            return new Search<T, WildcardQuery>(search, context =>
                if (context.RequestBody.Query != null)
                    var boolQuery = new BoolQuery();
                    boolQuery.MinimumNumberShouldMatch = 1;
                    context.RequestBody.Query = boolQuery;
                    context.RequestBody.Query = wildcardQuery;

And then used it like this:

                .FuzzyFilter(d => d.Name, query, 1)
Feb 16, 2018 14:10

That works alright, by itself as a separate filter to match the Name property against the query (which of course could be useful), but it´s not really working with boosting the way I want with PDF files specifically. However, I´m not sure as to where the requirement came from and the background so I´m going to dig a bit deeper into that to see if this could be a solution.

Thanks a bunch for this!

Feb 16, 2018 18:12
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.