github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/idx/query.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package idx
    22  
    23  import (
    24  	"github.com/m3db/m3/src/m3ninx/search"
    25  	"github.com/m3db/m3/src/m3ninx/search/query"
    26  )
    27  
    28  // Query encapsulates a search query for an index.
    29  type Query struct {
    30  	query search.Query
    31  }
    32  
    33  func (q Query) String() string {
    34  	return q.query.String()
    35  }
    36  
    37  // NewQueryFromSearchQuery creates a new Query from a search.Query.
    38  func NewQueryFromSearchQuery(q search.Query) Query {
    39  	return Query{
    40  		query: q,
    41  	}
    42  }
    43  
    44  // NewAllQuery returns a new query for matching all documents.
    45  func NewAllQuery() Query {
    46  	return Query{
    47  		query: query.NewAllQuery(),
    48  	}
    49  }
    50  
    51  // NewFieldQuery returns a new query for finding documents which match a field exactly.
    52  func NewFieldQuery(field []byte) Query {
    53  	return Query{
    54  		query: query.NewFieldQuery(field),
    55  	}
    56  }
    57  
    58  // FieldQuery returns a bool indicating whether the Query is a FieldQuery,
    59  // and the backing bytes of the Field.
    60  func FieldQuery(q Query) (field []byte, ok bool) {
    61  	fieldQuery, ok := q.query.(*query.FieldQuery)
    62  	if !ok {
    63  		return nil, false
    64  	}
    65  	return fieldQuery.Field(), true
    66  }
    67  
    68  // NewTermQuery returns a new query for finding documents which match a term exactly.
    69  func NewTermQuery(field, term []byte) Query {
    70  	return Query{
    71  		query: query.NewTermQuery(field, term),
    72  	}
    73  }
    74  
    75  // NewRegexpQuery returns a new query for finding documents which match a regular expression.
    76  func NewRegexpQuery(field, regexp []byte) (Query, error) {
    77  	q, err := query.NewRegexpQuery(field, regexp)
    78  	if err != nil {
    79  		return Query{}, err
    80  	}
    81  	return Query{
    82  		query: q,
    83  	}, nil
    84  }
    85  
    86  // MustCreateRegexpQuery is like NewRegexpQuery but panics if the query cannot be created.
    87  func MustCreateRegexpQuery(field, regexp []byte) Query {
    88  	q, err := query.NewRegexpQuery(field, regexp)
    89  	if err != nil {
    90  		panic(err)
    91  	}
    92  	return Query{
    93  		query: q,
    94  	}
    95  }
    96  
    97  // NewNegationQuery returns a new query for finding documents which don't match a given query.
    98  func NewNegationQuery(q Query) Query {
    99  	return Query{
   100  		query: query.NewNegationQuery(q.SearchQuery()),
   101  	}
   102  }
   103  
   104  // NewConjunctionQuery returns a new query for finding documents which match each of the
   105  // given queries.
   106  func NewConjunctionQuery(queries ...Query) Query {
   107  	qs := make([]search.Query, 0, len(queries))
   108  	for _, q := range queries {
   109  		qs = append(qs, q.query)
   110  	}
   111  	return Query{
   112  		query: query.NewConjunctionQuery(qs),
   113  	}
   114  }
   115  
   116  // NewDisjunctionQuery returns a new query for finding documents which match at least one
   117  // of the given queries.
   118  func NewDisjunctionQuery(queries ...Query) Query {
   119  	qs := make([]search.Query, 0, len(queries))
   120  	for _, q := range queries {
   121  		qs = append(qs, q.query)
   122  	}
   123  	return Query{
   124  		query: query.NewDisjunctionQuery(qs),
   125  	}
   126  }
   127  
   128  // SearchQuery returns the underlying search query for use during execution.
   129  func (q Query) SearchQuery() search.Query {
   130  	return q.query
   131  }
   132  
   133  // Equal reports whether q is equal to o.
   134  func (q Query) Equal(o Query) bool {
   135  	return q.query.Equal(o.query)
   136  }