code.gitea.io/gitea@v1.22.3/modules/indexer/internal/bleve/query.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package bleve
     5  
     6  import (
     7  	"code.gitea.io/gitea/modules/optional"
     8  
     9  	"github.com/blevesearch/bleve/v2"
    10  	"github.com/blevesearch/bleve/v2/search/query"
    11  )
    12  
    13  // NumericEqualityQuery generates a numeric equality query for the given value and field
    14  func NumericEqualityQuery(value int64, field string) *query.NumericRangeQuery {
    15  	f := float64(value)
    16  	tru := true
    17  	q := bleve.NewNumericRangeInclusiveQuery(&f, &f, &tru, &tru)
    18  	q.SetField(field)
    19  	return q
    20  }
    21  
    22  // MatchPhraseQuery generates a match phrase query for the given phrase, field and analyzer
    23  func MatchPhraseQuery(matchPhrase, field, analyzer string, fuzziness int) *query.MatchPhraseQuery {
    24  	q := bleve.NewMatchPhraseQuery(matchPhrase)
    25  	q.FieldVal = field
    26  	q.Analyzer = analyzer
    27  	q.Fuzziness = fuzziness
    28  	return q
    29  }
    30  
    31  // BoolFieldQuery generates a bool field query for the given value and field
    32  func BoolFieldQuery(value bool, field string) *query.BoolFieldQuery {
    33  	q := bleve.NewBoolFieldQuery(value)
    34  	q.SetField(field)
    35  	return q
    36  }
    37  
    38  func NumericRangeInclusiveQuery(min, max optional.Option[int64], field string) *query.NumericRangeQuery {
    39  	var minF, maxF *float64
    40  	var minI, maxI *bool
    41  	if min.Has() {
    42  		minF = new(float64)
    43  		*minF = float64(min.Value())
    44  		minI = new(bool)
    45  		*minI = true
    46  	}
    47  	if max.Has() {
    48  		maxF = new(float64)
    49  		*maxF = float64(max.Value())
    50  		maxI = new(bool)
    51  		*maxI = true
    52  	}
    53  	q := bleve.NewNumericRangeInclusiveQuery(minF, maxF, minI, maxI)
    54  	q.SetField(field)
    55  	return q
    56  }