1// Copyright (c) 2014 Couchbase, Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package bleve 16 17import ( 18 "time" 19 20 "github.com/blevesearch/bleve/search/query" 21) 22 23// NewBoolFieldQuery creates a new Query for boolean fields 24func NewBoolFieldQuery(val bool) *query.BoolFieldQuery { 25 return query.NewBoolFieldQuery(val) 26} 27 28// NewBooleanQuery creates a compound Query composed 29// of several other Query objects. 30// These other query objects are added using the 31// AddMust() AddShould() and AddMustNot() methods. 32// Result documents must satisfy ALL of the 33// must Queries. 34// Result documents must satisfy NONE of the must not 35// Queries. 36// Result documents that ALSO satisfy any of the should 37// Queries will score higher. 38func NewBooleanQuery() *query.BooleanQuery { 39 return query.NewBooleanQuery(nil, nil, nil) 40} 41 42// NewConjunctionQuery creates a new compound Query. 43// Result documents must satisfy all of the queries. 44func NewConjunctionQuery(conjuncts ...query.Query) *query.ConjunctionQuery { 45 return query.NewConjunctionQuery(conjuncts) 46} 47 48// NewDateRangeQuery creates a new Query for ranges 49// of date values. 50// Date strings are parsed using the DateTimeParser configured in the 51// top-level config.QueryDateTimeParser 52// Either, but not both endpoints can be nil. 53func NewDateRangeQuery(start, end time.Time) *query.DateRangeQuery { 54 return query.NewDateRangeQuery(start, end) 55} 56 57// NewDateRangeInclusiveQuery creates a new Query for ranges 58// of date values. 59// Date strings are parsed using the DateTimeParser configured in the 60// top-level config.QueryDateTimeParser 61// Either, but not both endpoints can be nil. 62// startInclusive and endInclusive control inclusion of the endpoints. 63func NewDateRangeInclusiveQuery(start, end time.Time, startInclusive, endInclusive *bool) *query.DateRangeQuery { 64 return query.NewDateRangeInclusiveQuery(start, end, startInclusive, endInclusive) 65} 66 67// NewDisjunctionQuery creates a new compound Query. 68// Result documents satisfy at least one Query. 69func NewDisjunctionQuery(disjuncts ...query.Query) *query.DisjunctionQuery { 70 return query.NewDisjunctionQuery(disjuncts) 71} 72 73// NewDocIDQuery creates a new Query object returning indexed documents among 74// the specified set. Combine it with ConjunctionQuery to restrict the scope of 75// other queries output. 76func NewDocIDQuery(ids []string) *query.DocIDQuery { 77 return query.NewDocIDQuery(ids) 78} 79 80// NewFuzzyQuery creates a new Query which finds 81// documents containing terms within a specific 82// fuzziness of the specified term. 83// The default fuzziness is 1. 84// 85// The current implementation uses Levenshtein edit 86// distance as the fuzziness metric. 87func NewFuzzyQuery(term string) *query.FuzzyQuery { 88 return query.NewFuzzyQuery(term) 89} 90 91// NewMatchAllQuery creates a Query which will 92// match all documents in the index. 93func NewMatchAllQuery() *query.MatchAllQuery { 94 return query.NewMatchAllQuery() 95} 96 97// NewMatchNoneQuery creates a Query which will not 98// match any documents in the index. 99func NewMatchNoneQuery() *query.MatchNoneQuery { 100 return query.NewMatchNoneQuery() 101} 102 103// NewMatchPhraseQuery creates a new Query object 104// for matching phrases in the index. 105// An Analyzer is chosen based on the field. 106// Input text is analyzed using this analyzer. 107// Token terms resulting from this analysis are 108// used to build a search phrase. Result documents 109// must match this phrase. Queried field must have been indexed with 110// IncludeTermVectors set to true. 111func NewMatchPhraseQuery(matchPhrase string) *query.MatchPhraseQuery { 112 return query.NewMatchPhraseQuery(matchPhrase) 113} 114 115// NewMatchQuery creates a Query for matching text. 116// An Analyzer is chosen based on the field. 117// Input text is analyzed using this analyzer. 118// Token terms resulting from this analysis are 119// used to perform term searches. Result documents 120// must satisfy at least one of these term searches. 121func NewMatchQuery(match string) *query.MatchQuery { 122 return query.NewMatchQuery(match) 123} 124 125// NewNumericRangeQuery creates a new Query for ranges 126// of numeric values. 127// Either, but not both endpoints can be nil. 128// The minimum value is inclusive. 129// The maximum value is exclusive. 130func NewNumericRangeQuery(min, max *float64) *query.NumericRangeQuery { 131 return query.NewNumericRangeQuery(min, max) 132} 133 134// NewNumericRangeInclusiveQuery creates a new Query for ranges 135// of numeric values. 136// Either, but not both endpoints can be nil. 137// Control endpoint inclusion with inclusiveMin, inclusiveMax. 138func NewNumericRangeInclusiveQuery(min, max *float64, minInclusive, maxInclusive *bool) *query.NumericRangeQuery { 139 return query.NewNumericRangeInclusiveQuery(min, max, minInclusive, maxInclusive) 140} 141 142// NewTermRangeQuery creates a new Query for ranges 143// of text terms. 144// Either, but not both endpoints can be "". 145// The minimum value is inclusive. 146// The maximum value is exclusive. 147func NewTermRangeQuery(min, max string) *query.TermRangeQuery { 148 return query.NewTermRangeQuery(min, max) 149} 150 151// NewTermRangeInclusiveQuery creates a new Query for ranges 152// of text terms. 153// Either, but not both endpoints can be "". 154// Control endpoint inclusion with inclusiveMin, inclusiveMax. 155func NewTermRangeInclusiveQuery(min, max string, minInclusive, maxInclusive *bool) *query.TermRangeQuery { 156 return query.NewTermRangeInclusiveQuery(min, max, minInclusive, maxInclusive) 157} 158 159// NewPhraseQuery creates a new Query for finding 160// exact term phrases in the index. 161// The provided terms must exist in the correct 162// order, at the correct index offsets, in the 163// specified field. Queried field must have been indexed with 164// IncludeTermVectors set to true. 165func NewPhraseQuery(terms []string, field string) *query.PhraseQuery { 166 return query.NewPhraseQuery(terms, field) 167} 168 169// NewPrefixQuery creates a new Query which finds 170// documents containing terms that start with the 171// specified prefix. 172func NewPrefixQuery(prefix string) *query.PrefixQuery { 173 return query.NewPrefixQuery(prefix) 174} 175 176// NewRegexpQuery creates a new Query which finds 177// documents containing terms that match the 178// specified regular expression. 179func NewRegexpQuery(regexp string) *query.RegexpQuery { 180 return query.NewRegexpQuery(regexp) 181} 182 183// NewQueryStringQuery creates a new Query used for 184// finding documents that satisfy a query string. The 185// query string is a small query language for humans. 186func NewQueryStringQuery(q string) *query.QueryStringQuery { 187 return query.NewQueryStringQuery(q) 188} 189 190// NewTermQuery creates a new Query for finding an 191// exact term match in the index. 192func NewTermQuery(term string) *query.TermQuery { 193 return query.NewTermQuery(term) 194} 195 196// NewWildcardQuery creates a new Query which finds 197// documents containing terms that match the 198// specified wildcard. In the wildcard pattern '*' 199// will match any sequence of 0 or more characters, 200// and '?' will match any single character. 201func NewWildcardQuery(wildcard string) *query.WildcardQuery { 202 return query.NewWildcardQuery(wildcard) 203} 204 205// NewGeoBoundingBoxQuery creates a new Query for performing geo bounding 206// box searches. The arguments describe the position of the box and documents 207// which have an indexed geo point inside the box will be returned. 208func NewGeoBoundingBoxQuery(topLeftLon, topLeftLat, bottomRightLon, bottomRightLat float64) *query.GeoBoundingBoxQuery { 209 return query.NewGeoBoundingBoxQuery(topLeftLon, topLeftLat, bottomRightLon, bottomRightLat) 210} 211 212// NewGeoDistanceQuery creates a new Query for performing geo distance 213// searches. The arguments describe a position and a distance. Documents 214// which have an indexed geo point which is less than or equal to the provided 215// distance from the given position will be returned. 216func NewGeoDistanceQuery(lon, lat float64, distance string) *query.GeoDistanceQuery { 217 return query.NewGeoDistanceQuery(lon, lat, distance) 218} 219