P_R
P_R

Reputation: 488

ElasticSearch 8.15 c# Client

I have this query in Kibana that works well but I can't translate it using c# 8.15 client

GET product_it/_search // _search
{
"query": {
"bool": {
  "filter":
    {
      "terms": {
        "slug": [
          "slug1",
          "slug2"
        ]
      }
    }
}

} }

I translated in this part of code (obviusly slugs is a list of string)

var result = await Client.SearchAsync<Product>(s => s
            .Index(idsIndex)
            .Query(q => q.Bool(t => t.Filter(f => f.Terms())),cancellationToken);

but when I try to insert the correct Terms query I can't insert correctly my slugs

Upvotes: 0

Views: 193

Answers (1)

Qiang Fu
Qiang Fu

Reputation: 8411

I think they missed something in the latest 8.15.x version of Elastic.Clients.Elasticsearch that Terms cannot work properly. But in a lower version, Such as 8.13.x. It could be implemented like following:

var slugs = new string[] { "slug1","slug2" };
var response = await esClient.SearchAsync<Product>(s => s
    .Index("index3")
    .Query(q => q
        .Bool(b => b
            .Filter(f => f
                .Terms(ts => ts
                    .Field(d=>d.slug)
                    .Terms(new TermsQueryField(slugs.Select(x => FieldValue.String(x)).ToArray()))
                )
            )
        )
    )
);

if (response.IsValidResponse)
{
    var doc2 = response.Documents;
}

Test result
enter image description here

Upvotes: 0

Related Questions