# Query on multiple index using NEST

**URL:** <https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200>\
**Category:** Elasticsearch\
**Created:** [June 11, 2019, 1:40pm UTC](https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200 "2019-06-11T13:40:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandru](https://avatars.discourse-cdn.com/v4/letter/c/fbc32d/32.png) [@Chandru](https://discuss.elastic.co/u/Chandru)\
**Post date:** [June 11, 2019, 1:40pm UTC](https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200/1 "2019-06-11T13:40:23Z")

</div>

Hi All ,

i'm new to elasticsearch ,and trying to implement "Free Text search" and the challenge to fetch records from two index , but there is no straightforward way to do this and i got the below link and i followed the same but no results , and not as expected hits count always 0

link [Link to stack overflow on My version and code](https://stackoverflow.com/questions/56478093/full-text-search-with-multiple-index-in-elastic-search-using-nest-c-sharp?noredirect=1&lq=1)

[The example which i followed in stackoverflow](https://stackoverflow.com/questions/47836986/how-to-search-inside-multiple-indices-using-nest-elasticsearch?noredirect=1&lq=1)

please let me if you need any additional informational last 3 days i was searching for a solution but no luck any help would be appreciated

example 2 : multi phrase search

combinedResponse.elasticSearchResponse = elasticClient.Search(s =\> s  
.Index(indices)  
.Type(Types.Type(typeof(ComponentTypeES), typeof(BotProjectTypeES)))  
.Query(q =\> (q  
.MatchPhrase(m =\> m  
.Field(Infer.Field(ff =\> ff.Componentname)).Analyzer("standard")  
.Query(query).Analyzer("standard").Boost(100)  
) && +q  
.Term("\_index", componentindex)) || (q  
.MatchPhrase(m =\> m  
.Field(Infer.Field(ff =\> ff.Projectname)).Analyzer("standard")  
.Query(query).Analyzer("standard").Boost(100)  
)  
) && +q  
.Term("\_index", projectindex)  
).From(page - 1)  
.Size(pagesize)  
);

---

<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:** [June 14, 2019, 9:13am UTC](https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200/2 "2019-06-14T09:13:04Z")

</div>

[I answered your same question on Stack Overflow](https://stackoverflow.com/a/56572101/1831), but will include here for completeness.

The issue is that the casing of properties within the fields in the index mappings, and the casing of properties that NEST will send by default are different, so the nested `must` clauses within the `should` clause of the `bool` query that is generated by NEST will never be matched by any documents because of the field casing difference.

[NEST by default camel cases property names](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/field-inference.html#camel-casing), but the fields in the index mappings and documents in your example are all pascal cased, so the field names generated by NEST will not match those within the mapping. You can easily change NEST's field casing behaviour by using the `DefaultFieldNameInferrer(Func<string, string>)` method on `ConnectionSettings`. A delegate that simply returns the string value passed will leave field names as they are on the POCOs.

Here's a complete working example

```auto
private static void Main()
{
	const string componentIndex = "componenttypeindex";
	const string projectIndex = "projecttypeindex";

	var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

	var settings = new ConnectionSettings(pool)
		.DefaultIndex(componentIndex)
		.DefaultMappingFor<ComponentTypES>(i => i.IndexName(componentIndex).TypeName("Componenttype").IdProperty(f => f.ComponentID))
		.DefaultMappingFor<ProjectTypES>(j => j.IndexName(projectIndex).TypeName("Projecttype").IdProperty(f => f.ProjectID))
		.DefaultFieldNameInferrer(f => f)
		.DefaultTypeName("_doc")
		.DisableDirectStreaming()
		.PrettyJson()
		.OnRequestCompleted(callDetails =>
		{
			if (callDetails.RequestBodyInBytes != null)
			{
				Console.WriteLine(
					$"{callDetails.HttpMethod} {callDetails.Uri} \n" +
					$"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
			}
			else
			{
				Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
			}

			Console.WriteLine();

			if (callDetails.ResponseBodyInBytes != null)
			{
				Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
						 $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
						 $"{new string('-', 30)}\n");
			}
			else
			{
				Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
						 $"{new string('-', 30)}\n");
			}
		});

	var client = new ElasticClient(settings);

	foreach (var index in new[] { componentIndex, projectIndex })
	{
		if (client.IndexExists(index).Exists)
			client.DeleteIndex(index);

		client.CreateIndex(index, c => c
			.Mappings(m =>
			{
				if (index == projectIndex)
					return m.Map<ProjectTypES>(mm => mm.AutoMap());
				else
					return m.Map<ComponentTypES>(mm => mm.AutoMap());
			})
		);
	}

	client.Bulk(b => b
		.IndexMany(new[] {
				new ComponentTypES
				{
					ComponentID = "5342e739-1635-4021-baf2-55e25b95b8ec",
					Componentname = "TestComponent1",
					Summary = "this is summary of test component1"
				},
				new ComponentTypES
				{
					ComponentID = "90781386-8065-11e9-bc42-526af7764f64",
					Componentname = "TestComponent2",
					Summary = "this is summary of test component3"
				},
				new ComponentTypES
				{
					ComponentID = "19871386-8065-11e9-bc42-526af7764f64",
					Componentname = "some xyz component test",
					Summary = "this is summary test of test xyz"
				},
		})
		.IndexMany(new[] {
				new ProjectTypES
				{
					ProjectID = "5342e739-2019-4021-baf2-55e25b95b8ec",
					Projectname = "Test Project1",
					Summary = "summary of Test Project1",
					Description = "Description of TestProject1"
				},
				new ProjectTypES
				{
					ProjectID = "5342f739-2019-4021-baf2-55e25b95b8ba",
					Projectname = "Test Project2",
					Summary = "summary of Test Project2",
					Description = "Description of TestProject1"
				},
				new ProjectTypES
				{
					ProjectID = "6342f739-2020-4021-baf2-55e25b95b8ac",
					Projectname = "some PQRS project",
					Summary = "summary of PQRS Project",
					Description = "Description of PQORS Project1"
				},
		})
		.Refresh(Refresh.WaitFor)
	);

	var query = "test";

	var response = client.Search<object>(s => s
		.Index(Indices.Index(typeof(ComponentTypES)).And(typeof(ProjectTypES)))
		.Type(Types.Type(typeof(ComponentTypES), typeof(ProjectTypES)))
		.Query(q =>
			(q
				.MultiMatch(m => m
					.Fields(f => f
						.Field(Infer.Field<ComponentTypES>(ff => ff.Componentname))
						.Field(Infer.Field<ComponentTypES>(ff => ff.Summary, 1.1))
					)
					.Operator(Operator.Or)
					.Query(query)
				) && +q
				.Term("_index", componentIndex)
			) ||
			(q
				.MultiMatch(m => m
					.Fields(f => f
						.Field(Infer.Field<ProjectTypES>(ff => ff.Projectname))
						.Field(Infer.Field<ProjectTypES>(ff => ff.Summary, 0.3))
					)
					.Operator(Operator.Or)
					.Query(query)
				) && +q
				.Term("_index", projectIndex)
			)
		)
	);
}

public class ComponentTypES
{
	public string ComponentID { get; set; }
	public string Componentname { get; set; }
	public string Summary { get; set; }

}

public class ProjectTypES
{

	public string ProjectID { get; set; }
	public string Projectname { get; set; }
	public string Summary { get; set; }
	public string Description { get; set; }
}

```

The resulting JSON query for the search is

```
POST http://localhost:9200/componenttypeindex%2Cprojecttypeindex/Componenttype%2CProjecttype/_search?pretty=true&typed_keys=true 
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": {
                    "value": "componenttypeindex"
                  }
                }
              }
            ],
            "must": [
              {
                "multi_match": {
                  "fields": [
                    "Componentname",
                    "Summary^1.1"
                  ],
                  "operator": "or",
                  "query": "test"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_index": {
                    "value": "projecttypeindex"
                  }
                }
              }
            ],
            "must": [
              {
                "multi_match": {
                  "fields": [
                    "Projectname",
                    "Summary^0.3"
                  ],
                  "operator": "or",
                  "query": "test"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

which returns 5 results

---

<div class="post-metadata">

**Author:** ![Chandru](https://avatars.discourse-cdn.com/v4/letter/c/fbc32d/32.png) [@Chandru](https://discuss.elastic.co/u/Chandru)\
**Post date:** [June 17, 2019, 5:40am UTC](https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200/3 "2019-06-17T05:40:59Z")

</div>

Thanks a lot man you saved the day ,i'm new to elastic search , i've added few changes in the comment section in stack overflow , if you find time please suggest any solutions

---

<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:** [June 17, 2019, 9:01am UTC](https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200/4 "2019-06-17T09:01:29Z")

</div>

It may be easier to continue the discussion here @Chandru as it's more designed for back and forth discussion.

---

<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:** [July 15, 2019, 9:01am UTC](https://discuss.elastic.co/t/query-on-multiple-index-using-nest/185200/5 "2019-07-15T09:01:30Z")

</div>

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