Dynamically Create Query in using asp dotnet client

The following is a query written in C#. It leverages Elastic.Clients.Elasticsearch version 8.13.7. This is NOT the NEST client.

The query uses myTenants which is passed in as a parameter to the method. The type of myTenants is IEnumerable<Tuple<string, string>>. Each item in the list represents and ORI/CRI pair. I need to update the query to traverse the entire myTenants list. The query should return all SubmissionDto where the ORI/CRI combination is equal to those in myTenants

I've tried a bunch of different queries cant seem to get it to work

Hi Tiffany,

I'm not an expert with .NET client but I'll try to help. I have a couple questions:

  1. myTenants.ElementAt(0).Item1.ToLower() and myTenants.ElementAt(0).Item2.ToLower() are static values, right?
  2. Have you checked the actual queries that are generated by the client?

IMO the best approach to figure out what the problem is would be to make a query in Kibana Dev tools and then try to use it via the client, debugging which query is created by the client itself. This guide might be helpful to see the query generated by the client.

Im not sure what you mean by if they are static values. I was just doing some testing to make sure the query worked with 1 ori/cri pair. The issues is i need multiple ori/cri pairs but I am not sure how to construct that query dynamically ( based on the array ) in c#. I am using the kibana tool to debug also. THanks for the resposne

Im not sure what you mean by if they are static values

Consider the following examples using SQL:

SELECT * FROM Customers WHERE language_code = "en_us"

The query above uses static value while filtering - "en_us".

Then another query:

SELECT * FROM Customers WHERE home_phone = mobile_phone

This query does not use static values - it queries the data based on the data itself.

Queries with static values are easy and should be achievable using regular query statements that you have - must, filter and similar.

If you want to make a query that returns results based on the filter that checks values within document (like in the example - if customer has same phone number specified for home and mobile) you will need to do it via scripting like here: Accessing document fields and special variables | Elasticsearch Guide [8.13] | Elastic

oh ok i misunderstood static in this context. No the values are not static. This was me just making sure the query worked when there was just 1 ori/cri pair. I am trying to achieve the following logic

(Ori = A && Cri = B) || (Ori = C && Cri = D) || ...

So the query needs to be dynamically built based on what is in myTenants

I see. Indeed it should be possible with the approach that you're taking. Have you had a chance to build a query yourself in Kibana Dev Tools and compare to the query generated by the client?

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