github.com/m3db/m3@v1.5.0/src/m3ninx/search/query/field.go (about) 1 // Copyright (c) 2019 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 query 22 23 import ( 24 "bytes" 25 "strings" 26 27 "github.com/m3db/m3/src/m3ninx/generated/proto/querypb" 28 "github.com/m3db/m3/src/m3ninx/search" 29 "github.com/m3db/m3/src/m3ninx/search/searcher" 30 ) 31 32 // FieldQuery finds document which have the given field exactly. 33 type FieldQuery struct { 34 str string 35 field []byte 36 } 37 38 // NewFieldQuery constructs a new FieldQuery for the given field. 39 func NewFieldQuery(field []byte) search.Query { 40 q := &FieldQuery{ 41 field: field, 42 } 43 q.str = q.string() 44 return q 45 } 46 47 // Field returns the field []byte. 48 func (q *FieldQuery) Field() []byte { 49 return q.field 50 } 51 52 // Searcher returns a searcher over the provided readers. 53 func (q *FieldQuery) Searcher() (search.Searcher, error) { 54 return searcher.NewFieldSearcher(q.field) 55 } 56 57 // Equal reports whether q is equivalent to o. 58 func (q *FieldQuery) Equal(o search.Query) bool { 59 o, ok := singular(o) 60 if !ok { 61 return false 62 } 63 64 inner, ok := o.(*FieldQuery) 65 if !ok { 66 return false 67 } 68 69 return bytes.Equal(q.field, inner.field) 70 } 71 72 // ToProto returns the Protobuf query struct corresponding to the term query. 73 func (q *FieldQuery) ToProto() *querypb.Query { 74 term := querypb.FieldQuery{ 75 Field: q.field, 76 } 77 78 return &querypb.Query{ 79 Query: &querypb.Query_Field{Field: &term}, 80 } 81 } 82 83 func (q *FieldQuery) String() string { 84 return q.str 85 } 86 87 func (q *FieldQuery) string() string { 88 var str strings.Builder 89 str.WriteString("field(") 90 str.Write(q.field) 91 str.WriteRune(')') 92 return str.String() 93 }