I have an Elasticsearch index with a title field, and I am trying to search for partial matches. For example, the title is “hp 250 g10”. When I search with “hp 250” or the exact key “hp 250 g10”, the results are as expected. However, when I search “hp g10”, no results are returned. Here’s the relevant part of my code:
Must(
!String.IsNullOrEmpty(purchaseSearchingModel.Key) ? b => b.Bool(x => x.Should(
sh => sh.Match(m => m.Field(f => f.Title).Fuzziness(new Fuzziness(fuzziness)).Query(purchaseSearchingModel.Key)),
sh => sh.Wildcard(w => w.Field(f => f.Title).Value($"*{purchaseSearchingModel.Key}*"))
))
);
How can I modify my query to support partial matches like “hp g10”? Would using an N-gram analyzer help? Are there better approaches for this in .NET with Elasticsearch.Net?
-
Match Query: I used a Match query with a fuzziness setting to allow for minor differences in the search term. This works well for closely matching terms but doesn’t support skipping words in the middle of the query (e.g., “hp g10” missing “250”).
-
Wildcard Query: I added a Wildcard query to try and match parts of the title by surrounding the search key with wildcards (e.g., hp g10). While this works for partial matches, it doesn’t match terms when words in the middle are skipped unless explicitly covered by the wildcard.
Afet Agayeva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.