What's the best way to implement 'contains' search queries in combination with MultiMatch?


I have documents with most fields indexed as 'Keywords'. I currently want to perform a global search, over all indexed fields, which I've implemented as such:

var result = await client.SearchAsync<PersonRecord>(s => s
                .Query(q => q
                    .MultiMatch(m => m

This gives me all documents with any value starting with 'jo'.

What's the best way to convert this prefix query in a 'contains' search? So that all documents are returned with any value matching with '*jo*'.

I already found out about Wildcard queries, but these are not compatible with the MultiMatch query.

An other solution would be indexing these fields using a nGram tokenizer, but doing this for all fields would increase the needed storage, since more tokens will be added.

Any tips or pointers to direct me towards a more ideal implementation?

I managed to implement the 'contains' search by changing the above code to:

var result = await client.SearchAsync<PersonRecord>(s => s
                .Query(q => q
                    .QueryString(m => m

However, I read in the documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html) that QueryString is not recommended to use for search boxes (which is actually my use case) because the query returns an error upon an invalid syntax.

One way to solve this is to perform query string formatting in advance and limit the possibilities (since only the wild cards are needed). Or would another approach be recommended?

Glad you found a solution!

You may also want to take a look at the boolean query allowing you to specify more than one query.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.