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

**URL:** <https://discuss.elastic.co/t/whats-the-best-way-to-implement-contains-search-queries-in-combination-with-multimatch/251909>\
**Category:** Elasticsearch\
**Created:** [October 13, 2020, 1:30pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-implement-contains-search-queries-in-combination-with-multimatch/251909 "2020-10-13T13:30:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanglingNode](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DanglingNode](https://discuss.elastic.co/u/DanglingNode)\
**Post date:** [October 13, 2020, 1:30pm UTC](https://discuss.elastic.co/t/whats-the-best-way-to-implement-contains-search-queries-in-combination-with-multimatch/251909/1 "2020-10-13T13:30:33Z")

</div>

Hi,

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:

```auto
var result = await client.SearchAsync<PersonRecord>(s => s
                .Query(q => q
                    .MultiMatch(m => m
                        .Query("jo")
                        .Type(TextQueryType.BoolPrefix)
                    ));

```

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?

---

<div class="post-metadata">

**Author:** ![DanglingNode](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DanglingNode](https://discuss.elastic.co/u/DanglingNode)\
**Post date:** [October 14, 2020, 7:26am UTC](https://discuss.elastic.co/t/whats-the-best-way-to-implement-contains-search-queries-in-combination-with-multimatch/251909/2 "2020-10-14T07:26:48Z")

</div>

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

```auto
var result = await client.SearchAsync<PersonRecord>(s => s
                .Query(q => q
                    .QueryString(m => m
                        .Query("*jo*")
                        .AnalyzeWildcard()
                    ));

```

However, I read in the documentation ([https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html](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?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 14, 2020, 7:59am UTC](https://discuss.elastic.co/t/whats-the-best-way-to-implement-contains-search-queries-in-combination-with-multimatch/251909/3 "2020-10-14T07:59:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 11, 2020, 7:59am UTC](https://discuss.elastic.co/t/whats-the-best-way-to-implement-contains-search-queries-in-combination-with-multimatch/251909/4 "2020-11-11T07:59:56Z")

</div>

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