Terms Set Query Usage
editTerms Set Query Usage
editReturns any documents that match with at least one or more of the provided terms. The terms are not analyzed and thus must match exactly. The number of terms that must match varies per document and is either controlled by a minimum should match field or computed per document in a minimum should match script.
Be sure to read the Elasticsearch documentation on Terms Set query for more information.
Minimum should match with field
editThe field that controls the number of required terms that must match must be a number field
Fluent DSL example
editq .TermsSet(c => c .Name("named_query") .Boost(1.1) .Field(p => p.Branches) .Terms("master", "dev") .MinimumShouldMatchField(p => p.RequiredBranches) )
Object Initializer syntax example
editnew TermsSetQuery { Name = "named_query", Boost = 1.1, Field = Infer.Field<Project>(p => p.Branches), Terms = new[] { "master", "dev" }, MinimumShouldMatchField = Infer.Field<Project>(p => p.RequiredBranches) }
Example json output.
{ "terms_set": { "branches": { "_name": "named_query", "boost": 1.1, "terms": [ "master", "dev" ], "minimum_should_match_field": "requiredBranches" } } }
Minimum should match with script
editScripts can also be used to control how many terms are required to match in a more dynamic way.
The params.num_terms
parameter is available in the script to indicate the number of
terms that have been specified in the query.
Fluent DSL example
editq .TermsSet(c => c .Name("named_query") .Boost(1.1) .Field(p => p.Branches) .Terms("master", "dev") .MinimumShouldMatchScript(s => s .Source("doc['requiredBranches'].size() == 0 ? params.num_terms : Math.min(params.num_terms, doc['requiredBranches'].value)") ) )
Object Initializer syntax example
editnew TermsSetQuery { Name = "named_query", Boost = 1.1, Field = Infer.Field<Project>(p => p.Branches), Terms = new[] { "master", "dev" }, MinimumShouldMatchScript = new InlineScript("doc['requiredBranches'].size() == 0 ? params.num_terms : Math.min(params.num_terms, doc['requiredBranches'].value)") }
Example json output.
{ "terms_set": { "branches": { "_name": "named_query", "boost": 1.1, "terms": [ "master", "dev" ], "minimum_should_match_script": { "source": "doc['requiredBranches'].size() == 0 ? params.num_terms : Math.min(params.num_terms, doc['requiredBranches'].value)" } } } }