Search code examples
c#opensearch

Can I use OpenSearch.Client to build a "query string" query?


I am using the OpenSearch.Client to access an OpenSearch node.

I have seen examples of using C# expressions to build various OpenSearch queries, like this one. This seems helpful enough, to avoid building the query yourself 100% manually.

But in my case, I want to use a query string that will contain much nested AND/OR logic (here is what my query might look like, in case you are interested). And I see that the single method for issuing such a query string accepts a string argument for the query:

openSearchClient
    .Search<Document>(s => s
        .Index("documents")
        .QueryOnQueryString(queryonquerystring: "...")
    );

Is there a way to build this queryonquerystring string query using C# expressions code?

Does OpenSearch.Client support it or I will have to use another library (maybe Lucene.NET)?


Solution

  • I am not sure if using Lucene.NET could build such a query, but it turns out that using the OpenSearch.Client way of making queries works much better:

    var response = openSearchClient.Search<Document>(s => s
                .Index(documentsIndexName)
                .Query(q => q
                    .Bool(bq => bq
                        .Must(mq =>
                        {
                             return mq.Term(d => d.field1, 0) && (!mq.Term(d => d.field2, 3) || mq.Term(d => d.field3, 7));
                        });