I want to to find the documents that have a field match any of the values in a set, say an array of new int[]{3,7,80}
.
I see that I could potentially do it like that:
var response = openSearchClient.Search<Document>(s => s
.Index(documentsIndexName)
.Query(q => q
.Bool(bq => bq
.Must(mq => mq.Term("fieldName", 3) || mq.Term("filedName", 7) || mq.Term("filedName", 8))
)
);
Maybe I could also build this query in a programmatic loop as well.
But isn't there a better way?
I see there is a .TermSet()
method (documentation here), but it doesn't seem to produce the expected results for me:
var response = openSearchClient.Search<Document>(s => s
.Index(documentsIndexName)
.Query(q => q
.Bool(bq => bq
.Must(mq => TermsSet(f => f.Field(d => d.fieldName).Terms(new int[]{3,7,80})))
)
);
I have enabled logging of the queries and I see that the code above generates empty queries.
Can anyone tell me how this is done?
Update:
I think I should include a script for the minimum_should_match
parameter. But I have tried this now, and it doesn't work:
mq.TermsSet(f => f.Field(d => d.fieldName).Terms(new int[]{3,7,80}).MinimumShouldMatchScript(sr => sr.Source("1")))
You can indeed use the terms_set
query to find documents where a field matches any of the values in a set (an array in this case).
Try this :
var response = openSearchClient.Search<Document>(s => s
.Index(documentsIndexName)
.Query(q => q
.Bool(bq => bq
.Must(mq => mq
.TermsSet(ts => ts
.Field(d => d.fieldName)
.Terms(new int[] { 3, 7, 80 })
.MinimumShouldMatchScript(sr => sr
.Source("1") // You can adjust the script based on your requirements
)
)
)
)
)
);