Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] TermSet query generates an empty query instead #371

Closed
DumboJet opened this issue Sep 21, 2023 · 5 comments
Closed

[BUG] TermSet query generates an empty query instead #371

DumboJet opened this issue Sep 21, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@DumboJet
Copy link

DumboJet commented Sep 21, 2023

What is the bug?

I cannot build a TermSet query that uses a script.
When I try to do so, an empty query gets sent.

How can one reproduce the bug?

Just use this code that tries to select documents with a field matching any value in a set of values:

var response = openSearchClient.Search<Document>(s => s
            .Index(documentsIndexName)
            .Query(q => q
                .Bool(bq => bq
                    .Must(mq => mq
                         .TermsSet(f => f.Field(d => d.fieldName).Terms(new int[]{3,7,80}).MinimumShouldMatchScript(sr => sr.Source("1"))
                    )
                )
            ));

I have also tried this without success:

return mq.TermsSet(f => f
                .Field(d => d.fieldName)
                .Terms(new int[]{3,7,80})
                .MinimumShouldMatchScript(sr => sr
                                .Source("params.minMatchCount")
                                .Lang(ScriptLang.Expression)
                                .Params(new Dictionary<string, object>{ {"minMatchCount", 1 } })
                )
            );

What is the expected behavior?

A TermSet query gets sent to the OpenSearch node.

What is your host/environment?

Windows 11 Pro

Do you have any screenshots?

Do you have any additional context?

There is this related StackOverflow question: https://stackoverflow.com/q/77143853/2173353
This is what I get in the log file (opensearch_index_search_slowlog.log) :

[documents][0] took[167.3micros], took_millis[0], total_hits[1 hits], stats[], search_type[QUERY_THEN_FETCH], total_shards[1], source[{}], id[], 

I use the latest Nuget package (v.1.5.0).

@DumboJet DumboJet added bug Something isn't working untriaged labels Sep 21, 2023
@DumboJet DumboJet changed the title [BUG] TermSet query generates an empty query isntead [BUG] TermSet query generates an empty query instead Sep 21, 2023
@DumboJet
Copy link
Author

I have now found that using .Terms() probably achieves the same with less pain, but the bug report remains :

var response = openSearchClient.Search<Document>(s => s
            .Index(documentsIndexName)
            .Query(q => q
                .Bool(bq => bq
                    .Must(mq => mq
                         .Terms(f => f.Field(d => d.fieldName).Terms(new int[]{3,7,80})
                    )
                )
            ));

@Xtansia Xtansia removed the untriaged label Sep 22, 2023
@Xtansia
Copy link
Collaborator

Xtansia commented Sep 22, 2023

Using the following code:

var response = client.Search<Doc>(s => s
            .Index("my-index")
            .Query(q => q
                .Bool(bq => bq
                    .Must(mq => mq
                        .TermsSet(f =>
                            f.Field(d => d.Foo).Terms(new int[] { 3, 7, 80 })
                                .MinimumShouldMatchScript(sr => sr.Source("1"))
                        )
                    )
                )));
        Console.WriteLine(Encoding.UTF8.GetString(response.ApiCall.RequestBodyInBytes));

I'm able to confirm it's generating the expected search query:

{"query":{"bool":{"must":[{"terms_set":{"foo":{"minimum_should_match_script":{"source":"1"},"terms":[3,7,80]}}}]}}}

I can confirm though that if you had initially excluded MinimumShouldMatchScript that the query is treated as conditionless and wouldn't be serialized.

@DumboJet
Copy link
Author

Hm...
This looks identical to what I was trying, and which gave me an empty query.
But I have tried again now, and I got a query.

Weird.
Is there any way to make the library throw exceptions when something vital is missing in a query?
I see that there is a .Strict(bool) method, but I am not sure how well it works, and you have to apply it on each sub-query, from what I have seen.

You can close this issue if you like.

@Xtansia
Copy link
Collaborator

Xtansia commented Sep 24, 2023

Weird. Is there any way to make the library throw exceptions when something vital is missing in a query? I see that there is a .Strict(bool) method, but I am not sure how well it works, and you have to apply it on each sub-query, from what I have seen.

You're correct regarding .Strict()/.IsStrict in that there's currently no mechanism to automatically propagate it's behaviour to every branch of a query. If you have suggestions on a good way to provide this behaviour or even a willingness to have a go at implementing it, that'd be fantastic and I'd love to hear your thoughts. Maybe you'd like to file that as a feature request issue? Otherwise we can try re-working this issue to represent the feature request?

@DumboJet
Copy link
Author

@Xtansia
Yes, I could create a new feature request issue, and close this issue.
I have not looked deep into the code yet, but if/when I do, I might get more suggestions on how this could be done.

Here is the feature request issue: #375

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants