# Elasticsearch.Net and multisearch

**URL:** https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602
**Category:** Elasticsearch
**Created:** [August 10, 2017, 11:55am UTC](https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602 "2017-08-10T11:55:47Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Karolinebryn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karolinebryn/32/17095_2.png) [@Karolinebryn](https://discuss.elastic.co/u/Karolinebryn)
#### Post date: [August 10, 2017, 11:55am UTC](https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602/1 "2017-08-10T11:55:47Z")

</div>

How do you perform a multisearch with [Elasticsearch.Net](http://Elasticsearch.Net)? I cannot find any examples of this.

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [August 11, 2017, 6:04am UTC](https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602/2 "2017-08-11T06:04:48Z")

</div>

Here's an example using the fluent lambda API

```
client.MultiSearch(ms => ms
    .Search<Product>("products", s => s
        .Index(IndexName)
        .Explain(explain)
        .Query(q => q
           .Bool(b => b
                .Should(sh => sh
                    .MultiMatch(qs => qs
                        .Fields(d => d
                            .Field(Name + ".raw", NameBoost + 0.5)
                            .Field(Name, NameBoost)
                        )
                        .Type(TextQueryType.BestFields)
                        .Query(key)
                    )
                )
            )
        )
        .From(startfrom)
        .Size(size)
    )
    .Search<Category>("categories", s => s
        .Index(IndexName)
        .Explain(explain)
        .Query(q => q
            .Bool(b => b
                .Should(sh => sh
                    .MultiMatch(m => m
                        .Fields(d => d
                            .Field(f => f.Name, NameBoost)
                            .Field(p => p.Name.Suffix("raw"), NameBoost + 0.5)
                        )
                        .Type(TextQueryType.BestFields)
                        .Query(key)
                    )
                )
            )
        )
        .From(startfrom)
        .Size(size)
    )
);

```

And the same with the Object Initializer syntax

```
var multiSearch = new MultiSearchRequest
{
    Operations = new Dictionary<string, ISearchRequest>
    {
        { "products", new SearchRequest<Product>(IndexName)
            {
                Explain = true,
                Query = new BoolQuery
                {
                    Should = new QueryContainer[] {
                        new MultiMatchQuery
                        {
                            Fields = 
                                ((Fields)Field.Create(Name + ".raw", NameBoost + 0.5))
                                .And(Name, NameBoost),
                            Type = TextQueryType.BestFields,
                            Query = key
                        }
                    }
                },
                From = startfrom,
                Size = size
            }
        },
        { "categories", new SearchRequest<Category>(IndexName)
            {
                Explain = true,
                Query = new BoolQuery
                {
                    Should = new QueryContainer[] {
                        new MultiMatchQuery
                        {
                            Fields =    
                                ((Fields)Infer.Field<Category>(f => f.Name, NameBoost))
                                .And<Category>(f => f.Name.Suffix("raw"), NameBoost + 0.5),
                            Type = TextQueryType.BestFields,
                            Query = key
                        }
                    }
                },
                From = startfrom,
                Size = size
            }
        },
    }
};

client.MultiSearch(multiSearch);

```

Take a look at the integration tests for another example: [https://github.com/elastic/elasticsearch-net/blob/5.x/src/Tests/Search/MultiSearch/MultiSearchApiTests.cs#L52-L81](https://github.com/elastic/elasticsearch-net/blob/5.x/src/Tests/Search/MultiSearch/MultiSearchApiTests.cs#L52-L81)

---

<div class="post-metadata">

### Author: ![Karolinebryn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karolinebryn/32/17095_2.png) [@Karolinebryn](https://discuss.elastic.co/u/Karolinebryn)
#### Post date: [August 11, 2017, 11:02am UTC](https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602/3 "2017-08-11T11:02:36Z")

</div>

But this is using Nest right? I do not want to use Nest.

I found that you can use ElasticsearchClient.Msearch();

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [August 12, 2017, 7:47am UTC](https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602/4 "2017-08-12T07:47:22Z")

</div>

Yes, this is using NEST, the high level client.

To use [Elasticsearch.Net](http://Elasticsearch.Net) low level client with multi search would be

```
var client = new ElasticLowLevelClient();

client.LowLevel.Msearch<dynamic>(new object[] {
	new { index = "products", type = "product"},
	new { 
		query = new { 
			@bool = new { 
				should = new object [] { 
					new { 
						multi_match = new { 
							type = "best_fields", 
							query = "product query", 
							fields = new [] { "name","name.raw" }
						}
					}
				}
			}
		}
	},
	new { index = "categories", type = "category"},
	new { 
		query = new { 
			@bool = new { 
				should = new object [] { 
					new { 
						multi_match = new { 
							type = "best_fields", 
							query = "category query", 
							fields = new [] { "name","name.raw" }
						}
					}
				}
			}
		}
	}
});

```

The `dynamic` generic type parameter on the method is the type into which the response will be deserialized into (which will be an internal `JsonObject` type when using `dynamic`).

The low level client by default supports `string`, `byte[]`, `Stream`, `object`, or your own type. The low level client contains a simple json parser that will deserialize the response into an internal `JsonObject` that can be easily traversed when using `dynamic` (with all the caveats of bypassing static typing). If you need strongly typed responses, I'd recommend using NEST which exposes all requests and responses as strong types.

---

<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: [September 9, 2017, 7:47am UTC](https://discuss.elastic.co/t/elasticsearch-net-and-multisearch/96602/5 "2017-09-09T07:47:34Z")

</div>

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