Difficulty to perform search operation in an array of items in new Elasticsearchclient library in dotnet

Hi,
I am trying to run this C# code in dotnet8.0
using Elastic.Clients.Elasticsearch library version 8.15.10

var searchResult = await _elasticsearchClient.SearchAsync<Product>(s => s
    .Index("products") // Name of the index
    .Size(0) // Set to 0 if you don't want to return any documents
    .Query(q => q
        .Bool(b => b
            .Must(
                m => m.Terms(t => t
                    .Field(f => f.CategoryId) // Field to match
                    .Terms(categoryIds) // Assume categoryIds is an array of category IDs
                )
            )
        )
    )
);

While writing this code, it is showing error (Cannot resolve symbol 'categoryIds') in .Terms(categoryIds) line
Anyone have any idea to resolve this?

Hi,

cannot resolve symbol 'X'

is a compiler error. This means you have not declared the array categoryIds (or not declared in the correct scope).

This has nothing to do with the Elasticsearch client itself, but if you show a little bit more code, I'm happy to help you anyways.

Please find the Code

//code block
public async Task SearchProductsByCategory()
{
// Declare and initialize the array with category IDs to search for
string categoryIds = { "electronics", "furniture", "clothing" };

    // Perform the search
    var searchResult = await _elasticsearchClient.SearchAsync<Product>(s => s
      .Index("products") // Specify the index name
      .Size(0) // Set to 0 if you don't want to return any documents
      .Query(q => q
        .Bool(b => b
          .Must(
            m => m.Terms(t => t
                .Field(f => f.CategoryId) // Field to match in the Product index
                .Terms(categoryIds) // Use the categoryIds array here
            )
          )
        )
      )
    );
    
  }

This is the code i am trying in dotnet, The exact problem is,
there is an error at the part Must, .Terms(categoryIds) line of the code

These are the problem descriptions found in the riderconsole:

for the line of **.Must( **
Ambiguous invocation:
Elastic.Clients.Elasticsearch.QueryDsl.BoolQueryDescriptor<Product> Must(params System.Action<Elastic.Clients.Elasticsearch.QueryDsl.QueryDescriptor<Product>>[]) (in class BoolQueryDescriptor<Product>)
Elastic.Clients.Elasticsearch.QueryDsl.BoolQueryDescriptor<Product> Must(System.Action<Elastic.Clients.Elasticsearch.QueryDsl.QueryDescriptor<Product>>) (in class BoolQueryDescriptor<Product>)
match

for the line of .Terms(categoryIds)

Cannot resolve symbol 'Terms'

Previously in Nest, i was able to use with no problem at all,
it wasnt giving this error indication for the same code

I suspect that by using the new ElasticSearchClient Library, It is not able to identify the second Terms statement in the given code

Cannot resolve symbol 'Terms'

Could you please try Term() (without the s) instead of the second Terms()?

yeah tried,

  public async Task SearchProductsByCategory()
  {
    // Declare and initialize the array with category IDs to search for
    string[] categoryIds = { "electronics", "furniture", "clothing" };

    // Perform the search
    var searchResult = await _elasticsearchClient.SearchAsync<Product>(s => s
      .Index("products") // Specify the index name
      .Size(0) // Set to 0 if you don't want to return any documents
      .Query(q => q
        .Bool(
          b => b.Must(m => m.Terms(t => t.Field(f => f.CategoryId).Term(categoryIds)
            )
          )
        )
      )
    );

  }

I got this error for this line b => b.Must(m => m.Terms(t => t.Field(f => f.CategoryId).Term(categoryIds)

Argument type 'string' is not assignable to parameter type 'Elastic.Clients.Elasticsearch.QueryDsl.TermsQueryField'

FieldValue[] categoryIds = { "electronics", "furniture", "clothing" };

var searchResult = await client.SearchAsync<Product>(s => s
      .Index("products") // Specify the index name
      .Size(0) // Set to 0 if you don't want to return any documents
      .Query(q => q
        .Bool(b => b
          .Must(
            m => m.Terms(t => t
                .Field(f => f.CategoryId) // Field to match in the Product index
                .Term(new TermsQueryField(categoryIds)) // Use the categoryIds array here
            )
          )
        )
      )
    );
1 Like

Thanks a lot for this, it has solved my issue