|
| 1 | +// Copyright (c) 2025 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 | + |
| 15 | +package query |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/blevesearch/bleve/v2/mapping" |
| 23 | + "github.com/blevesearch/bleve/v2/search" |
| 24 | + "github.com/blevesearch/bleve/v2/search/searcher" |
| 25 | + "github.com/blevesearch/bleve/v2/util" |
| 26 | + index "github.com/blevesearch/bleve_index_api" |
| 27 | +) |
| 28 | + |
| 29 | +type NestedQuery struct { |
| 30 | + Path string `json:"path"` |
| 31 | + InnerQuery Query `json:"query"` |
| 32 | +} |
| 33 | + |
| 34 | +func NewNestedQuery(path string, innerQuery Query) *NestedQuery { |
| 35 | + return &NestedQuery{ |
| 36 | + Path: path, |
| 37 | + InnerQuery: innerQuery, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (q *NestedQuery) Searcher(ctx context.Context, i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) { |
| 42 | + nr, ok := i.(index.NestedReader) |
| 43 | + if !ok { |
| 44 | + return nil, fmt.Errorf("nested searcher requires an index reader that supports nested documents") |
| 45 | + } |
| 46 | + childCount := nr.ChildCount(q.Path) |
| 47 | + if childCount == 0 { |
| 48 | + return nil, fmt.Errorf("nested searcher: path %q has no child documents", q.Path) |
| 49 | + } |
| 50 | + innerSearchers := make([]search.Searcher, 0, childCount) |
| 51 | + for arrayPos := range childCount { |
| 52 | + nctx := context.WithValue(ctx, search.NestedInfoCallbackKey, &search.NestedInfo{ |
| 53 | + Path: q.Path, |
| 54 | + ArrayPosition: arrayPos, |
| 55 | + }) |
| 56 | + innerSearcher, err := q.InnerQuery.Searcher(nctx, i, m, options) |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("nested searcher: failed to create inner searcher at pos %d: %w", arrayPos, err) |
| 59 | + } |
| 60 | + innerSearchers = append(innerSearchers, innerSearcher) |
| 61 | + } |
| 62 | + return searcher.NewDisjunctionSearcher(ctx, i, innerSearchers, 0, options) |
| 63 | +} |
| 64 | + |
| 65 | +func (q *NestedQuery) Validate() error { |
| 66 | + if q.Path == "" { |
| 67 | + return fmt.Errorf("nested query must have a path") |
| 68 | + } |
| 69 | + if q.InnerQuery == nil { |
| 70 | + return fmt.Errorf("nested query must have a query") |
| 71 | + } |
| 72 | + if vq, ok := q.InnerQuery.(ValidatableQuery); ok { |
| 73 | + if err := vq.Validate(); err != nil { |
| 74 | + return fmt.Errorf("nested query must have a valid query: %v", err) |
| 75 | + } |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (q *NestedQuery) UnmarshalJSON(data []byte) error { |
| 81 | + tmp := struct { |
| 82 | + Path string `json:"path"` |
| 83 | + Query json.RawMessage `json:"query"` |
| 84 | + }{} |
| 85 | + err := util.UnmarshalJSON(data, &tmp) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if tmp.Path == "" { |
| 90 | + return fmt.Errorf("nested query must have a path") |
| 91 | + } |
| 92 | + if tmp.Query == nil { |
| 93 | + return fmt.Errorf("nested query must have a query") |
| 94 | + } |
| 95 | + q.Path = tmp.Path |
| 96 | + q.InnerQuery, err = ParseQuery(tmp.Query) |
| 97 | + if err != nil || q.InnerQuery == nil { |
| 98 | + return fmt.Errorf("nested query must have a valid query: %v", err) |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
0 commit comments