### Install Go-Query-DSL Source: https://github.com/companyinfo/go-query-dsl/blob/main/README.md Use 'go get' to install the package. Import it into your Go code using the provided path. ```bash go get -u "go.companyinfo.dev/go-query-dsl" ``` ```bash import "go.companyinfo.dev/go-query-dsl" ``` -------------------------------- ### Regex Query Example Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Construct a regex query to match terms against a regular expression pattern. Options include setting flags, case-insensitivity, and maximum determinized states. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/regex" ) func main() { qb := query.New() // Search using regular expression regexQuery := regex.New("phone", regex.NewParam("\\+1-[0-9]{3}-[0-9]{4}"). SetFlags("ALL"). SetCaseInsensitive(true). SetMaxDeterminizedStates(20000). SetBoost(1.0)) qb.SetQuery(regexQuery).SetSize(25) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"regex":{"phone":{"value":"\\+1-[0-9]{3}-[0-9]{4}","case_insensitive":true,"flags":"ALL","max_determinized_states":20000}}},"size":25} } ``` -------------------------------- ### Wildcard Query Example Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use the wildcard query to find documents matching a pattern. Supports '*' for any sequence and '?' for a single character. Case-insensitivity and rewrite strategies can be configured. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/wildcard" ) func main() { qb := query.New() // Search using wildcard patterns wildcardQuery := wildcard.New("email", wildcard.NewParam("*@example.com"). SetBoost(1.0). SetCaseInsensitive(true). SetRewrite("constant_score")) qb.SetQuery(wildcardQuery).SetSize(50) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"wildcard":{"email":{"value":"*@example.com","case_insensitive":true,"rewrite":"constant_score"}}},"size":50} } ``` -------------------------------- ### IDs Query Example Source: https://context7.com/companyinfo/go-query-dsl/llms.txt The IDs query allows fetching documents directly by their unique document IDs. Boost can be set for scoring adjustments. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/ids" ) func main() { qb := query.New() // Find documents by their IDs idsQuery := ids.New( ids.NewParam([]int{1, 2, 3, 4, 5}).SetBoost(1.0)) qb.SetQuery(idsQuery).SetSize(10) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"ids":{"values":[1,2,3,4,5]}},"size":10} } ``` -------------------------------- ### Exists Query Example Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use the exists query to retrieve documents where a specific field contains an indexed value. Boost can be applied to influence scoring. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/exists" ) func main() { qb := query.New() // Find documents where a field exists existsQuery := exists.New( exists.NewParam("email").SetBoost(1.0)) qb.SetQuery(existsQuery).SetSize(100) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"exists":{"field":"email"}}},"size":100} } ``` -------------------------------- ### Terms Set Query Examples Source: https://context7.com/companyinfo/go-query-dsl/llms.txt The terms_set query finds documents with a minimum number of exact terms from a list in a field. It supports specifying a field for the minimum match count or using a script for more complex logic. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/termsset" ) func main() { qb := query.New() // Search with minimum matching terms requirement termsSetQuery := termsset.New("programming_languages", termsset.NewParam([]string{"go", "python", "java", "rust"}). SetMinimumShouldMatchField("required_languages"). SetBoost(1.0)) qb.SetQuery(termsSetQuery).SetSize(20) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"terms_set":{"programming_languages":{"terms":["go","python","java","rust"],"minimum_should_match_field":"required_languages"}}},"size":20} // Using script for minimum match qb2 := query.New() scriptQuery := termsset.New("skills", termsset.NewParam([]string{"docker", "kubernetes", "terraform"}). SetMinimumShouldMatchScript("Math.min(params.num_terms, doc['required_skills'].value)")) qb2.SetQuery(scriptQuery).SetSize(15) jsonQuery2, _ := qb2.Build() fmt.Println(string(jsonQuery2)) // Output: {"query":{"terms_set":{"skills":{"terms":["docker","kubernetes","terraform"],"minimum_should_match_script":{"source":"Math.min(params.num_terms, doc['required_skills'].value)"}}}},"size":15} } ``` -------------------------------- ### Build a Basic Match Query in Go Source: https://github.com/companyinfo/go-query-dsl/blob/main/README.md Create a new query builder and set a match query for the 'first_name' field. Ensure error handling when building the final query. ```go func main() { qb := query.New() qb.SetQuery(boolean.New().AddMust(match.New("first_name", match.NewParam("tom")))) q, err := qb.Build() if err != nil { return fmt.Errorf("failed on building a query: %w", err) } ... } ``` -------------------------------- ### Build Complete Query with Go Query DSL Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Constructs a comprehensive query including search, sorting, pagination, and aggregation using the Go Query DSL builder. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/aggregation" "go.companyinfo.dev/go-query-dsl/query/compound/boolean" "go.companyinfo.dev/go-query-dsl/query/full_text/match" "go.companyinfo.dev/go-query-dsl/query/sort" ) func main() { qb := query.New() // Build a complete query with search, sorting, pagination, and aggregation qb.SetQuery(boolean.New().SetBoost(1.0).AddMust(match.New("first_name", match.NewParam("tom")))). SetFrom(0). SetSize(10). SetSource(true). AddFields([]string{"first_name", "last_name", "age"}). AddSort(sort.New("age", sort.NewParam().SetOrder(sort.AscOrder).SetMode(sort.MinMode))). AddAggregation("age_aggregation", aggregation.New().AddTerms("age", 20)) jsonQuery, err := qb.Build() if err != nil { fmt.Printf("Error building query: %v\n", err) return } fmt.Println(string(jsonQuery)) // Output: {"sort":[{"age":{"order":"asc","mode":"min"}}],"query":{"bool":{"must":[{"match":{"first_name":{"query":"tom"}}}],"boost":1}},"aggs":{"age_aggregation":{"terms":{"field":"age","size":20}}},"from":0,"size":10,"fields":["first_name","last_name","age"],"_source":true} } ``` -------------------------------- ### Go: Create a Prefix Query Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Find documents where a field's terms begin with a specified prefix. Supports case-insensitivity and rewrite strategies. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/prefix" ) func main() { qb := query.New() // Search for terms starting with a prefix prefixQuery := prefix.New("product_code", prefix.NewParam("PRD-"). SetBoost(1.0). SetCaseInsensitive(true). SetRewrite("constant_score")) qb.SetQuery(prefixQuery).SetSize(100) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"prefix":{"product_code":{"value":"PRD-","rewrite":"constant_score","case_insensitive":true}}},"size":100} } ``` -------------------------------- ### Implement Autocomplete Suggestions in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Utilize the suggestion feature for autocomplete functionality with fuzzy matching. SetSize(0) ensures only suggestion results are returned. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/suggestion" ) func main() { qb := query.New() // Autocomplete suggestion with fuzzy matching autocomplete := suggestion.NewAutocomplete( suggestion.NewAutocompleteParam("elast", "suggest_field"). SetSize(10). SetFuzziness("AUTO")) qb.AddSuggestion(autocomplete).SetSize(0) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"suggest":{"autocomplete":{"prefix":"elast","completion":{"field":"suggest_field","size":10,"fuzzy":{"fuzziness":"AUTO"}}}},"size":0} } ``` -------------------------------- ### Create Boosting Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use the boosting query to return documents matching a positive query while reducing the score of documents that also match a negative query. Set the negative boost factor to control the score reduction. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/compound/boosting" "go.companyinfo.dev/go-query-dsl/query/full_text/match" "go.companyinfo.dev/go-query-dsl/query/term_level/term" ) func main() { qb := query.New() // Boost documents matching positive query, demote those matching negative boostingQuery := boosting.New(). SetPositive(match.New("content", match.NewParam("elasticsearch"))). SetNegative(term.New("category", term.NewParam("outdated"))). SetNegativeBoost(0.5) qb.SetQuery(boostingQuery).SetSize(30) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"boosting":{"positive":{"match":{"content":{"query":"elasticsearch"}}},"negative":{"term":{"category":{"value":"outdated"}}},"negative_boost":0.5}},"size":30} } ``` -------------------------------- ### Multi-Match Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use this to search across multiple fields with different matching strategies like best_fields. Configure fields, type, operator, tie-breaker, fuzziness, and boost. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/full_text/match" "go.companyinfo.dev/go-query-dsl/query/full_text/multimatch" ) func main() { qb := query.New() // Search across multiple fields with best_fields strategy multiMatchQuery := multimatch.New( multimatch.NewParam("search keywords") .SetFields([]string{"title^3", "description", "content"}) .SetType(multimatch.BestFields) .SetOperator(match.OrOperator) .SetTieBreaker(0.3) .SetFuzziness("AUTO") .SetBoost(2.0)) qb.SetQuery(multiMatchQuery).SetSize(25) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"multi_match":{"query":"search keywords","fuzziness":"AUTO","type":"best_fields","operator":"or","fields":["title^3","description","content"],"tie_breaker":0.3,"boost":2}},"size":25} } ``` -------------------------------- ### Bool Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Combine multiple queries using must, should, must_not, and filter clauses for complex logical operations. Configure minimum should match and boost. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/compound/boolean" "go.companyinfo.dev/go-query-dsl/query/full_text/match" "go.companyinfo.dev/go-query-dsl/query/term_level/term" ranging "go.companyinfo.dev/go-query-dsl/query/term_level/range" ) func main() { qb := query.New() // Complex bool query with must, should, must_not, and filter boolQuery := boolean.New(). AddMust(match.New("title", match.NewParam("elasticsearch"))). AddMust(match.New("content", match.NewParam("search engine"))). AddShould(match.New("tags", match.NewParam("tutorial"))). AddMustNot(term.New("status", term.NewParam("draft"))). AddFilter(ranging.New("publish_date", ranging.NewParam().SetGTE("2023-01-01").SetLTE("2024-12-31"))). SetMinimumShouldMatch(1). SetBoost(1.5) qb.SetQuery(boolQuery).SetSize(20) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"bool":{"must":[{"match":{"title":{"query":"elasticsearch"}}},{"match":{"content":{"query":"search engine"}}}]}},"must_not":[{"term":{"status":{"value":"draft"}}}],"should":[{"match":{"tags":{"query":"tutorial"}}}],"filter":[{"range":{"publish_date":{"gte":"2023-01-01","lte":"2024-12-31"}}}],"minimum_should_match":1,"boost":1.5}},"size":20} } ``` -------------------------------- ### Go: Create a Terms Query Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use this to search for documents containing one or more exact terms in a specified field. Supports lookup from another document. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/terms" ) func main() { qb := query.New() // Search for multiple exact terms termsQuery := terms.New("category", []string{"electronics", "computers", "accessories"}) qb.SetQuery(termsQuery).SetSize(50) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"terms":{"category":["electronics","computers","accessories"]}},"size":50} // Terms lookup from another document qb2 := query.New() lookupQuery := terms.New("tags", terms.NewLookup("users", "user123", "favorite_tags"). SetRouting("user_routing")) qb2.SetQuery(lookupQuery).SetSize(20) jsonQuery2, _ := qb2.Build() fmt.Println(string(jsonQuery2)) // Output: {"query":{"terms":{"tags":{"index":"users","id":"user123","path":"favorite_tags","routing":"user_routing"}}},"size":20} } ``` -------------------------------- ### Create Match Phrase Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Builds a match_phrase query to search for exact phrases with optional slop for word reordering tolerance. Use when the order of terms is important. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/full_text/matchphrase" ) func main() { qb := query.New() // Search for an exact phrase with slop tolerance phraseQuery := matchphrase.New("content", matchphrase.NewParam("quick brown fox") .SetSlop(2) .SetAnalyzer("english")) qb.SetQuery(phraseQuery).SetSize(10) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"match_phrase":{"content":{"query":"quick brown fox","analyzer":"english","slop":2}}},"size":10} } ``` -------------------------------- ### Create Match Query with Fuzzy Matching in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Generates a full-text match query with fuzzy matching and other advanced parameters. Use for text analysis and approximate matching. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/full_text/match" ) func main() { qb := query.New() // Create a match query with fuzzy matching and other parameters matchQuery := match.New("description", match.NewParam("search text") .SetOperator(match.AndOperator) .SetFuzziness("AUTO") .SetBoost(1.5) .SetMinimumShouldMatch(2) .SetLenient(true) .SetAnalyzer("standard") .SetZeroTermsQuery(match.AllZeroTermType)) qb.SetQuery(matchQuery).SetSize(20) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"match":{"description":{"query":"search text","analyzer":"standard","fuzziness":"AUTO","zero_terms_query":"all","operator":"and","minimum_should_match":2,"boost":1.5,"lenient":true}}},"size":20} } ``` -------------------------------- ### Go: Create a Fuzzy Query Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Perform fuzzy searches to find terms similar to the input, measured by Levenshtein edit distance. Useful for typos. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/fuzzy" ) func main() { qb := query.New() // Fuzzy search with customized parameters fuzzyQuery := fuzzy.New("username", fuzzy.NewParam("johnsn"). SetFuzziness("AUTO"). SetPrefixLength(2). SetMaxExpansions(50). SetTranspositions(true). SetBoost(1.0)) qb.SetQuery(fuzzyQuery).SetSize(10) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"fuzzy":{"username":{"value":"johnsn","fuzziness":"AUTO","max_expansions":50,"prefix_length":2,"transpositions":true}}},"size":10} } ``` -------------------------------- ### Go: Create a Numeric Range Query Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Construct a query to find documents with numeric values within a specified range. Supports boosting results. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" ranging "go.companyinfo.dev/go-query-dsl/query/term_level/range" ) func main() { qb := query.New() // Numeric range query numericRange := ranging.New("price", ranging.NewParam(). SetGTE("100"). SetLTE("500"). SetBoost(1.5)) qb.SetQuery(numericRange).SetSize(25) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"range":{"price":{"gte":"100","lte":"500","boost":1.5}}},"size":25} // Date range query with timezone qb2 := query.New() dateRange := ranging.New("created_at", ranging.NewParam(). SetGTE("2023-01-01"). SetLT("2024-01-01"). SetFormat("yyyy-MM-dd"). SetTimezone("+01:00")) qb2.SetQuery(dateRange).SetSize(100) jsonQuery2, _ := qb2.Build() fmt.Println(string(jsonQuery2)) // Output: {"query":{"range":{"created_at":{"gte":"2023-01-01","lt":"2024-01-01","format":"yyyy-MM-dd","timezone":"+01:00"}}},"size":100} } ``` -------------------------------- ### Query String Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Construct complex query strings using Lucene syntax for field-specific searches, logical operators, wildcards, and regular expressions. Configure fields, default operator, fuzziness, and wildcard behavior. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/full_text/match" "go.companyinfo.dev/go-query-dsl/query/full_text/querystring" ) func main() { qb := query.New() // Complex query string with Lucene syntax queryStringQuery := querystring.New( querystring.NewParam("(title:elasticsearch OR content:search) AND status:published") .SetFields([]string{"title", "content", "status"}) .SetDefaultOperator(match.AndOperator) .SetFuzziness("AUTO") .SetAllowLeadingWildcard(false) .SetAnalyzeWildcard(true) .SetBoost(1.0)) qb.SetQuery(queryStringQuery).SetSize(50) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"query_string":{"query":"(title:elasticsearch OR content:search) AND status:published","allow_leading_wildcard":false,"analyze_wildcard":true,"default_operator":"and","fields":["title","content","status"],"fuzziness":"AUTO","boost":1}},"size":50} } ``` -------------------------------- ### Term Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Find documents with an exact term in a field without analyzing the search term. Configure boost and case insensitivity. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/term_level/term" ) func main() { qb := query.New() // Exact term match with case insensitivity termQuery := term.New("status", term.NewParam("published") .SetBoost(1.2) .SetCaseInsensitive(true)) qb.SetQuery(termQuery).SetSize(100) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"term":{"status":{"value":"published","boost":1.2,"case_insensitive":true}}},"size":100} } ``` -------------------------------- ### Create Disjunction Max Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use the dis_max query to return documents that match any of the provided query clauses, scoring them based on the highest score from any single matching clause. Optionally set a tie-breaker value. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/compound/disjunctionmax" "go.companyinfo.dev/go-query-dsl/query/full_text/match" ) func main() { qb := query.New() // Get highest score from multiple query clauses queries := []any{ match.New("title", match.NewParam("elasticsearch")), match.New("body", match.NewParam("elasticsearch")), match.New("tags", match.NewParam("elasticsearch")), } disMaxQuery := disjunctionmax.New(queries).SetTieBreaker(0.7) qb.SetQuery(disMaxQuery).SetSize(20) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"dis_max":{"queries":[{"match":{"title":{"query":"elasticsearch"}}},{"match":{"body":{"query":"elasticsearch"}}},{"match":{"tags":{"query":"elasticsearch"}}}],"tie_breaker":0.7}},"size":20} } ``` -------------------------------- ### Perform Multiple Aggregations in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Combine various aggregation types like terms, avg, sum, range, and histogram in a single query. SetSize(0) is used to return only aggregation results. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/aggregation" "go.companyinfo.dev/go-query-dsl/query/full_text/match" ) func main() { qb := query.New() // Multiple aggregations in a single query qb.SetQuery(match.New("category", match.NewParam("electronics"))). AddAggregation("brands", aggregation.New().AddTerms("brand", 10)). AddAggregation("avg_price", aggregation.New().AddAvg("price")), AddAggregation("max_price", aggregation.New().AddMax("price")), AddAggregation("min_price", aggregation.New().AddMin("price")), AddAggregation("total_sales", aggregation.New().AddSum("sales_count")), AddAggregation("price_ranges", aggregation.New().AddRange("price", []map[string]any{ {"to": 100}, {"from": 100, "to": 500}, {"from": 500}, })). AddAggregation("price_histogram", aggregation.New().AddHistogram("price", 50)). AddAggregation("sales_by_month", aggregation.New().AddDateHistogram("sale_date", "month")), SetSize(0) // Size 0 to only return aggregation results jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output includes all aggregations: terms, avg, max, min, sum, range, histogram, date_histogram // Nested aggregations qb2 := query.New() nestedAgg := aggregation.New().AddTerms("brand", 10). AddNested(aggregation.New().AddAvg("price")) qb2.SetQuery(match.New("category", match.NewParam("electronics"))). AddAggregation("brand_with_avg_price", nestedAgg). SetSize(0) jsonQuery2, _ := qb2.Build() fmt.Println(string(jsonQuery2)) } ``` -------------------------------- ### Create Constant Score Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt The constant_score query assigns a fixed score to all matching documents. Wrap a filter query and set a boost value to determine the constant score. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/compound/constantscore" "go.companyinfo.dev/go-query-dsl/query/term_level/term" ) func main() { qb := query.New() // All matching documents get the same score constantScoreQuery := constantscore.New(). SetFilter(term.New("status", term.NewParam("active"))). SetBoost(1.5) qb.SetQuery(constantScoreQuery).SetSize(50) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"constant_score":{"filter":{"term":{"status":{"value":"active"}}},"boost":1.5}},"size":50} } ``` -------------------------------- ### Create Combined Fields Query in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt The combined_fields query allows searching across multiple text fields as if they were a single combined field. Configure operator and minimum should match parameters for precise control. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/full_text/combinedfields" "go.companyinfo.dev/go-query-dsl/query/full_text/match" ) func main() { qb := query.New() // Search across multiple fields as a single combined field combinedQuery := combinedfields.New("search", combinedfields.NewParam("quick brown fox", []string{"title", "abstract", "body"}). SetOperator(match.AndOperator). SetMinimumShouldMatch(2)) qb.SetQuery(combinedQuery).SetSize(25) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"query":{"combined_fields":{"search":{"fields":["title","abstract","body"],"query":"quick brown fox","operator":"and","minimum_should_match":2}}},"size":25} } ``` -------------------------------- ### Set Multiple Sort Criteria in Go Source: https://context7.com/companyinfo/go-query-dsl/llms.txt Use AddSort to specify multiple sorting criteria, including order, mode, and numeric type casting. SetSize limits the number of results. ```go package main import ( "fmt" "go.companyinfo.dev/go-query-dsl/query" "go.companyinfo.dev/go-query-dsl/query/full_text/match" "go.companyinfo.dev/go-query-dsl/query/sort" "go.companyinfo.dev/go-query-dsl/query/term_level/term" ) func main() { qb := query.New() // Multiple sort criteria qb.SetQuery(match.New("content", match.NewParam("search"))). AddSort(sort.New("publish_date", sort.NewParam().SetOrder(sort.DescOrder))). AddSort(sort.New("score", sort.NewParam().SetOrder(sort.DescOrder).SetMode(sort.AvgMode))). AddSort(sort.New("price", sort.NewParam(). SetOrder(sort.AscOrder). SetNumericType(sort.DoubleNumericType))). SetSize(50) jsonQuery, _ := qb.Build() fmt.Println(string(jsonQuery)) // Output: {"sort":[{"publish_date":{"order":"desc"}},{"score":{"order":"desc","mode":"avg"}},{"price":{"order":"asc","numeric_type":"double"}}],"query":{"match":{"content":{"query":"search"}}},"size":50} // Nested sort with filter qb2 := query.New() qb2.SetQuery(match.New("title", match.NewParam("product"))). AddSort(sort.New("offers.price", sort.NewParam(). SetOrder(sort.AscOrder). SetMode(sort.MinMode). SetNested("offers", term.New("offers.active", term.NewParam(true))))). SetSize(20) jsonQuery2, _ := qb2.Build() fmt.Println(string(jsonQuery2)) // Output: {"sort":[{"offers.price":{"order":"asc","mode":"min","nested":{"path":"offers","filter":{"term":{"offers.active":{"value":true}}}}}}],"query":{"match":{"title":{"query":"product"}}},"size":20} } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.